Machine-translated by Google. Questions are authored in English.
Top

Trees and Binary Trees Questions and Answers

1.

Consider the following New-order strategy for traversing a binary tree:

  • Visit the root;
  • Visit the right subtree using New-order;
  • Visit the left subtree 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:

Exam Year(s)

GATE CS - 2016,

Answer: C

The New-order traversal is - + 1 * 7 6 ^ 2 - 5 * 4 3.

Build the tree from the postfix expression 3 4 * 5 - 2 ^ 6 7 * 1 + -: it is ((3 * 4) - 5) ^ 2 - ((6 * 7) + 1). The root is the last operator, -, with left subtree ((3*4)-5) ^ 2 and right subtree (6*7)+1.

New-order visits root, then right subtree, then left subtree, recursively:

  • Root: -
  • Right subtree (6*7)+1: +, then its right child 1, then its left subtree * 7 6 (root, right, left).
  • Left subtree: ^, then its right child 2, then its left subtree -, 5, then * 4 3.

This gives - + 1 * 7 6 ^ 2 - 5 * 4 3. New-order is exactly the reverse of postfix order, which is a quick check: reverse 3 4 * 5 - 2 ^ 6 7 * 1 + - and you get the same string. The option with * 6 7 and * 3 4 visits the left child before the right in the inner subtrees, and the other two options start with the wrong node.

Please log in or register to report this question.

2.

Consider a binary tree \(T\) in which every node has either zero or two children. Let \(n \gt 0\) be the number of nodes in \(T\).

Which ONE of the following is the number of nodes in \(T\) that have exactly two children?

Exam Year(s)

GATE CS - 2025,

Answer: B

The number of nodes with two children is \(\dfrac{n-1}{2}\).

In a full binary tree, let i be the number of internal nodes (two children each) and l the number of leaves. Counting edges two ways: every node except the root has exactly one parent edge, so there are n - 1 edges, and each internal node contributes 2 edges, so there are 2i edges. Hence:

\[ 2i = n - 1 \quad \Rightarrow \quad i = \frac{n-1}{2} \]

A check: a root with two leaf children has n = 3 and i = 1 = (3 - 1)/2. (n + 1)/2 is the number of leaves, the usual confusion, and n/2 or (n - 2)/2 are not even integers for the odd n that a full binary tree always has.

Please log in or register to report this question.

3.

The set T represents various traversals over binary tree. The set S represents the order of visiting nodes during a traversal.

TS
I: InorderL: left subtree, node, right subtree
II: PreorderM: node, left subtree, right subtree
III: PostorderN: left subtree, right subtree, node

Which one of the following is the correct match from T to S ?

Exam Year(s)

GATE CS - 2026,

Answer: A

The correct match is I - L, II - M, III - N.

The names say where the node is visited relative to its subtrees:

  • Inorder: left subtree, node, right subtree (L). For a binary search tree this lists the keys in sorted order.
  • Preorder: node first, then left and right subtrees (M).
  • Postorder: left and right subtrees first, node last (N).

Every other option assigns at least one traversal the wrong position for the node: matching inorder to 'node, left, right' or postorder to 'left, node, right' confuses the prefixes pre, in and post, which refer exactly to that position.

Please log in or register to report this question.

4.

Consider the C function foo and the binary tree shown.

typedef struct node {
int val;
struct node *left, *right;
} node;

int foo(node *p) {
int retval;
if (p == NULL)
return 0;
else {
retval = p-\(\gt\)val + foo(p-\(\gt\)left) + foo(p-\(\gt\)right);
printf("%d ", retval);
return retval;
}
}

Binary tree with root 10; left child 5 with children 3 and 8; right child 11 whose only child, on its right, is 13

When foo is called with a pointer to the root node of the given binary tree, what will it print?

Exam Year(s)

GATE CS - 2023,

Answer: C

The program prints 3 8 16 13 24 50.

foo computes the sum of the values in the subtree rooted at p, and it prints that sum only after both recursive calls have returned, so the subtrees are printed in post-order: left subtree, right subtree, then the node.

  • Node 3: prints 3. Node 8: prints 8.
  • Node 5: 5 + 3 + 8 = 16, printed.
  • Node 13: prints 13. Node 11: 11 + 0 + 13 = 24, printed.
  • Root 10: 10 + 16 + 24 = 50, printed.

'3 8 5 13 11 10' is the post-order of the node values, which would print val rather than the subtree sums. '3 5 8 10 11 13' is the in-order sequence. '3 16 8 50 24 13' has the right sums but in an in-order arrangement that the code does not produce.

Please log in or register to report this question.

5.

The pre-order and post-order traversal of a binary tree generates the same output. The tree can have maximum :

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: C

The tree can have at most one node.

Pre-order visits the root first and then its subtrees; post-order visits the subtrees first and the root last. In any tree with two or more nodes, the root is the first node of the pre-order output but the last node of the post-order output, so the two sequences differ.

Only a tree with a single node, where the root is both first and last, gives the same output in both traversals. So two nodes, three nodes and any number of nodes are all wrong.

Please log in or register to report this question.

6.

The minimum number of nodes possible in a binary tree of height h (assuming that the root is at height 1) is :

Exam Year(s)

MH SET - 2020, SET Exam - 2020,

Answer: A

The minimum number of nodes is h.

A binary tree of height h has the fewest nodes when it is a single chain, each node having exactly one child. With the root at height 1, that chain has one node per level, h nodes in all.

\(2^{h} - 1\) is the maximum number of nodes (a full binary tree), \(2^{h-1}\) is the most nodes possible on the last level, and \(2^{h}\) is not a bound for this counting at all.

Please log in or register to report this question.

7.

The in-order traversal output and pre-order traversal output of a binary tree are same, then the binary tree is :

Exam Year(s)

MH SET - 2023, SET Exam - 2023,

Answer: B

The binary tree is right skewed.

In-order traversal visits left subtree, node, right subtree; pre-order visits node, left subtree, right subtree. The two orders can only agree if no node has a left subtree, because a left subtree would be visited before its node in in-order but after it in pre-order. Every node then has only a right child, which is a right-skewed tree; both traversals list the nodes from the root downwards.

In a left-skewed tree the in-order output is the reverse of the pre-order. A balanced or complete tree with more than one level has left children, so its two outputs differ.

Please log in or register to report this question.

8.

A binary tree whose every node has either zero or two children is called :

Exam Year(s)

MH SET - 2024, SET Exam - 2024,

Answer: B

Such a tree is called an extended binary tree.

An extended binary tree, also called a 2-tree, is one in which every node has either 0 or 2 children. The nodes with two children are internal nodes and those with none are external nodes. It is also known as a full binary tree.

A binary search tree is defined by key order, a complete binary tree by levels being filled left to right, and a skewed tree by every node having only one child.

Please log in or register to report this question.

Answer: C

The tree has root C, with B as its left child, D as its right child, and A as the left child of B.

Preorder lists the root first, so C is the root. In the inorder sequence A B C D, everything before C (A, B) is in the left subtree and D is in the right subtree. In the left subtree the preorder is B A, so B is its root, and A comes before B in inorder, so A is B's left child.

Check: inorder A B C D and preorder C B A D both match. The trees rooted at A would give a preorder starting with A, so options A, B and D are wrong.

Please log in or register to report this question.

10.

A complete binary tree has depth, given by the formula (where n is the number nodes) :

Exam Year(s)

MH SET - 2024, SET Exam - 2024,

Answer: C

The depth is \(\log_2 (n + 1)\).

A complete binary tree of depth d has at most \(2^d - 1\) nodes, filled level by level. Solving for d:

\[ \begin{aligned} n &= 2^d - 1 \\ d &= \log_2 (n + 1) \end{aligned} \]

For a tree that is not full, the depth is the ceiling of this value. \(\log_2 n\) is out by one for full trees (7 nodes give 3 levels, not 2.8), and \(n \log_2 n\) is the cost of sorting, not a depth.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Trees and Binary Trees Questions and Answers || Trees and Binary Trees MCQ Questions and Answers || Trees and Binary Trees GK Questions and Answers || Trees and Binary Trees GK MCQ Questions || Trees and Binary Trees Multiple Choice Questions and Answers || Trees and Binary Trees GK || GK on Trees and Binary Trees || Data Structures and Algorithms Questions and Answers || Data Structures and Algorithms MCQ Questions and Answers || Data Structures and Algorithms GK Questions and Answers || GK on Data Structures and Algorithms