10 Binary Tree Data Structures

Document Sample
10 Binary Tree Data Structures Powered By Docstoc
					            10
Binary Tree Data Structures
• Binary trees and binary search trees.
• Searching.
• Insertion.
• Deletion.
• Traversal.
• Implementation of sets using BSTs.
                                          10-1
                    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.

                                   A                   root node

                         B                    C
       header
                              D           E        F

                  leaf node   leaf node                  G
                                       leaf node                     10-2
                   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.




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

                                A
A’s left                                                       A’s right
                    B                          C
subtree                                                        subtree
                        D              E           F

                                                       G
 B’s left subtree   B’s right
    is empty        subtree     C’s left subtree       C’s right subtree
                                                                   10-4
        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.




                                                                   10-5
            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.




                                                           10-6
               Node and tree depths (2)
• Illustrations:
    (a)                    A               depth 0

                   B               C       depth 1
 balanced
                       D       E       F   depth 2


    (b)                            J       depth 0

                       K               L   depth 1
ill-balanced
                   M       N               depth 2

                               P           depth 3
                                                     10-7
             Node and tree depths (3)
• Illustrations (continued):
   (c)              S                          depth 0

                        T                      depth 1

                               U               depth 2
 very ill-
 balanced                          V           depth 3

                                       W       depth 4

                                           X   depth 5




                                                         10-8
              Balanced binary trees
• A binary tree of depth d is balanced if all nodes at depths
  0, 1, …, d–2 have two children.
       Nodes at depth d–1 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 2d+1 – 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
  d+1 nodes. Conversely:
   Max. depth of ill-balanced binary tree of size n = n–1    10-9
              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.




                                                                         10-10
             Binary search trees (2)
• Illustrations:
   (a)                         lion

                         fox                 rat

            cat                       pig          tiger

                   dog

   (b)                   cat

                                                   pig

                                      fox                        tiger

                               dog          lion           rat           10-11
              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.




                                                                         10-12
           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)
  }

                                                       10-13
             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)
  }



                                                           10-14
                       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.




                                                             10-15
                     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 BST’s root.
  2. Repeat:
     2.1. If curr is null:
           2.1.1. Terminate with answer none.
     2.2. Otherwise, if target is equal to curr’s element:
           2.2.1. Terminate with answer curr.
     2.3. Otherwise, if target is less than curr’s element:
           2.3.1. Set curr to curr’s left child.
     2.4. Otherwise, if target is greater than curr’s element:
           2.4.1. Set curr to curr’s right child.

                                                                    10-16
                            BST search (3)
• Animation (successful search):
To find which if any node of a BST contains an element equal to target:
1. Set curr to the BST’s root.
2. Repeat:
   2.1. If curr is null, terminate with answer none.
   2.2. Otherwise, if target is equal to curr’s element, terminate with answer curr.
   2.3. Otherwise, if target is less than curr’s element, set curr to curr’s left child.
   2.4. Otherwise, if target is greater than curr’s element, set curr to curr’s right
        child.

     root                                  lion                       target     pig

                                     fox                rat
     curr
                         cat                      pig         tiger

                                                                                    10-17
                               dog
                            BST search (4)
• Animation (unsuccessful search):
To find which if any node of a BST contains an element equal to target:
1. Set curr to the BST’s root.
2. Repeat:
   2.1. If curr is null, terminate with answer none.
   2.2. Otherwise, if target is equal to curr’s element, terminate with answer curr.
   2.3. Otherwise, if target is less than curr’s element, set curr to curr’s left child.
   2.4. Otherwise, if target is greater than curr’s element, set curr to curr’s right
        child.

     root                                  lion                       target    goat

                                     fox                rat
     curr
                         cat                      pig         tiger

                                                                                    10-18
                               dog
                   BST search (5)
• Analysis (counting comparisons):
   Let the BST’s size be n.
   If the BST has depth d, the number of comparisons is at
   most d+1.
• 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 n–1:
   Max. no. of comparisons = n
   Worst-case time complexity is O(n).
                                                             10-19
                  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 < 0) curr = curr.left;
       else curr = curr.right;
    }
  }

                                                    10-20
                 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.




                                                               10-21
                   BST insertion (2)
• BST insertion algorithm:
  To insert the element elem into a BST:
  1. Set parent to null, and set curr to the BST’s root.
  2. Repeat:
     2.1. If curr is null:
          2.1.1. Replace the null link from which curr was taken
                  (either the BST’s root or parent’s left child or
                  parent’s 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 curr’s element:
          2.2.1. Terminate.
     2.3. Otherwise, …

                                                                      10-22
                  BST insertion (3)
• BST insertion algorithm (continued):
     2.3. Otherwise, if elem is less than curr’s element:
          2.3.1. Set parent to curr, and set curr to curr’s left child.
     2.4. Otherwise, if elem is greater than curr’s element:
          2.4.1. Set parent to curr, and set curr to curr’s right child.




                                                                      10-23
                      BST insertion (4)
• Animation (empty BST):
To insert the element elem into a BST:
1. Set parent to null, and set curr to the BST’s root.
2. Repeat:
   2.1. If curr is null, replace the null link from which curr was taken by a
        link to a newly-created leaf node with element elem, and terminate.
   2.2. Otherwise, if elem is equal to curr’s element, terminate.
   2.3. Otherwise, if elem is less than curr’s element, set parent to curr
        and set curr to curr’s left child.
   2.4. Otherwise, if elem is greater than curr’s element, set parent to curr
        and set curr to curr’s right child.

    root                   goat                  elem    goat
  parent
    curr                                                                 10-24
                      BST insertion (5)
• Animation (nonempty BST):
To insert the element elem into a BST:
1. Set parent to null, and set curr to the BST’s root.
2. Repeat:
   2.1. If curr is null, replace the null link from which curr was taken by a
        link to a newly-created leaf node with element elem, and terminate.
   2.2. Otherwise, if elem is equal to curr’s element, terminate.
   2.3. Otherwise, if elem is less than curr’s element, set parent to curr
        and set curr to curr’s left child.
   2.4. Otherwise, if elem is greater than curr’s element, set parent to curr
        and set curr to curr’s right child.
    root                               lion                   elem    goat

  parent                         fox             rat
    curr
                                                                         10-25
                           cat         goat
                 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).


                                                       10-26
                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 < 0)
             parent.left = ins;
          else parent.right = ins;
          return;
       }

                                                    10-27
                BST insertion (8)
• Implementation (continued):
          direction = elem.compareTo(curr.element);
          if (direction == 0) return;
          parent = curr;
          if (direction < 0) curr = curr.left;
          else curr = curr.right;
      }
  }




                                                 10-28
      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.




                                                           10-29
  Example 1: successive insertions (1)
• Animation (inserting ‘lion’, ‘fox’, ‘rat’, ‘cat’, ‘pig’, ‘dog’,
  ‘tiger’):

                    ‘fox’:
                    ‘tiger’:
                    ‘dog’:
                    ‘pig’:
                    ‘cat’:
                    ‘rat’:
    After inserting ‘lion’:
    Initially:

                                     lion

                               fox                rat

                 cat                        pig         tiger

                       dog



                                                                10-30
                       Example 1 (2)
• Animation (inserting ‘cat’, ‘dog’, ‘fox’, ‘lion’, ‘pig’, ‘rat’):
                   ‘dog’:
                   ‘rat’:
                   ‘pig’:
                   ‘lion’:
                   ‘fox’:
   After inserting ‘cat’:
   Initially:
                      cat

                             dog

                                   fox

                                         lion

                                                pig

                                                      rat

                                                              10-31
      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:
                                                               0

                                                               2
                                                               4

                                                               6




                                                           10-32
      BSTs in practice: insertions (3)
• Second trial with n = 35:
                                             0

                                             2
                                             4

                                             6

                                             8




                                         10-33
                    BST deletion
• Deleting a subtree’s leftmost element.
• Deleting a subtree’s topmost element.
• Deleting an arbitrary given element in a BST.




                                                  10-34
       Deleting a leftmost element (1)
•   Problem: Delete the leftmost element in a subtree.
•   Two cases to consider:
     1) The subtree’s topmost node has no left child.
     2) The subtree’s topmost node has a left child.

•   Note: By definition, the leftmost node has no left child.




                                                            10-35
       Deleting a leftmost element (2)
• Case 1 (topmost node has no left child):
  Replace the subtree by its own right subtree. E.g.:
   Before: top             lion

                                        rat
              leftmost
                node
                                  pig         tiger


   After:   top            lion
            top’
                                        rat

                                  pig         tiger
                                                        10-36
      Deleting a leftmost element (3)
• Case 2 (topmost node has a left child):
  Link the leftmost node’s parent to the leftmost node’s right
  child. E.g.:
  Before: top                          lion

                      cat                           rat
      leftmost
        node                  fox             pig         tiger

  After:   top                         lion
           top’
                      cat                           rat

      garbage                 fox             pig         tiger
                                                                  10-37
      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:                                        case 1
     1.1. Terminate with top’s right child as answer.
  2. If top has a left child:                                         case 2
     2.1. Set parent to top, and set curr to top’s left child.
     2.2. While node curr has a left child, repeat:
           2.2.1. Set parent to curr, and set curr to curr’s left child.
     2.3. Set parent’s left child to curr’s right child.
     2.4. Terminate with top as answer.



                                                                       10-38
      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;
       }
       parent.left = curr.right;
       return this;
    }
  }

                                                        10-39
       Deleting a topmost element (1)
•   Problem: Delete the topmost element in a subtree.
•   Four cases to consider:
     1) The topmost node has no children.
     2) The topmost node has a right child but no left child.
     3) The topmost node has a left child but no right child.
     4) The topmost node has two children.




                                                                10-40
      Deleting a topmost element (2)
• Case 1 (topmost node has no children):
  Replace the subtree by an empty subtree. E.g.:
   Before: top            lion

   After:   top           lion
            top’




                                                   10-41
      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.:
   Before: top             lion

                                        rat

                                  pig         tiger


   After:   top            lion
            top’
                                        rat

                                  pig         tiger
                                                           10-42
      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.:
   Before: top                           lion

                            fox

                      cat         goat


   After:   top                          lion
            top’
                            fox

                      cat         goat
                                                           10-43
      Deleting a topmost element (5)
• Case 4 (topmost node has two children):
  Copy the right subtree’s leftmost element into the topmost
  node, then delete the right subtree’s leftmost element. E.g.:
   Before: top                      lion

                              fox                rat

                        cat                pig         tiger

   After:   top                     pig
            top’
                              fox                rat           garbage

                        cat                pig         tiger
                                                                         10-44
      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:                                   cases 1, 2
     1.1. Terminate with top’s right child as answer.
  2. If top has no right child:                                  cases 1, 3
     2.1. Terminate with top’s left child as answer.
  3. If top has two children:                                       case 4
     3.1. Set top’s element to the leftmost element in top’s right
           subtree.
     3.2. Delete the leftmost element in top’s right subtree.
     3.3. Terminate with top as answer.


                                                                     10-45
      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 curr’s left child.
  3. Terminate with curr’s element as answer.




                                                                  10-46
      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;
    }
  }



                                                        10-47
      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;
  }




                                         10-48
         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 BST’s root node.
  2. Repeat:
     2.1. If curr is null:
          2.1.1. Terminate.
     2.2. Otherwise, if elem is equal to curr’s 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, …

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




                                                                      10-50
           Deleting a given element (3)
• Animation:
To delete the element elem in a BST:
1. Set parent to null, and set curr to the BST’s root node.
2. Repeat:
   2.1. If curr is null, terminate.
   2.2. Otherwise, if elem is equal to curr’s element:
        …
        2.2.1. Delete the topmost element in the subtree whose topmost
   2.3. Otherwise, if elem is less than curr’s element, set parentsubtree.
               node is curr, and let del be a link to the resulting to curr,
        and setReplace curr’s left child.by del.
        2.2.2. curr to the link to curr
   2.4. Otherwise, if elem is greater than curr’s element, set parent to curr,
        2.2.3. Terminate.
        Otherwise, to
   2.3. and set curr… curr’s right child.
   root                               lion                       elem   fox
 parent
                          fox                      rat
   curr
    del                         dog          pig         tiger                10-51
        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;
       }                                       10-52
          Deleting a given element (5)
• Implementation (continued):
          parent = curr;
          if (direction < 0)
             curr = parent.left;
          else // direction > 0
             curr = parent.right;
      }
  }




                                         10-53
         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.




                                                       10-54
      Example 2: successive deletions
• Animation (deleting ‘lion, ‘fox’, ‘pig’):

                     ‘fox’:
                     ‘pig’:
      After deleting ‘lion’:
      Initially:

                                     pig
                                      rat
                                     lion

                               fox                rat

                   cat                      pig         tiger

                         dog           garbage
                                      garbage
                                        garbage




                                                                10-55
              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.      10-56
   Binary tree in-order traversal (1)
• Schematic:




                                        10-57
    Binary tree in-order traversal (2)
• Illustration:

                           lion

                     fox                rat

         cat                      pig         tiger

               dog

• In-order traversal of a BST visits the elements in ascending
  order.


                                                          10-58
    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, top’s left subtree.
     1.2. Visit top.
     1.3. Traverse, in in-order, top’s right subtree.
  2. Terminate.




                                                                     10-59
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);                         visit top
        printInOrder(top.right);
     }
  }




                                                                    10-60
  Binary tree pre-order traversal (1)
• Schematic:




                                    10-61
   Binary tree pre-order traversal (2)
• Illustration:

                           lion

                     fox                rat

         cat                      pig         tiger

               dog




                                                      10-62
   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, top’s left subtree.
     1.3. Traverse, in pre-order, top’s right subtree.
  2. Terminate.




                                                                      10-63
  Binary tree post-order traversal (1)
• Schematic:




                                     10-64
  Binary tree post-order traversal (2)
• Illustration:

                            lion

                      fox                rat

          cat                      pig         tiger

                dog




                                                       10-65
  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, top’s left subtree.
     1.2. Traverse, in post-order, top’s right subtree.
     1.3. Visit top.
  2. Terminate.




                                                                       10-66
Implementation of sets using BSTs (1)
• Represent an (unbounded) set by a BST whose elements
  are the set members.
  Invariant:




  Empty set:

  Illustration:          MX
                                         represents the set
                                         {CA, MX, US}
                    CA        US

                                                          10-67
Implementation of sets using BSTs (2)
• Note: The BST representation of a set is not unique:
                    FR
                                           represents the set {BE,
               DE             LU           DE, FR, IT, LU, NL}

          BE             IT        NL

                         IT
                                           represents the set {BE,
          BE                  LU           DE, FR, IT, LU, NL}

               DE                  NL

                    FR
                                                             10-68
Implementation of sets using BSTs (3)
• Summary of algorithms and time complexities:
   Operation   Algorithm       Time complexity
   contains BST search         O(log n)    best
                               O(n)       worst
   add         BST insertion   O(log n)    best
                               O(n)       worst
   remove      BST deletion    O(log n)    best
                               O(n)       worst




                                                  10-69