Left nil do x xleft return x n time oh tree maximumx

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ght ≠Nil) do x = x.right; return x; CSE 2331/5331 Successor n Successor(x) n n n n The node y with the smallest key greater than or equal to x.key Ambiguous when multiple nodes have same key Successor in the inorder tree walk Return Nil if none exists -- has largest key CSE 2331/5331 Example 15 6 3 2 18 7 17 20 13 4 9 CSE 2331/5331 Two Cases n n n If right child of x exists n Leftmost node in right subtree why? Otherwise n Lowest ancestor y where x is in the left-subtree of y Tree-successor(x) if x.right ≠Nil then return Tree-minimum(x.right) y = x.parent while y ≠Nil and x = y.right do x = y y = y.right return y Time: O(h) CSE 2331/5331 Predecessor (x) n Two cases n The left child of x exists n n Otherwise n n Rightmost child in the left subtree of x Lowest ancestor y where x is in the right subtree of y. Completely symmetric CSE 2331/5331 Complete Binary Tree n A complete binary tree is a binary search tree: n n all internal nodes have exactly two children all leaves are at the same distance from the root CSE 2331/5331 CSE 2331/5331 Balanced Binary Trees CSE 2331/5331 Summary n Using binary search tree n n n n n Traversal (inorder tree walk) Min / max Search Successor / predecessor All have running time O(h) n h is height of the tree. We could use a sorted array to do all these in O(1) time! However, array is not efficient in dynamic updates. CSE 23...
View Full Document

Ask a homework question - tutors are online