Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with.

Similar presentations


Presentation on theme: "CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with."— Presentation transcript:

1 CSC317 1 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

2 CSC317 2 Example of storing keys in a binary search tree: Binary Search Trees (binary because branches either to left or to right) Pattern anyone?

3 CSC317 3 Example of storing keys in a binary search tree: Binary Search Trees (binary because branches either to left or to right) Main property of binary search tree: Left side: key smaller equal to parent Right side: key larger equal to parent

4 CSC317 4 Example of storing keys in a binary search tree: Binary Search Trees (binary because branches either to left or to right) Given node with key k Left: all keys < k Right: all Keys > k 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. Binary search tree property

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

6 CSC317 6 Question: How can we traverse the tree and print out keys in ascending order (i.e. like this: 2 5 5 6 7 8)?

7 CSC317 7 Answer: Recursive algorithm

8 CSC317 8 What about this one?

9 CSC317 9 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 tha t 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

10 CSC317 10 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 = 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

11 CSC317 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 ?

12 CSC317 12 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) !

13 CSC317 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) search for 13 …

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

15 CSC317 15 Min/Max Question: How can we find the min?

16 CSC317 16 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…

17 CSC317 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 find successor of 15

18 CSC317 18 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

19 CSC317 19 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

20 CSC317 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. Right sub-tree empty: Successor of 9 is 13; lowest ancestor of 9 whose left child (here 9) is also an ancestor of 9

21 CSC317 21 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.

22 CSC317 22 Run time of successor: O(h), since we either follow a path up the tree or a path down the tress


Download ppt "CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with."

Similar presentations


Ads by Google