Sei sulla pagina 1di 2

Q.6 .

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.

All of find, insert, delete, min, max, pred, succ

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)

O(n) whether the tree is balanced or unbalanced.

Q.8 Consider the following algorithm to build a balanced search tree from a sorted
sequence.

 Make the mid-point of the sequence the root of the tree


 Recursively construct balanced search trees from elements to the left and right of
the midpoint and make these the left and right subtrees of the root.

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.

 Make the mid-point of the sequence the root of the tree


 Recursively construct balanced search trees from elements to the left and right of
the midpoint and make these the left and right subtrees of the root.
What is the complexity of this procedure where the sorted sequence is a sorted list?

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)

Q.10 Which of the following is not a greedy algorithm?

Kruskal's algorithm for minimum cost spanning tree


Prim's algorithm for minimum cost spanning tree
Floyd-Warshall algorithm for all pairs shortest paths
Dijkstra's algorithm for single source shortest paths
ANSWER: Floyd-Warshall efficiently tests all possible paths of length upto n-1 between
every pair of nodes, so it is not greedy.
Floyd-Warshall algorithm for all pairs shortest paths