1 / 61

Design and Analysis of Algorithms Binary search trees

Design and Analysis of Algorithms Binary search trees. Haidong Xue Summer 2012, at GSU. Operations on a binary search tree. SEARCH(S, k) MINIMUM(S) MAXIMUM(S) SUCCESSOR(S, x) PREDECESSOR(S, x) INSERT(S, x) DELETE(S, x). O(h). O(h). h is the height of the tree. O(h). O(h). O(h) .

larue
Download Presentation

Design and Analysis of Algorithms Binary search trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design and Analysis of Algorithms Binary search trees HaidongXue Summer 2012, at GSU

  2. Operations on a binary search tree • SEARCH(S, k) • MINIMUM(S) • MAXIMUM(S) • SUCCESSOR(S, x) • PREDECESSOR(S, x) • INSERT(S, x) • DELETE(S, x) O(h) O(h) h is the height of the tree O(h) O(h) O(h) O(h) O(h)

  3. What is a binary search tree? • A binary tree • Binary-search-tree property • For each node, all the nodes in its left sub tree is smaller than to or equal to this node; all the nodes in its right sub tree is larger than or equal to this node What the difference between “binary search tree” and a “max-heap”? Not a complete binary tree With a different tree property

  4. What is a binary search tree? 11 6 9 12 7 5 8 10 11 12 2 5 8 2 Yes Yes

  5. What is a binary search tree? 2 1 5 2 3 7 4 6 8 6 5 Yes Yes

  6. What is a binary search tree? 11 11 9 12 9 25 8 12 11 12 8 10 20 78 8 10 13 19 No No

  7. Elements in a binary search tree • Can we use an array to represent a binary search tree? • No • So some tree structure information has to be stored

  8. Elements in a binary search tree • binary search tree node { • Key • Satellite data • Left node (left) • Right node (right) • Parent node (p) }

  9. 11 NIL 9 12 p 11 right left 8 10 11 12 2 p p 9 12 right right left left p p p p 10 8 11 right 12 left right left right right left left NIL NIL NIL NIL NIL NIL NIL p 2 right left Operations are based on this structure NIL NIL

  10. Print all keys in sorted order • Preorder tree walk • Inorder tree walk • Postorder tree walk

  11. Print all keys in sorted order • Preorder-tree-walk (node x){ • If(x==NIL) return; • Access(x); • Preorder-tree-walk(x.left); • Preorder-tree-walk(x.right) }

  12. Print all keys in sorted order • Preorder-tree-walk ( );

  13. Print all keys in sorted order • Inorder-tree-walk (node x){ • If(x==NIL) return; • Inorder-tree-walk( x.left); • Access(x); • Inorder-tree-walk( x.right) }

  14. Print all keys in sorted order • Inorder-tree-walk ( );

  15. Print all keys in sorted order • Postorder-tree-walk (node x){ • If(x==NIL) return; • Postorder-tree-walk( x.left); • Postorder-tree-walk( x.right) • Access(x); }

  16. Print all keys in sorted order • Postorder-tree-walk ( );

  17. Print all keys in sorted order • Preorder tree walk • Inorder tree walk • Postorder tree walk

  18. Print all keys in sorted order 11 • Preorder tree walk • Inorder tree walk • Postorder tree walk 11 12 10 12 8 11 9 9 12 12 12 8 9 10 11 11 8 10 11 12 8 10 9 11 12 12 11 Sorted order from inorder tree walk!

  19. Print all keys in sorted order • Time complexity of inorder tree walk • Access each node once

  20. Searching in a binary search tree TREE-SEARCH • Input: root pointer (x), key (k) • Output: a element whose key is the same as the input key; NIL if no element has the input key • if(x==NIL or x.key==k) return x; • if(k<x.key) return TREE-SEARCH(x.left, k); • Return TREE-SEARCH(x.right, k);

  21. Searching in a binary search tree 11 TREE-SEARCH( , 11) 11 11 9 12 8 10 11 12 2

  22. Searching in a binary search tree 11 TREE-SEARCH( , 11) 11 9 12 8 10 11 11 12 2

  23. Searching in a binary search tree 11 TREE-SEARCH( , 8) 9 TREE-SEARCH( , 8) 11 8 TREE-SEARCH( , 8) 9 12 8 8 10 11 12 2

  24. Searching in a binary search tree 11 TREE-SEARCH( , 20) 11 12 TREE-SEARCH( , 20) 9 12 12 TREE-SEARCH( , 20) 8 10 11 12 TREE-SEARCH( NIL , 20) NIL 2 NIL Means there is no such a node in the tree

  25. Searching in a binary search tree 30 TREE-SEARCH( , 20) 30 11 9 12 Illegal, but never happen if start from the root 8 10 11 12 2

  26. Searching in a binary search tree What’s the worst case? Worst successful search 11 11 TREE-SEARCH( , 2) O(h) 9 12 Worst unsuccessful search 11 TREE-SEARCH( , 1) O(h) 8 10 11 12 2

  27. Searching in a binary search tree Iterative code could be more efficient than recursive code TREE-SEARCH (x, k) if(x==NIL or x.key==k) return x; if(k<x.key) return TREE-SEARCH(x.left, k); Return TREE-SEARCH(x.right, k); • TREE-SEARCH (x, k) • current=x; • While (current!=NIL and current.key!=k){ • if(x.key<k) current=x.left; • else current=x.right; • } • return current;

  28. Searching in a binary search tree 11 TREE-SEARCH( , 8) • TREE-SEARCH (x, k) • current=x; • while (current!=NIL and current.key!=k){ • if(x.key<k) current=x.left; • else current=x.right; • } • return current; 11 9 12 8 8 10 11 12 2

  29. Searching in a binary search tree 11 TREE-SEARCH( , 20) 11 NIL 9 12 8 10 11 12 NIL 2

  30. Minimum and maximum 11 9 12 8 10 11 12 2 As a human, can you tell where is the minima and maxima?

  31. Minimum and maximum The minima of the tree rooted at x is: the minima of x.leftif x.left is not NIL; x if x.left is NIL 11 9 12 TREE-MINIMUM( x ) //the recursive one 1. if (x.left==NIL) return x; 2. return TREE-MINIMUM(x.left); 0. if(x==NIL) return NIL; 8 10 11 12 2 It has some cost, so if x is guaranteed not NIL we can remove it

  32. Minimum and maximum 11 TREE-MINIMUM( ) //recursive 11 9 12 8 10 11 12 2 2

  33. Minimum and maximum The minima of the tree rooted at x is: the leftmost node 11 9 12 • TREE-MINIMUM( x ) //the iterative one • current = x; • while(current.left!=NIL) • current = current.left; • return current; 0. if(x==NIL) return NIL; 8 10 11 12 2

  34. Minimum and maximum 11 TREE-MINIMUM( ) //iterative 11 9 12 8 10 11 12 2 2

  35. Minimum and maximum TREE-MAXIMUM( x ) //the recursive one 0. if(x==NIL) return NIL; 1. if (x.right==NIL) return x; 2. return TREE-MAXIMUM( x.right); TREE-MINIMUM( x ) //the recursive one 0. if(x==NIL) return NIL; 1. if (x.left==NIL) return x; 2. return TREE-MINIMUM(x.left); • TREE-MINIMUM( x ) //the iterative one • 0. if(x==NIL) return NIL; • current = x; • while(current.left!=NIL) • current = current.left; • return current; • TREE-MAXIMUM( x ) //the iterative one • 0. if(x==NIL) return NIL; • current = x; • while(current.right!=NIL) • current = current.right; • return current; O(h) O(h) Time complexity?

  36. Successor and predecessor • What is a successor of x? • What is a successor of x if there is another node has the same key in the binary search tree? 11 9 12 12 12 8 9 10 11 11 8 10 11 12

  37. Successor and predecessor 15 TREE-SUCCESSOR( ) 15 The minimum of the right sub tree 6 18 13 TREE-SUCCESSOR( ) 3 7 17 20 There is no right sub tree 4 2 13 The lowest ancestor whose left child is also an ancestor of or 13 13 9

  38. Successor and predecessor TREE-SUCCESSOR( x ) // When x has a right sub tree if(x.right!=NIl) return TREE-MINIMUM(x); // When x does not have a right sub tree 2. current = x 3. currentParent = x.p 4. while( currentParent!=NIL and currentParent.left!=current ){ current = currentParent; currentParrent = currenParent.p; } 5. return currentParent;

  39. Successor and predecessor 15 15 TREE-SUCCESSOR( ) TREE-MINIMUM 6 18 3 7 17 17 20 4 2 13 9 2 3 4 6 7 15 18 20 9 13 17

  40. Successor and predecessor 9 15 TREE-SUCCESSOR( ) Has no right sub tree 6 18 current == currentParent.left is true 17 20 3 7 4 13 2 13 currentParent 9 current 2 3 4 6 7 15 18 20 9 13 17

  41. Successor and predecessor 13 15 15 TREE-SUCCESSOR( ) No right subtree 6 18 current == currentParent.left is false currentParent == NIL is false 17 20 3 7 current == currentParent.left is false currentParent== NIL is false currentParent 4 13 2 current == currentParent.left is true current 9 2 3 4 6 7 15 18 20 9 13 17

  42. Successor and predecessor 20 15 TREE-SUCCESSOR( ) No right subtree 6 18 current == currentParent.left is false currentParent== NIL is false currentParent 17 20 3 current == currentParent.left is false currentParent== NIL is false 7 current currentParent== NIL is true 4 13 2 • NIL 9 2 3 4 6 7 15 18 20 9 13 17

  43. TREE-SUCCESSOR( x ) // When x has a right sub tree if(x.right!=NIl) return TREE-MINIMUM(x); // When x does not have a right sub tree 2. current = x 3. currentParent = x.p 4. while( !(currentParent==NIL or currentParent.left==current) ){ current = currentParent; currentParrent = currenParent.p; } 5. return currentParent; Time complexity? TREE-PREDECESSOR( x ) // When x has a left sub tree if(x.left!=NIl) return TREE-MAXIMUM(x); // When x does not have a left sub tree 2. current = x 3. currentParent = x.p 4. while( !(currentParent==NIL or currentParent.right==current) ){ current = currentParent; currentParrent = currenParent.p; } 5. return currentParent; O(h)

  44. Insertion and deletion 11 9 19 8 10 18 22 2 As a human, how to insert a element to a binary search tree?

  45. Insertion and deletion TREE-INSERT( T, z ) if(T.root==NIL){ T.root = z; z.p = NIL; } else { INSERT(t.root, z); } • INSERT(x, z) • if(z.key<x.key) • if(z.left==NIL){ • z.p=x; • x.left=z; • } • else • INSERT(x.left, z); • else • if(z.right==NIL){ • z.p=x; • x.right=z; • } • else • INSERT(x.right, z);

  46. Insertion and deletion 3 3 TREE-INSERT( T, ) //recursive 11 Not NIL 9 19 Not NIL 8 10 18 22 Not NIL 2 NIL

  47. Insertion and deletion TREE-INSERT( T, z ) // iterative posParent = NIL; pos = T.root; // try to find a position, start from T.root while(pos!=NIL){ posParent = pos; if(z.key < pos.key) pos = pos.left; else pos = pos.right; } z.p = posParent; if(posParent==NIL); // T is empty T.root = z; else if(z.key<posParent.key) posParent.left = z; else posParent.right = z; Find a position Modify z Modify posParent

  48. Insertion and deletion 3 3 TREE-INSERT( T, ) //iterative 11 NIL posParent 9 19 pos ….. while(pos!=NIL){ posParent = pos; if(z.key < pos.key) pos = pos.left; else pos = pos.right; } … 8 10 18 22 2 NIL

  49. Insertion and deletion The element has less than 2 children 11 The element has two children and its successor is the right child 19 9 18 8 10 22 The element has two children and its successor is not the right child 2 As a human, how to delete a element from a binary search tree?

  50. Insertion and deletion The element has less than one child 11 22 TREE-DELETE( ) // no child 19 9 8 TREE-DELETE( ) // no child 8 10 18 22 2 Replace a tree with another tree TRANSPLANT( T, u, v )

More Related