NPTEL Design and Analysis of Algorithms Week 6 Assignment Answers 2024
1. Suppose that the following is a binary search tree. The letters indicate the names of the nodes, not the values that are stored.
What is the predecessor node, in terms of value, of the node C?
- A
- F
- K
- N
Answer :- For Answers Click Here
2. We have n distinct values stored in a binary search tree. Define the height of a tree to be the number of nodes in the longest path from root to leaf. Which of the following statements is not true?
- If the root is the median value, the height of the tree is at most n/2.
- If the root is the median value, the height of the tree is at most log n.
- The height of the tree is at least log n.
- The height of the tree is at most n.
Answer :- For Answers Click Here
3. 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?
- O(n)
- O(n log n)
- O(n2)
- Depends on the contents of the original sequence
Answer :- For Answers Click Here
4. 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?
- O(n)
- O(n log n)
- O(n2)
- Depends on the contents of the original sequence.
Answer :-
5. Preorder traversal prints a tree by first listing the value at the root and then recursively listing the values of the left and right subtrees.
function preOrder(t) {
if (t != NIL) {
print(t.value);
preOrder(t.left);
preOrder(t.right);
}
}
What is the complexity of preorder traversal for a binary search tree with n nodes?
- O(log n) if the tree is balanced, O(n2) otherwise.
- O(n) if the tree is balanced, O(n log n) otherwise.
- O(n log n) whether the tree is balanced or unbalanced.
- O(n) whether the tree is balanced or unbalanced.
Answer :- For Answers Click Here