Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Suppose we do not have a parent pointer in the nodes of a search tree, only left-child and
right-child. Which of the following operations can be computed in time O(log n) for a balanced
search tree?
ANSWER: The only operations that were explained using the parent link were pred/succ, in the case
where the node was a minimum/maximum node in its subtree. But we can find the appropriate
ancestor by a second scan from root to this node by checking where the path takes the last
right/left turn.
Q.7. Postorder traversal prints a tree by recursively listing the values of the left and right
subtrees and then listing the value at the root.
function postOrder(t)
if (t != NIL)
postOrder(t.left)
postOrder(t.right)
print(t.value)
What is the complexity of postorder traversal for a binary search tree with n nodes?
ANSWER: Postorder traversal visits and lists each element once, so it is O(n)
Q.8 Consider the following algorithm to build a balanced search tree from a sorted
sequence.
What is the complexity of this procedure where the sorted sequence is a sorted array?
ANSWER: The recursive construction has the recurrence T(n) = 2T(n/2) + O(1) since we can find the
midpoint of an array in constant time.
O(n)
Q.9 Consider the following algorithm to build a balanced search tree from a sorted
sequence.
ANSWER: The recursive construction has the recurrence T(n) = 2T(n/2) + O(n), like merge sort,
since it takes time O(n) to find the midpoint in a list.
O(n log n)