Download presentation
Presentation is loading. Please wait.
Published byJemima Sharp Modified over 9 years ago
1
Design and Analysis of Algorithms Binary search trees Haidong Xue 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) h is the height of the tree 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 912 8101112 2 6 5 7 258 Yes
5
What is a binary search tree? 2 5 5 68 7 1 2 6 4 3 Yes
6
What is a binary search tree? 11 912 8 1112 10 11 925 8102078 8 1319 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 { –K–Key –S–Satellite data –L–Left node (left) –R–Right node (right) –P–Parent node (p) }
9
11 912 8 10 11 12 2 p left right p left right p left right p left right p left right p left right p left right p left right 11 912 8101112 2 NIL Operations are based on this structure
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 Preorder tree walk Inorder tree walk Postorder tree walk 11 9 12 8101112 119 8 1012 11 12 891011 12 8109 1112 11 Sorted order from inorder tree walk!
19
Print all keys in sorted order
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 1.if(x==NIL or x.key==k) return x; 2.if(k<x.key) return TREE-SEARCH(x.left, k); 3.Return TREE-SEARCH(x.right, k);
21
11 Searching in a binary search tree 11 912 8101112 2 TREE-SEARCH(, 11) 11
22
Searching in a binary search tree 11 912 8101112 2 TREE-SEARCH(, 11) 11
23
8 Searching in a binary search tree 11 912 8101112 2 TREE-SEARCH(, 8) 11 TREE-SEARCH(, 8) 9 8
24
Searching in a binary search tree 11 912 8101112 2 TREE-SEARCH(, 20) 11 TREE-SEARCH(, 20) 12 TREE-SEARCH(, 20) 12 NIL TREE-SEARCH( NIL, 20) NIL Means there is no such a node in the tree
25
Searching in a binary search tree 11 912 8101112 2 TREE-SEARCH(, 20) 30 Illegal, but never happen if start from the root
26
Searching in a binary search tree 11 912 8101112 2 TREE-SEARCH(, 2) 11 What’s the worst case? Worst successful search Worst unsuccessful search TREE-SEARCH(, 1) 11 O(h)
27
Searching in a binary search tree Iterative code could be more efficient than recursive code TREE-SEARCH (x, k) 1.if(x==NIL or x.key==k) return x; 2.if(k<x.key) return TREE-SEARCH(x.left, k); 3.Return TREE-SEARCH(x.right, k); TREE-SEARCH (x, k) 1.current=x; 2.While (current!=NIL and current.key!=k){ if(x.key<k) current=x.left; else current=x.right; } 3.return current;
28
8 Searching in a binary search tree 11 912 8101112 2 TREE-SEARCH(, 8) 11 TREE-SEARCH (x, k) 1.current=x; 2.while (current!=NIL and current.key!=k){ if(x.key<k) current=x.left; else current=x.right; } 3.return current;
29
Searching in a binary search tree 11 912 8101112 2 TREE-SEARCH(, 20) 11 NIL
30
Minimum and maximum 11 912 8101112 2 As a human, can you tell where is the minima and maxima?
31
Minimum and maximum 11 912 8101112 2 The minima of the tree rooted at x is: the minima of x.left if x.left is not NIL; x if x.left is NIL 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; It has some cost, so if x is guaranteed not NIL we can remove it
32
Minimum and maximum 11 912 8101112 2 TREE-MINIMUM( ) //recursive 11 2
33
Minimum and maximum 11 912 8101112 2 The minima of the tree rooted at x is: the leftmost node TREE-MINIMUM( x ) //the iterative one 1.current = x; 2.while(current.left!=NIL) current = current.left; 3.return current; 0. if(x==NIL) return NIL;
34
2 Minimum and maximum 11 912 8101112 2 TREE-MINIMUM( ) //iterative 11
35
Minimum and maximum TREE-MINIMUM( x ) //the iterative one 0. if(x==NIL) return NIL; 1.current = x; 2.while(current.left!=NIL) current = current.left; 3.return current; 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-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-MAXIMUM( x ) //the iterative one 0. if(x==NIL) return NIL; 1.current = x; 2.while(current.right!=NIL) current = current.right; 3.return current; 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 8101112 891011 12
37
Successor and predecessor 15 618 371720 2 4 13 9 TREE-SUCCESSOR( ) 15 The minimum of the right sub tree TREE-SUCCESSOR( ) 13 There is no right sub tree The lowest ancestor whose left child is also an ancestor of or 13
38
Successor and predecessor TREE-SUCCESSOR( x ) // When x has a right sub tree 1.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
17 Successor and predecessor 15 618 371720 2 4 13 9 TREE-SUCCESSOR( ) 15 23 46 7 913 15 17 1820 TREE-MINIMUM
40
Successor and predecessor 15 618 3 7 1720 2 413 9 TREE-SUCCESSOR( ) 9 23 46 7 9 13 15 17 1820 current currentParent current == currentParent.left is true Has no right sub tree 13
41
Successor and predecessor 15 618 3 7 1720 2 413 9 TREE-SUCCESSOR( ) 13 23 46 7 9 15 17 1820 current currentParent current == currentParent.left is false currentParent == NIL is false No right subtree current == currentParent.left is false currentParent == NIL is false current == currentParent.left is true 15
42
Successor and predecessor 15 618 3 7 1720 2 413 9 TREE-SUCCESSOR( ) 20 23 46 7 913 15 17 1820 current currentParent current == currentParent.left is false currentParent == NIL is false No right subtree current == currentParent.left is false currentParent == NIL is false currentParent == NIL is true NIL
43
TREE-SUCCESSOR( x ) // When x has a right sub tree 1.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; TREE-PREDECESSOR( x ) // When x has a left sub tree 1.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; Time complexity? O(h)
44
Insertion and deletion 11 919 8101822 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 11 9 19 8101822 2 TREE-INSERT( T, ) //recursive 3 Not NIL NIL 3
47
Insertion and deletion TREE-INSERT( T, z ) // iterative 1.posParent = NIL; 2.pos = T.root; // try to find a position, start from T.root 3.while(pos!=NIL){ 4. posParent = pos; 5. if(z.key < pos.key) 6. pos = pos.left; 7. else 8. pos = pos.right; 9.} 10.z.p = posParent; 11.if(posParent==NIL); // T is empty 12. T.root = z; 13.else if(z.key<posParent.key) 14. posParent.left = z; 15.else 16. posParent.right = z; Find a position Modify z Modify posParent
48
NIL Insertion and deletion 11 9 19 8101822 2 TREE-INSERT( T, ) //iterative 3 pos posParent ….. 3.while(pos!=NIL){ 4. posParent = pos; 5. if(z.key < pos.key) 6. pos = pos.left; 7. else 8. pos = pos.right; 9.} … 3 NIL
49
Insertion and deletion 11 9 19 810 18 22 2 As a human, how to delete a element from a binary search tree? The element has less than 2 children The element has two children and its successor is the right child The element has two children and its successor is not the right child
50
Insertion and deletion 11 9 19 8101822 2 The element has less than one child TREE-DELETE( ) 22 // no child TREE-DELETE( ) 8 // no child TRANSPLANT( T, u, v ) Replace a tree with another tree
51
Insertion and deletion TRANSPLANT( T, u, v ) //in T, replace u tree with v tree //modify v 1. if(v!=NIL) v.p = u.p; //modify u’s parent 2. if( u.p==NIL) 3. T.root = v; 4. else if (u == u.p.left) 5. u.p.left = v; 6.else 7. u.p.right=v;
52
Insertion and deletion 11 9 19 8101822 2 TRANSPLANT( T, u, v ) //modify v 1. if(v!=NIL) v.p = u.p; //modify u’s parent 2. if( u.p==NIL) 3. T.root = v; 4. else if (u == u.p.left) 5. u.p.left = v; 6.else 7. u.p.right=v; TRANSPLANT( T,, ) 82 p left right p left right
53
Insertion and deletion 11 9 19 8101822 2 TRANSPLANT( T, u, v ) //modify v 1. if(v!=NIL) v.p = u.p; //modify u’s parent 2. if( u.p==NIL) 3. T.root = v; 4. else if (u == u.p.left) 5. u.p.left = v; 6.else 7. u.p.right=v; TRANSPLANT( T,,.right) 222 p left right NIL
54
Insertion and deletion 11 9 19 810 1822 2 TRANSPLANT( T, u, v ) //modify v 1. if(v!=NIL) v.p = u.p; //modify u’s parent 2. if( u.p==NIL) 3. T.root = v; 4. else if (u == u.p.left) 5. u.p.left = v; 6.else 7. u.p.right=v; TRANSPLANT( T,, ) 18 9 p left right p left right
55
Insertion and deletion 11 19 1822 9 810 2 TRANSPLANT( T, u, v ) //modify v 1. if(v!=NIL) v.p = u.p; //modify u’s parent 2. if( u.p==NIL) 3. T.root = v; 4. else if (u == u.p.left) 5. u.p.left = v; 6.else 7. u.p.right=v; TRANSPLANT( T,, ) 18 9
56
TREE-DELETE( T, z) // When z has less than two children 1. if(z.left==NIL) TRANSPLANT(T, z, z.right) 2. else if (z.right==NIL) TRANSPLANT(T, z, z.left) // When z has two children 3. else{ //Get the successor of z 4. y= TREE-MINIMUM(z.right); // it is TREE-SUCCESSOR(z) // if the successor is not z’s right child 5. if(z.right != y){ // upgrade the succesor’s right 6. TRANSPLANT(T, y, y.right); // assign z’right to the successor 7. y.right = z.right; 8. y.right.p = y; 9. } // replace z with the successor 8. TRANSPLANT(T, z, y); // assign z’left to the successor 9. y.left = z.left; 10. y.left.p = y.left; 11.}
57
Insertion and deletion TREE-DELETE( T, ) 15 6 18 371720 2 4 13 9 7 Only one child
58
Insertion and deletion TREE-DELETE( T, ) 15 6 18 31720 2 4 7 13 9 6 Find the successor Replace with the successor tree 6 Assign.left to the successor 6 7
59
Insertion and deletion TREE-DELETE( T, ) 15 6 19 3 71720 2 413 9 15 Find the successor Replace the successor with its right tree Assign.left to the successor 15 17 18 Replace with the successor 15 Assign.right to the successor 15
60
Insertion and deletion Time complexity TREE-INSERT(T, x) TREE-DELETE(T, x) Similar to TREE-SEARCH, O(h) Because of TREE-SUCCESSOR, O(h)
61
How to build a binary search tree? By insertion When it is done it randomly, the expected height is O(lgn) What is the worst case? – There is only 1 leaf How to avoid the worst case? – Randomly insert – Variations of binary search tree like RBT
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.