rotate_root_right
function:
Suppose that, before calling rotate_root_right
:
rotate_root_right
is done, the tree looks like this:
Part b: Suppose that each node has a member variable called height that records the length of the longest path from that node to a leaf. You need to ensure that, assuming that the height variable is correct for each node before calling rotate_root_right
, after rotate_root_right
is done the height variable is still correct for each node of the tree. What is the best running time complexity you can achieve for rotate_root_right
under this constraint?
Part c: Suppose that each node has a member variable called depth that records the length of the path from that node to the root of the tree. You need to ensure that, assuming that the depth variable is correct for each node before calling rotate_root_right
, after rotate_root_right
is done the depth variable is still correct for each node of the tree. What is the best running time complexity you can achieve for rotate_root_right
under this constraint? For this part, assume that nodes do NOT contain the height variable of part b.
Part a: Implement a function count_occurrences(node, key)
that returns the number of all occurrences of the specified key in the tree rooted at the given node.
Part b: What is the best running time complexity you can achieve for this function, assuming that the tree is balanced?
count_less_equal(N, K)
that returns the number of items with key less than K in the tree rooted at N. Furthermore, we want this function to take time logarithmic to the number of items in the tree, when the tree is balanced (do not worry about the time complexity when the tree is not balanced).
Part a: How would you modify the definition of a node to support this function running in logarithmic time?
Part b: Implement count_less_equal(N, K)
, using your answer for part a, so that it has logarithmic time complexity.
Suppose that we have an estimate ahead of time of how often search keys are to be accessed in a binary tree. Should the keys be inserted into the tree in increasing or decreasing order of likely frequency of access? Explain your answer.
Back to the list of assignments.