GATE 2005
203 questions
MCQ 2031-mark 203
Questions 106–110 of 203
Answer: (B)
The number of distinct BSTs with distinct keys is the Catalan number:
A given binary tree shape can be filled with the keys in only way to make a BST. So the number of different BSTs with keys is the same as the number of different binary tree shapes with nodes.
For :
Check by choosing the root: .
Answer: (B)
10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29
Which one of the following sequences of keys can be the result of an in-order traversal of the tree T?
Answer: (A)
The in-order sequence uses the same 12 keys as the postorder sequence: 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95 when sorted. Among the given options, only (A) is in ascending order, and it is exactly this list. Options (B), (C) and (D) are not sorted.
Answer: (A)
10, 8, 5, 3, 2
Two new elements '1' and '7' are inserted in the heap in that order. The level-order traversal of the heap after the insertion of the elements is:
Answer: (D)
Initial heap:
Insert 1 at index 6, as the left child of 5 (index 3). 1 < 5, so it stays. Array: 10, 8, 5, 3, 2, 1.
Heap after inserting 1:
Insert 7 at index 7, as the right child of 5. 7 > 5, so swap with 5. Then the parent is 10 > 7, so stop.
Heap after inserting 7:
Level order: 10, 8, 7, 3, 2, 1, 5.
Answer: (D)
Answer: (D)
goes to loc
goes to loc
goes to loc
goes to loc (collision, so next free space)
goes to loc (collision in , and are already full)
So the answer is D
double foo (int n)
{
int i;
double sum;
if (n == 0) return 1.0;
else
{
sum = 0.0;
for (i = 0; i < n; i++)
sum += foo (i);
return sum;
}
} Suppose we modify the above function foo() and store the values of foo (i), , as and when they are computed. With this modification, the time complexity for function foo() is significantly reduced. The space complexity of the modified function would be:Answer: (B)
Without storing values, the calls form this tree. The loop runs for , so the last child of the root is , and the labels going down that last branch are .
Many calls repeat. Let be the number of calls: gives . The deepest chain of calls is , so the stack depth is . So space is linear and time is exponential.
Take . Then , in general for . There are calls and the stack depth is .
Now store each in a one-dimensional table of doubles the first time it is computed. The plan is:
- First, look in the table for the value of the call we need.
- If it is there, return it without any recursive call.
- If not, run the loop of recursive calls, store the result, and return it.
The figure below shows the same idea.
Here is the code:
#define UNUSED 0.5
double *memo_table; // size n+1, every entry set to UNUSED, then memo_table[0] = 1.0
double foo(int n) {
int i;
double sum;
if (memo_table[n] != UNUSED) {
return memo_table[n];
} else {
sum = 0.0;
for (i = 0; i < n; i++) {
sum += foo(i);
}
return memo_table[n] = sum;
}
}
The loop calls in order, so when runs, are already stored and its own calls return at once. So the recursion is only a few levels deep, which is stack space. The time drops to , because each of the values is computed once with a loop of at most steps.
Space = table + stack = . It cannot be , since the table keeps values. and are far more than is needed.
Answer: (B)