10 Binary Tree Data Structures - PowerPoint PPT Presentation

About This Presentation
Title:

10 Binary Tree Data Structures

Description:

A binary search tree (or BST) is a binary tree with the following property. ... To find which if any node of a BST contains an element equal to target: ... – PowerPoint PPT presentation

Number of Views:169
Avg rating:3.0/5.0
Slides: 70
Provided by: Watt83
Category:
Tags: binary | bst | data | structures | tree

less

Transcript and Presenter's Notes

Title: 10 Binary Tree Data Structures


1
10Binary Tree Data Structures
  • Binary trees and binary search trees.
  • Searching.
  • Insertion.
  • Deletion.
  • Traversal.
  • Implementation of sets using BSTs.

2
Binary trees (1)
  • A binary tree consists of a header, plus a number
    of nodes connected by links in a hierarchical
    data structure
  • Each node contains an element (value or object),
    plus links to at most two other nodes (its left
    child and right child).
  • The header contains a link to a node designated
    as the root node.

3
Binary trees (2)
  • A leaf node is one that has no children (i.e.,
    both its links are null).
  • Every node, except the root node, is the left or
    right child of exactly one other node (its
    parent). The root node has no parent the only
    link to it is the header.
  • The size of a binary tree is the number of nodes
    (elements).
  • An empty binary tree has size zero. Its header is
    null.

4
Binary trees and subtrees (1)
  • Each node has both a left subtree and a right
    subtree (either of which may be empty). The
    nodes left (right) subtree consists of the
    nodes left (right) child together with that
    childs own children, grandchildren, etc.

5
Binary trees and subtrees (2)
  • Each subtree is itself a binary tree.
  • This gives rise to an equivalent recursive
    definition. A binary tree is
  • empty, or
  • nonempty, in which case it has a root node
    containing an element, a link to a left subtree,
    and a link to a right subtree.

6
Node and tree depths (1)
  • Observation For any node N in a tree, there is
    exactly one sequence of links between the root
    node and N.
  • The depth of node N is the number of links
    between the root node and N.
  • The depth of a tree is the depth of the deepest
    node in the tree.
  • A tree consisting of a single node has depth 0.
  • By convention, an empty tree has depth 1.

7
Node and tree depths (2)
  • Illustrations

8
Node and tree depths (3)
  • Illustrations (continued)

very ill-balanced
9
Balanced binary trees
  • A binary tree of depth d is balanced if all nodes
    at depths 0, 1, , d2 have two children.
  • Nodes at depth d1 may have two/one/no children.
  • Node at depth d have no children (by definition).
  • A binary tree of depth 0 or 1 is always balanced.
  • A balanced binary tree of depth d has at least 2d
    and at most 2d1 1 nodes. Conversely
  • Depth of balanced binary tree of size n
    floor(log2 n)
  • An ill-balanced binary tree of depth d could have
    as few as d1 nodes. Conversely
  • Max. depth of ill-balanced binary tree of size n
    n1

10
Binary search trees (1)
  • A binary search tree (or BST) is a binary tree
    with the following property. For any node in the
    binary tree, if that node contains element elem
  • Its left subtree (if nonempty) contains only
    elements less than elem.
  • Its right subtree (if nonempty) contains only
    elements greater than elem.

11
Binary search trees (2)
  • Illustrations

12
Binary search trees (3)
  • An equivalent recursive definition is also
    possible. A binary search tree is
  • empty, or
  • nonempty, in which case it has
  • a root node containing an element elem
  • a link to a left subtree in which (if it is not
    empty) all elements are less than elem
  • a link to a right subtree in which (if it is not
    empty) all elements are greater than elem.

13
Binary search trees (4)
  • Java class implementing BST nodes
  • public class BSTNode
  • protected Comparable element protected
    BSTNode left, right
  • protected BSTNode (Comparable elem) element
    elem left null right null

BSTNode methods (to follow)
14
Binary search trees (5)
  • Java class implementing BST headers
  • public class BST
  • private BSTNode root
  • public BST () // Construct an empty
    BST. root null

BST methods (to follow)
15
BST search (1)
  • Problem Search for a given target value in a
    BST.
  • Idea Compare the target with the element in the
    root node.
  • If it is equal, the search is successful.
  • If it is less, search the left subtree.
  • If it is greater, search the right subtree.
  • If the subtree is empty, the search is
    unsuccessful.

16
BST search (2)
  • BST search algorithm
  • To find which if any node of a BST contains an
    element equal to target
  • 1. Set curr to the BSTs root.2. Repeat 2.1. I
    f curr is null 2.1.1. Terminate with answer
    none. 2.2. Otherwise, if target is equal to
    currs element 2.2.1. Terminate with answer
    curr. 2.3. Otherwise, if target is less than
    currs element 2.3.1. Set curr to currs left
    child. 2.4. Otherwise, if target is greater than
    currs element 2.4.1. Set curr to currs right
    child.

17
BST search (3)
  • Animation (successful search)

18
BST search (4)
  • Animation (unsuccessful search)

19
BST search (5)
  • Analysis (counting comparisons)
  • Let the BSTs size be n.
  • If the BST has depth d, the number of
    comparisons is at most d1.
  • If the BST is well-balanced, its depth is
    floor(log2 n)
  • Max. no. of comparisons floor(log2 n) 1
  • Best-case time complexity is O(log n).
  • If the BST is ill-balanced, its depth is at most
    n1
  • Max. no. of comparisons n
  • Worst-case time complexity is O(n).

20
BST search (6)
  • Implementation as a Java method (in class BST)
  • public BSTNode search (Comparable target) int
    direction 0 BSTNode curr root for ()
    if (curr null) return null direction
    target.compareTo(curr.element) if
    (direction 0) return curr else if
    (direction lt 0) curr curr.left else curr
    curr.right

21
BST insertion (1)
  • Idea To insert a new element into a BST, proceed
    as if searching for that element. If the element
    is not already present, the search will lead to a
    null link. Replace that null link by a link to a
    leaf node containing the new element.

22
BST insertion (2)
  • BST insertion algorithm
  • To insert the element elem into a BST
  • 1. Set parent to null, and set curr to the BSTs
    root.2. Repeat 2.1. If curr is
    null 2.1.1. Replace the null link from which
    curr was taken (either the BSTs root or
    parents left child or parents right child)
    by a link to a newly-created leaf node with
    element elem. 2.1.2. Terminate. 2.2. Otherwise,
    if elem is equal to currs element 2.2.1. Term
    inate. 2.3. Otherwise,

23
BST insertion (3)
  • BST insertion algorithm (continued)
  • 2.3. Otherwise, if elem is less than currs
    element 2.3.1. Set parent to curr, and set
    curr to currs left child. 2.4. Otherwise, if
    elem is greater than currs element 2.4.1. Set
    parent to curr, and set curr to currs right
    child.

24
BST insertion (4)
  • Animation (empty BST)

25
BST insertion (5)
  • Animation (nonempty BST)

26
BST insertion (6)
  • Analysis (counting comparisons)
  • No. of comparisons is the same as for BST
    search.
  • If the BST is well-balanced
  • Max. no. of comparisons floor(log2 n) 1
  • Best-case time complexity is O(log n).
  • If the BST is ill-balanced
  • Max. no. of comparisons n
  • Worst-case time complexity is O(n).

27
BST insertion (7)
  • Implementation as a Java method (in class BST)
  • public void insert (Comparable elem) int
    direction 0 BSTNode parent null, curr
    root for () if (curr null)
    BSTNode ins new BSTNode(elem) if (root
    null) root ins else if (direction lt
    0) parent.left ins else parent.right
    ins return

28
BST insertion (8)
  • Implementation (continued)
  • direction elem.compareTo(curr.element) if
    (direction 0) return parent curr if
    (direction lt 0) curr curr.left else curr
    curr.right

29
BSTs in practice insertions (1)
  • Whether a BST is well-balanced or ill-balanced
    depends on the order of insertions.
  • If the inserted elements are randomly ordered,
    the BST will probably be reasonably
    well-balanced.
  • If the inserted elements happen to be in
    ascending (or descending) order, the BST will be
    extremely ill-balanced.

30
Example 1 successive insertions (1)
  • Animation (inserting lion, fox, rat, cat,
    pig, dog, tiger)

31
Example 1 (2)
  • Animation (inserting cat, dog, fox, lion,
    pig, rat)

32
BSTs in practice insertions (2)
  • The following trials show the results of loading
    a BST with n randomly-generated elements.
  • First trial with n 35

33
BSTs in practice insertions (3)
  • Second trial with n 35

34
BST deletion
  • Deleting a subtrees leftmost element.
  • Deleting a subtrees topmost element.
  • Deleting an arbitrary given element in a BST.

35
Deleting a leftmost element (1)
  • Problem Delete the leftmost element in a
    subtree.
  • Two cases to consider
  • The subtrees topmost node has no left child.
  • The subtrees topmost node has a left child.
  • Note By definition, the leftmost node has no
    left child.

36
Deleting a leftmost element (2)
  • Case 1 (topmost node has no left child) Replace
    the subtree by its own right subtree. E.g.

leftmost node
37
Deleting a leftmost element (3)
  • Case 2 (topmost node has a left child) Link the
    leftmost nodes parent to the leftmost nodes
    right child. E.g.

leftmost node
garbage
38
Deleting a leftmost element (4)
  • Algorithm
  • To delete the leftmost element in the (nonempty)
    subtree whose topmost node is top
  • 1. If top has no left child 1.1. Terminate
    with tops right child as answer.2. If top has a
    left child 2.1. Set parent to top, and set curr
    to tops left child. 2.2. While node curr has a
    left child, repeat 2.2.1. Set parent to curr,
    and set curr to currs left child. 2.3. Set
    parents left child to currs right
    child. 2.4. Terminate with top as answer.

case 1
case 2
39
Deleting a leftmost element (5)
  • Implementation as a Java method (in class
    BSTNode)
  • private BSTNode deleteLeftmost () if
    (this.left null) return this.right else
    BSTNode parent this, curr
    this.left while (curr.left ! null)
    parent curr curr curr.left paren
    t.left curr.right return this

40
Deleting a topmost element (1)
  • Problem Delete the topmost element in a subtree.
  • Four cases to consider
  • The topmost node has no children.
  • The topmost node has a right child but no left
    child.
  • The topmost node has a left child but no right
    child.
  • The topmost node has two children.

41
Deleting a topmost element (2)
  • Case 1 (topmost node has no children) Replace
    the subtree by an empty subtree. E.g.

42
Deleting a topmost element (3)
  • Case 2 (topmost node has a right child but no
    left child)Replace the subtree by its own right
    subtree. E.g.

43
Deleting a topmost element (4)
  • Case 3 (topmost node has a left child but no
    right child)Replace the subtree by its own left
    subtree. E.g.

44
Deleting a topmost element (5)
  • Case 4 (topmost node has two children) Copy the
    right subtrees leftmost element into the topmost
    node, then delete the right subtrees leftmost
    element. E.g.

garbage
45
Deleting a topmost element (6)
  • Algorithm
  • To delete the topmost element in the subtree
    whose topmost node is top
  • 1. If top has no left child 1.1. Terminate
    with tops right child as answer.2. If top has
    no right child 2.1. Terminate with tops left
    child as answer.3. If top has two
    children 3.1. Set tops element to the leftmost
    element in tops right subtree. 3.2. Delete
    the leftmost element in tops right
    subtree. 3.3. Terminate with top as answer.

cases 1, 2
cases 1, 3
case 4
46
Deleting a topmost element (7)
  • Auxiliary algorithm
  • To determine the leftmost element in the
    (nonempty) subtree whose topmost node is top
  • 1. Set curr to top.2. While curr has a left
    child, repeat 2.1. Set curr to currs left
    child.3. Terminate with currs element as
    answer.

47
Deleting a topmost element (8)
  • Implementation as a Java method (in class
    BSTNode)
  • public BSTNode deleteTopmost () if (this.left
    null) return this.right else if
    (this.right null) return this.left else
    // this node has two children this.element
    this.right.getLeftmost() this.right
    this.right.deleteLeftmost() return this

48
Deleting a topmost element (9)
  • Auxiliary method (in class BSTNode)
  • private Comparable getLeftmost () BSTNode
    curr this while (curr.left ! null) curr
    curr.left return curr.element

49
Deleting a given element (1)
  • BST deletion algorithm
  • To delete the element elem in a BST
  • 1. Set parent to null, and set curr to the BSTs
    root node.2. Repeat 2.1. If curr is
    null 2.1.1. Terminate. 2.2. Otherwise, if
    elem is equal to currs element 2.2.1. Delete
    the topmost element in the subtree whose
    topmost node is curr, and let del be a link
    to the resulting subtree. 2.2.2. Replace
    the link to curr by del. 2.2.3. Terminate. 2.3.
    Otherwise,

50
Deleting a given element (2)
  • BST deletion algorithm (continued)
  • 2.3. Otherwise, if elem is less than currs
    element 2.3.1. Set parent to curr, and set
    curr to currs left child. 2.4. Otherwise, if
    elem is greater than currs element 2.4.1. Set
    parent to curr, and set curr to currs right
    child.

51
Deleting a given element (3)
  • Animation

52
Deleting a given element (4)
  • Implementation as a Java method (in class BST)
  • public void delete (Comparable elem) int
    direction 0 BSTNode parent null, curr
    root for () if (curr null)
    return direction elem.compareTo(curr.element)
    if (direction 0) BSTNode del
    curr.deleteTopmost() if (curr root) root
    del else if (curr parent.left) parent
    .left del else parent.right
    del return

53
Deleting a given element (5)
  • Implementation (continued)
  • parent curr if (direction lt 0) curr
    parent.left else // direction gt 0 curr
    parent.right

54
BSTs in practice deletions
  • Whether a BST is well-balanced or ill-balanced
    depends on the order of insertions and deletions.
  • Deletion can make a well-balanced BST
    ill-balanced, or vice versa.

55
Example 2 successive deletions
  • Animation (deleting lion, fox, pig)

56
Binary tree traversal
  • Binary tree traversal Visit all nodes (elements)
    of the tree in some predetermined order.
  • During a binary tree traversal, we must visit the
    root node, traverse the left subtree, and
    traverse the right subtree. But in which order?
  • In-order traversal Traverse the left subtree,
    then visit the root node, then traverse the right
    subtree.
  • Pre-order traversal Visit the root node, then
    traverse the left subtree, then traverse the
    right subtree.
  • Post-order traversal Traverse the left subtree,
    then traverse the right subtree, then visit the
    root node.

57
Binary tree in-order traversal (1)
  • Schematic

58
Binary tree in-order traversal (2)
  • Illustration
  • In-order traversal of a BST visits the elements
    in ascending order.

59
Binary tree in-order traversal (3)
  • Binary tree in-order traversal algorithm
    (generic)
  • To traverse, in in-order, the subtree whose
    topmost node is top
  • 1. If top is not null 1.1. Traverse, in
    in-order, tops left subtree. 1.2. Visit
    top. 1.3. Traverse, in in-order, tops right
    subtree.2. Terminate.

60
Example 3 printing elements in order
  • Java method
  • public static void printInOrder (BSTNode top)
    // Print, in ascending order, all the elements
    in the BST subtree // whose topmost node is
    top. if (top ! null) printInOrder(top.left)
    System.out.println(top.element) printInOrder
    (top.right)

visit top
61
Binary tree pre-order traversal (1)
  • Schematic

62
Binary tree pre-order traversal (2)
  • Illustration

63
Binary tree pre-order traversal (3)
  • Binary tree pre-order traversal algorithm
    (generic)
  • To traverse, in pre-order, the subtree whose
    topmost node is top
  • 1. If top is not null 1.1. Visit
    top. 1.2. Traverse, in pre-order, tops left
    subtree. 1.3. Traverse, in pre-order, tops
    right subtree.2. Terminate.

64
Binary tree post-order traversal (1)
  • Schematic

65
Binary tree post-order traversal (2)
  • Illustration

66
Binary tree post-order traversal (3)
  • Binary tree post-order traversal algorithm
    (generic)
  • To traverse, in post-order, the subtree whose
    topmost node is top
  • 1. If top is not null 1.1. Traverse, in
    post-order, tops left subtree. 1.2. Traverse,
    in post-order, tops right subtree. 1.3. Visit
    top.2. Terminate.

67
Implementation of sets using BSTs (1)
  • Represent an (unbounded) set by a BST whose
    elements are the set members.

68
Implementation of sets using BSTs (2)
  • Note The BST representation of a set is not
    unique

69
Implementation of sets using BSTs (3)
  • Summary of algorithms and time complexities

Operation Algorithm Time complexity
contains BST search O(log n) bestO(n) worst
add BST insertion O(log n) bestO(n) worst
remove BST deletion O(log n) bestO(n) worst
Write a Comment
User Comments (0)
About PowerShow.com