0% found this document useful (0 votes)
138 views2 pages

Quiz 6

The document discusses algorithms for balanced binary search trees. It provides questions and answers about the time complexity of various operations on a binary search tree including find, insert, delete, traversal, and building a balanced binary search tree from a sorted sequence. Specifically, it states that find, insert, delete, min, max, pred, and succ can all be performed in O(log n) time even without parent pointers. It also states that postorder traversal has O(n) time complexity, and that building a balanced binary search tree from a sorted array has O(n) time complexity but from a sorted list has O(n log n) time complexity due to the cost of finding the midpoint. It identifies that the Floyd-

Uploaded by

Mamta Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views2 pages

Quiz 6

The document discusses algorithms for balanced binary search trees. It provides questions and answers about the time complexity of various operations on a binary search tree including find, insert, delete, traversal, and building a balanced binary search tree from a sorted sequence. Specifically, it states that find, insert, delete, min, max, pred, and succ can all be performed in O(log n) time even without parent pointers. It also states that postorder traversal has O(n) time complexity, and that building a balanced binary search tree from a sorted array has O(n) time complexity but from a sorted list has O(n log n) time complexity due to the cost of finding the midpoint. It identifies that the Floyd-

Uploaded by

Mamta Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like