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) h is the height of the tree O(h)
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
What is a binary search tree? Yes
What is a binary search tree? Yes
What is a binary search tree? No
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
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) }
p left right p left right p left right p left right p left right p left right p left right p left right NIL Operations are based on this structure
Print all keys in sorted order Preorder tree walk Inorder tree walk Postorder tree walk
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) }
Print all keys in sorted order Preorder-tree-walk ( );
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) }
Print all keys in sorted order Inorder-tree-walk ( );
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); }
Print all keys in sorted order Postorder-tree-walk ( );
Print all keys in sorted order Preorder tree walk Inorder tree walk Postorder tree walk
Print all keys in sorted order Preorder tree walk Inorder tree walk Postorder tree walk Sorted order from inorder tree walk!
Print all keys in sorted order
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);
11 Searching in a binary search tree TREE-SEARCH(, 11) 11
Searching in a binary search tree TREE-SEARCH(, 11) 11
8 Searching in a binary search tree TREE-SEARCH(, 8) 11 TREE-SEARCH(, 8) 9 8
Searching in a binary search tree 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
Searching in a binary search tree TREE-SEARCH(, 20) 30 Illegal, but never happen if start from the root
Searching in a binary search tree TREE-SEARCH(, 2) 11 What’s the worst case? Worst successful search Worst unsuccessful search TREE-SEARCH(, 1) 11 O(h)
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;
8 Searching in a binary search tree 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;
Searching in a binary search tree TREE-SEARCH(, 20) 11 NIL
Minimum and maximum As a human, can you tell where is the minima and maxima?
Minimum and maximum 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
Minimum and maximum TREE-MINIMUM( ) //recursive 11 2
Minimum and maximum 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;
2 Minimum and maximum TREE-MINIMUM( ) //iterative 11
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?
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?
Successor and predecessor 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
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;
17 Successor and predecessor TREE-SUCCESSOR( ) TREE-MINIMUM
Successor and predecessor TREE-SUCCESSOR( ) current currentParent current == currentParent.left is true Has no right sub tree 13
Successor and predecessor TREE-SUCCESSOR( ) 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
Successor and predecessor TREE-SUCCESSOR( ) 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
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)
Insertion and deletion As a human, how to insert a element to a binary search tree?
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);
Insertion and deletion TREE-INSERT( T, ) //recursive 3 Not NIL NIL 3
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
NIL Insertion and deletion 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
Insertion and deletion 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
Insertion and deletion 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
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;
Insertion and deletion 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
Insertion and deletion 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
Insertion and deletion 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
Insertion and deletion 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
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.}
Insertion and deletion TREE-DELETE( T, ) Only one child
Insertion and deletion TREE-DELETE( T, ) Find the successor Replace with the successor tree 6 Assign.left to the successor 6 7
Insertion and deletion TREE-DELETE( T, ) Find the successor Replace the successor with its right tree Assign.left to the successor Replace with the successor 15 Assign.right to the successor 15
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)
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