Open In App

Data Structures and Algorithms | Set 10

Last Updated : 13 Dec, 2022
Like Article

Following questions have been asked in GATE CS 2007 exam.

1. The height of a binary tree is the maximum number of edges in any root to leaf path. The maximum number of nodes in a binary tree of height h is:
(A) 2^h -1
(B) 2^(h-1) – 1
(C) 2^(h+1) -1
(D) 2*(h+1)

Answer (C)
Maximum number of nodes will be there for a complete tree.
Number of nodes in a complete tree of height h = 1 + 2 + 2^2 + 2*3 + …. 2^h = 2^(h+1) – 1

2: The maximum number of binary trees that can be formed with three unlabelled nodes is:
(A) 1
(B) 5
(C) 4
(D) 3

Answer (B)

          /     \
        O        O





Note that nodes are unlabelled. If the nodes are labeled, we get more number of trees.

3. Which of the following sorting algorithms has the lowest worst-case complexity?
(A) Merge sort
(B) Bubble sort
(C) Quick sort
(D) Selection sort

Answer (A)

Worst case complexities for the above sorting algorithms are as follows:
Merge Sort — nLogn
Bubble Sort — n^2
Quick Sort — n^2
Selection Sort — n^2

4. The following postfix expression with single digit operands is evaluated using a stack:

              8 2 3 ^ / 2 3 * + 5 1 * - 

Note that ^ is the exponentiation operator. The top two elements of the stack after the first * is evaluated are:
(A) 6, 1
(B) 5, 7
(C) 3, 2
(D) 1, 5

Answer (A)
The algorithm for evaluating any postfix expression is fairly straightforward:

1. While there are input tokens left
    o Read the next token from input.
    o If the token is a value
       + Push it onto the stack.
    o Otherwise, the token is an operator 
      (operator here includes both operators, and functions).
       * It is known a priori that the operator takes n arguments.
       * If there are fewer than n values on the stack
        (Error) The user has not input sufficient values in the expression.
       * Else, Pop the top n values from the stack.
       * Evaluate the operator, with the values as arguments.
       * Push the returned results, if any, back onto the stack.
2. If there is only one value in the stack
    o That value is the result of the calculation.
3. If there are more values in the stack
    o (Error)  The user input has too many values.

Source for algorithm:

Let us run the above algorithm for the given expression.
First three tokens are values, so they are simply pushed. After pushing 8, 2 and 3, the stack is as follows

    8, 2, 3  

When ^ is read, top two are popped and power(2^3) is calculated

    8, 8 

When / is read, top two are popped and division(8/8) is performed


Next two tokens are values, so they are simply pushed. After pushing 2 and 3, the stack is as follows

    1, 2, 3

When * comes, top two are popped and multiplication is performed.

    1, 6

5. The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively. The postorder traversal of the binary tree is:

(A) d e b f g c a
(B) e d b g f c a
(C) e d b f g c a
(D) d e f g b c a

Answer (A)

Below is the given tree.
                           /    \
                        /          \
                      b             c
                   /   \          /   \
                 /       \      /       \
               d         e    f          g

Please see GATE Corner for all previous year paper/solutions/explanations, syllabus, important dates, notes, etc.

Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads