GATE 2016 (Session 2)
65 questions
MCQ 38 · NAT 271-mark 30 · 2-mark 35
Questions 46–50 of 65
Consider the following New-order strategy for traversing a binary tree:
Visit the root;
Visit the right sub tree using New-order;
Visit the left sub tree using New-order;
The New-order traversal of the expression tree corresponding to the reverse polish expression
3 4 * 5 - 2 ^ 6 7 * 1 + - is given by:
Visit the root;
Visit the right sub tree using New-order;
Visit the left sub tree using New-order;
The New-order traversal of the expression tree corresponding to the reverse polish expression
3 4 * 5 - 2 ^ 6 7 * 1 + - is given by:
Answer: (B)
Build the expression tree from the postfix with a stack. Operands are pushed; an operator takes the top two items as its right and left operands.
gives (3*4). Then gives ((3*4) 5), and gives (((3*4) 5) ^ 2).
gives (6*7), and gives ((6*7) + 1).
The last operator joins these two, so the root is , the left subtree is ((3*4) 5) ^ 2 and the right subtree is (6*7) + 1.
New-order visits the root first, then the right subtree, then the left subtree, and the same rule applies inside each subtree:
- Root:
- Right subtree (+): visit +, then its right child 1, then its left child (6*7), which gives * 7 6. So: + 1 * 7 6
- Left subtree (^): visit ^, then its right child 2, then its left child (3*4) 5. That part gives , then 5, then * 4 3. So: ^ 2 5 * 4 3
Joined: - + 1 * 7 6 ^ 2 - 5 * 4 3
Answer: (B)
gives (3*4). Then gives ((3*4) 5), and gives (((3*4) 5) ^ 2).
gives (6*7), and gives ((6*7) + 1).
The last operator joins these two, so the root is , the left subtree is ((3*4) 5) ^ 2 and the right subtree is (6*7) + 1.
New-order visits the root first, then the right subtree, then the left subtree, and the same rule applies inside each subtree:
- Root:
- Right subtree (+): visit +, then its right child 1, then its left child (6*7), which gives * 7 6. So: + 1 * 7 6
- Left subtree (^): visit ^, then its right child 2, then its left child (3*4) 5. That part gives , then 5, then * 4 3. So: ^ 2 5 * 4 3
Joined: - + 1 * 7 6 ^ 2 - 5 * 4 3
Answer: (B)
Consider thefollowingprogram:
int f(int*p, int n)
{
if (n<=1)return 0;
else return max(f(p+1,n-1),p[0]-p[1]);
}
int main()
{
int a[]={3,5,2,6,4};
printf("%d", f(a,5));
} Note: max(x,y) returns the maxi mumof x and y. The value printed by this program is____________ .Answer: 3
The array is . The call returns 0 if , else . Trace from the innermost call:
- (base case)
So f returns the larger of 0 and the biggest difference over neighbouring pairs. Here the differences are , so the printed value is 3.
Answer: 3
Let A1,A2,A3, and A4 be four matrices of dimensions 10x5, 5x20, 20x10, and 10x5, respectively. The minimum number of scalar multiplications required to find the product A1A2A3A4 using the basic matrix multiplication method is __________.
Answer: 1500
The dimensions are 10, 5, 20, 10, 5. Let be the least cost of multiplying . A product of an and a matrix costs .
Pairs: , , .
Three matrices:
- : ; . So .
- : ; . So .
All four:
-
-
-
The minimum is 1500.
Answer: 1500
Pairs: , , .
Three matrices:
- : ; . So .
- : ; . So .
All four:
-
-
-
The minimum is 1500.
Answer: 1500
The given diagram shows the flowchart for a recursive function A(n). Assume that all statements, except for the recursive calls, have O(1) time complexity. If the worst case time complexity of this function is , then the least possible value (accurate upto two decimal positions) of is .
Answer: 2.2 to 2.4
In the flowchart, the worst case path makes these calls: A(n/2) at the start, then the right branch with two calls, then one more call going down, then one more call on the left branch. This gives 5 calls to A(n/2).
So the recurrence and its result are:
Answer: 2.32
So the recurrence and its result are:
Answer: 2.32
The number of ways in which the numbers 1,2,3,4,5,6,7 can be inserted in an empty binary search tree, such that the resulting tree has height 6, is _____.
Note: The height of a tree with a single node is 0.
Note: The height of a tree with a single node is 0.
Answer: 64
A tree with 7 nodes and height 6 must be one single path.
So at every insertion, the number chosen must be either the smallest or the largest of the numbers still left. That gives 2 choices each time, and the last number has no choice.
Answer: 64
So at every insertion, the number chosen must be either the smallest or the largest of the numbers still left. That gives 2 choices each time, and the last number has no choice.
Answer: 64