Binary Search Trees binary because branches either to
Binary Search Trees (binary because branches either to left or to right) Operations: • search • min • max • predecessor • successor. Costs? Time O(h) with h height of the tree (more on later). Data structures attributes associated with each node x: • x. left (left child) • x. right (right child) • x. p (parent) • x. key • Root of entire tree pointed to by T. root CSC 317 1
Binary Search Trees (binary because branches either to left or to right) Example of storing keys in a binary search tree: Pattern anyone? CSC 317 2
Binary Search Trees (binary because branches either to left or to right) Example of storing keys in a binary search tree: Main property of binary search tree: Left side: key smaller equal to parent Right side: key larger equal to parent CSC 317 3
Binary Search Trees (binary because branches either to left or to right) Example of storing keys in a binary search tree: Given node with key k Right: all Keys > k Left: all keys < k Binary search tree property • You can see this in tree example above node by node going from parent to child, and it is also true for the whole subtree to the left and to the right. • Is this tree unique for the given set of keys? Answer: No! The same set of keys have many possible trees. CSC 317 4
Binary Search Trees (binary because branches either to left or to right) • You can see this in tree example above node by node going from parent to child, and it is also true for the whole subtree to the left and to the right. • Is this tree unique for the given set of keys? Answer: No! The same set of keys have many possible trees. CSC 317 5
Question: How can we traverse the tree and print out keys in ascending order (i. e. like this: 2 5 5 6 7 8)? CSC 317 6
Answer: Recursive algorithm CSC 317 7
What about this one? CSC 317 8
Runtime for inorder-tree-walk? It takes Θ(n) since the procedure calls itself exactly twice for each node in the tree (once for the left, once for the right). Proof: • Let T(n) be the time taken by INORDER-TREE-WALK when it is called on a root of a n-node subtree. Since INORDER-TREE-WALK visits all nodes, T(n) = Ω(n). We need to show though that T(n) = O(n). • Since INORDER-TREE-WALK takes a constant amount of time on an empty subtree T(0) = c, c > 0. • For n > 0 suppose INORDER-TREE-WALK is called on a subtree whose left subtree has k nodes and the right one has n-k-1 nodes (why? ). The time to run INORDER-TREE-WALK is bounded by T(n) ≤ T(k) + T(n-k-1) + d (d > 0 is an upper bound to execute INORDER-TREE-WALK) • Substitution method: We want to show that T(n) = O(n) by proving that T(n) ≤ (c+d)n+c CSC 317 9
• Substitution method: We want to show that T(n) = O(n) by proving that T(n) ≤ (c+d)n+c For n = 0: T(n) ≤ (c+d)n+c = T(0) For n > 0: T(n) ≤ T(k) + T(n-k-1) + d = ((c+d)n+c) + ((c+d)(n-k-1) + c) + d = (c+d)n +c – (c+d) + c + d = (c+d)n+c q. e. d CSC 317 10
Query operations on binary search tree: • • • search max min successor Predecessor Costs? Depends on the height of the tree. Question: Is the height always log n? CSC 317 11
Query operations on binary search tree: • • • search max min successor Predecessor Costs? Depends on the height of the tree. Question: Is the height always log n? Answer: Not necessarily, since tree could be made non efficiently and each time branch to the right only. Then its run time could be O(n) and as bad as a linked list. Therefore height and run time for search queries could be O(log n) but could also be O(n)! CSC 317 12
Let’s search in a tree! (Umm, how? ) Main idea of searching for key k: if looking for k smaller than nodekey, search to the left, otherwise to the right. TREE-SEARCH (x, k) 1 if x == NIL or k == x. key 2 return x 3 if k < x. key 4 return TREE-SEARCH(x. left, k) 5 else return TREE-SEARCH(x. right, k) search for 13 … CSC 317 13
Let’s search in a tree! (Umm, how? ) Main idea of searching for key k: if looking for k smaller than nodekey, search to the left, otherwise to the right. TREE-SEARCH (x, k) 1 if x == NIL or k == x. key 2 return x 3 if k < x. key 4 return TREE-SEARCH(x. left, k) 5 else return TREE-SEARCH(x. right, k) Run time? height of tree. Again, could be O(log n) or O(n) depending on tree and its balance. CSC 317 14
Min/Max Question: How can we find the min? CSC 317 15
Min/Max Question: How can we find the min? Answer: Keep traversing to the left until you find a NIL. TREE-MINIMUM(x) 1 while x. left ≠ NIL 2 x = x. left 3 return x Finding the max: keep traversing to the right subtree… CSC 317 16
Successor: (similar for predecessor) TREE-SUCCESSOR (x) 1 if x. right ≠ NIL 2 return TREE-MINIMUM(x. right) 3 y=x. p 4 while y ≠ NIL and x == y. right 5 x=y 6 y = y. p 7 return y find successor of 15 CSC 317 17
Successor: (similar for predecessor) TREE-SUCCESSOR (x) 1 if x. right ≠ NIL 2 return TREE-MINIMUM(x. right) 3 y=x. p 4 while y ≠ NIL and x == y. right 5 x=y 6 y = y. p 7 return y Case 1 Case 2 CSC 317 18
Case 1: right sub-tree non empty: left-most node of right subtree (why? It’s the smallest value that is larger than the given node) find successor of 15 CSC 317 19
Successor: (similar for predecessor) Case 2: right sub-tree is empty: then successor is an ancestor (? ) and then more specifically what we mean here) Ancestor definition: an ancestor of node x is any node from the root to node x. This is either a “parent” of x or the node x itself. More specifically: successor of node x is an ancestor that is the lowest ancestor of node x, whose left child is also an ancestor of x. CSC 317 Right sub-tree empty: Successor of 9 is 13; lowest ancestor of 9 whose left child (here 9) is also an ancestor of 9 20
Successor: (similar for predecessor) Case 2: right sub-tree is empty: then successor is an ancestor (? ) and then more specifically what we mean here) Ancestor definition: an ancestor of node x is any node from the root to node x. This is either a “parent” of x or the node x itself. More specifically: successor of node x is an ancestor that is the lowest ancestor of node x, whose left child is also an ancestor of x. Why does this work? Consider node x (here node 4). Intuitively, if left child of ancestor y is also an ancestor of node x (here node 6), then node x is smaller than the ancestor y. All ancestor nodes before y (here before node 6 = node 3) do not have a left child that is an ancestor of x, which means that x is a right child of that ancestor and so that ancestor is smaller than x. CSC 317 21
Run time of successor: O(h), since we either follow a path up the tree or a path down the tress CSC 317 22
- Slides: 22