CSE 2320 - Assignments

Not Graded Practice for Assignment 10


Practice Question 1

Consider a binary search tree interface where: What is the running time complexity of insertions in this case? Note that the insertion code must ensure that, assuming that the height variable is correct for each node before the insertion, after each insertion the height variable is still correct for each node of the tree. Does this requirement affect the running time complexity?


Practice Question 2

Consider a binary search tree interface where we support the following rotate_root_right function: Suppose that, before calling rotate_root_right: After function rotate_root_right is done, the tree looks like this: Part a: What is the best running time complexity you can achieve for this function?

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.


Practice Question 3

Suppose that we have a binary search tree where we allow duplicate keys. The constraint is that, if a node N has key K, the keys on the left subtree of N are less than or equal to K, and the keys on the right subtree of N are greater than or equal to K.

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?


Practice Question 4

Suppose that we have a binary search tree where we do not allow duplicate keys. Suppose that we want to support a function 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.


Practice Question 5

(This is Problem 12.50 from the textbook).

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.