## Binary Trees

Please wait while the activity loads.

If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled.

If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled.

Question 1 |

Which of the following is a true about Binary Trees

Every binary tree is either complete or full. | |

Every complete binary tree is also a full binary tree. | |

Every full binary tree is also a complete binary tree. | |

No binary tree is both complete and full. | |

None of the above |

**Binary Trees**

**Discuss it**

Question 1 Explanation:

A full binary tree (sometimes proper binary tree or 2-tree or strictly binary tree) is a tree in which every node other than the leaves has two children.
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
A) is incorrect. For example, the following Binary tree is neither complete nor full

12 / 20 / 30B) is incorrect. The following binary tree is complete but not full

12 / \ 20 30 / 30C) is incorrect. Following Binary tree is full, but not complete

12 / \ 20 30 / \ 20 40D) is incorrect. Following Binary tree is both complete and full

12 / \ 20 30 / \ 10 40Please refer http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees

Question 2 |

If arity of operators is fixed, then which of the following notations can be used to parse expressions without parentheses?
a) Infix Notation (Inorder traversal of a expression tree)
b) Postfix Notation (Postorder traversal of a expression tree)
c) Prefix Notation (Preorder traversal of a expression tree)

b and c | |

Only b | |

a, b and c | |

None of them |

**Binary Trees**

**Discuss it**

Question 2 Explanation:

See Polish notation

Question 3 |

What are the main applications of tree data structure?
1) Manipulate hierarchical data
2) Make information easy to search (see tree traversal).
3) Manipulate sorted lists of data
4) Router algorithms
5) Form of a multi-stage decision-making, like Chess Game.
6) As a workflow for compositing digital images for visual effects

1, 2, 3, 4 and 6 | |

1, 2, 3, 4 and 5 | |

1, 3, 4, 5 and 6 | |

1, 2, 3, 4, 5 and 6 |

**Binary Trees**

**Discuss it**

Question 3 Explanation:

Question 4 |

Level of a node is distance from root to that node. For example, level of root is 1 and levels of left and right children of root is 2. The maximum number of nodes on level i of a binary tree is

In the following answers, the operator '^' indicates power.

In the following answers, the operator '^' indicates power.

2^(i-1) | |

2^i | |

2^(i+1) | |

2^[(i+1)/2] |

**Binary Trees**

**Discuss it**

Question 4 Explanation:

Answer: 2^(i-1) where i ≧1.
Proof: by Induction,
• Introduction base: i=1 (root)
The number of node is: 2^(i-1) = 2^0 = 1.
• Induction hypothesis
Assume that for i ≧1, the maximum number of nodes on level i-1 is 2^(i-2).
• Induction step
Since each node in a binary tree has a maximum degree of 2. Therefore, the maximum number of nodes on level i is 2*2^(i-2) which is 2^(i-1)

Question 5 |

In a complete k-ary tree, every internal node has exactly k children or no child. The number of leaves in such a tree with n internal nodes is:

nk | |

(n – 1) k+ 1 | |

n( k – 1) + 1 | |

n(k – 1) |

**Binary Trees**

**Discuss it**

Question 5 Explanation:

For an k-ary tree where each node has k children or no children, following relation holds
L = (k-1)*n + 1
Where L is the number of leaf nodes and n is the number of internal nodes.
Let us see following for example

o / | \ o o o / | \ / | \ o o o o o o / | \ o o o k = 3 Number of internal nodes n = 4 Number of leaf nodes = (k-1)*n + 1 = (3-1)*4 + 1 = 9

Question 6 |

The maximum number of binary trees that can be formed with three unlabeled nodes is:

1 | |

5 | |

4 | |

3 |

**Binary Trees**

**Discuss it**

Question 6 Explanation:

Following are all possible unlabeled binary trees

O / \ O O (i) O / O / O (ii) O / O \ O (iii) O \ O \ O (iv) O \ O / O (v)Note that nodes are unlabeled. If the nodes are labeled, we get more number of trees.

Question 7 |

The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3 children is:

n/2 | |

(n-1)/3 | |

(n-1)/2 | |

(2n+1)/3 |

**Binary Trees**

**Discuss it**

Question 7 Explanation:

Let L be the number of leaf nodes and I be the number of internal nodes, then following relation holds for above given tree (For details, please see question 3 of http://geeksforgeeks.org/?p=4545)

L = (3-1)I + 1 = 2I + 1Total number of nodes(n) is sum of leaf nodes and internal nodes

n = L + IAfter solving above two, we get L = (2n+1)/3

Question 8 |

A weight-balanced tree is a binary tree in which for each node. The number of nodes in the left sub tree is at least half and at most twice the number of nodes in the right sub tree. The maximum possible height (number of nodes on the path from the root to the farthest leaf) of such a tree on n nodes is best described by which of the following?
a)
b)
c)
d)

A | |

B | |

C | |

D |

**Binary Trees**

**Discuss it**

Question 8 Explanation:

Let the maximum possible height of a tree with n nodes is represented by H(n).
The maximum possible value of H(n) can be approximately written using following recursion

H(n) = H(2n/3) + 1The solution of above recurrence is [Tex]\log_{3/2} n[/Tex]. We can simply get it by drawing a recursion tree.

**4. Consider the following algorithm for searching for a given number x in an unsorted - array A[1..n] having n distinct values:**a) n b) n-l c) 2n d) n/2 Answer(a) If you remember the coin and dice questions, you can just guess the answer for the above. Below is proof for the answer. Let expected number of comparisons be E. Value of E is sum of following expression for all the possible cases.1) Choose an i uniformly at random from 1..n; 2) If A[i] = x then Stop else Goto 1;Assuming that x is present in A, what is the expected number of comparisons made by the algorithm before it terminates?

number_of_comparisons_for_a_case * probability_for_the_caseCase 1

If A[i] is found in the first attempt number of comparisons = 1 probability of the case = 1/nCase 2

If A[i] is found in the second attempt number of comparisons = 2 probability of the case = (n-1)/n*1/nCase 3

If A[i] is found in the third attempt number of comparisons = 2 probability of the case = (n-1)/n*(n-1)/n*1/nThere are actually infinite such cases. So, we have following infinite series for E.

E = 1/n + [(n-1)/n]*[1/n]*2 + [(n-1)/n]*[(n-1)/n]*[1/n]*3 + …. (1)After multiplying equation (1) with (n-1)/n, we get

E (n-1)/n = [(n-1)/n]*[1/n] + [(n-1)/n]*[(n-1)/n]*[1/n]*2 + [(n-1)/n]*[(n-1)/n]*[(n-1)/n]*[1/n]*3 ……….(2)Subtracting (2) from (1), we get

E/n = 1/n + (n-1)/n*1/n + (n-1)/n*(n-1)/n*1/n + …………The expression on right side is a GP with infinite elements. Let us apply the sum formula (a/(1-r))

E/n = [1/n]/[1-(n-1)/n] = 1 E = n

Question 9 |

A complete n-ary tree is a tree in which each node has n children or no children. Let I be the number of internal nodes and L be the number of leaves in a complete n-ary tree. If L = 41, and I = 10, what is the value of n?

6 | |

3 | |

4 | |

5 |

**Binary Trees**

**Discuss it**

Question 9 Explanation:

For an n-ary tree where each node has n children or no children, following relation holds

Where L is the number of leaf nodes and I is the number of internal nodes. Let us find out the value of n for the given data.L = (n-1)*I + 1

L = 41 , I = 10 41 = 10*(n-1) + 1 (n-1) = 4 n = 5

Question 10 |

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:

2^h -1 | |

2^(h-1) – 1 | |

2^(h+1) -1 | |

2*(h+1) |

**Binary Trees**

**Discuss it**

Question 10 Explanation:

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

Question 11 |

A scheme for storing binary trees in an array X is as follows. Indexing of X starts at 1 instead of 0. the root is stored at X[1]. For a node stored at X[i], the left child, if any, is stored in X[2i] and the right child, if any, in X[2i+1]. To be able to store any binary tree on n vertices the minimum size of X should be. (GATE CS 2006)

log2n | |

n | |

2n + 1 | |

2^n — 1 |

**Binary Trees**

**Discuss it**

Question 11 Explanation:

For a right skewed binary tree, number of nodes will be 2^n - 1. For example, in below binary tree, node 'A' will be stored at index 1, 'B' at index 3, 'C' at index 7 and 'D' at index 15.

A \ \ B \ \ C \ \ D

Question 12 |

Postorder traversal of a given binary search tree, T produces the following sequence of keys
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? (GATE CS 2005)

9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95 | |

9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29 | |

29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95 | |

95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29 |

**Binary Trees**

**Discuss it**

Question 12 Explanation:

Inorder traversal of a BST always gives elements in increasing order. Among all four options, a) is the only increasing order sequence.

Question 13 |

Consider the following nested representation of binary trees: (X Y Z) indicates Y and Z are the left and right sub stress, respectively, of node X. Note that Y and Z may be NULL, or further nested. Which of the following represents a valid binary tree?

(1 2 (4 5 6 7)) | |

(1 (2 3 4) 5 6) 7) | |

(1 (2 3 4)(5 6 7)) | |

(1 (2 3 NULL) (4 5)) |

**Binary Trees**

**Discuss it**

Question 13 Explanation:

C is fine.

(1 (2 3 4)(5 6 7)) represents following binary tree 1 / \ 2 5 / \ / \ 3 4 6 7A) (1 2 (4 5 6 7)) is not fine as there are 4 elements in one bracket. B) (1 (2 3 4) 5 6) 7) is not fine as there are 2 opening brackets and 3 closing. D) (1 (2 3 NULL) (4 5)) is not fine one bracket has only two entries (4 5)

Question 14 |

Consider a node X in a Binary Tree. Given that X has two children, let Y be Inorder successor of X. Which of the following is true about Y?

Y has no right child | |

Y has no left child | |

Y has both children | |

None of the above |

**Binary Trees**

**Discuss it**

Question 14 Explanation:

Since X has both children, Y must be leftmost node in right child of X.

Question 15 |

In a binary tree with n nodes, every node has an odd number of descendants. Every node is considered to be its own descendant. What is the number of nodes in the tree that have exactly one child?

0 | |

1 | |

(n-1)/2 | |

n-1 |

**Binary Trees**

**GATE CS 2010**

**Discuss it**

Question 15 Explanation:

It is mentioned that each node has odd number of descendants including node itself, so all nodes must have even number of descendants 0, 2, 4 so on. Which means each node should have either 0 or 2 children. So there will be no node with 1 child. Hence 0 is answer.
Following are few examples.

a / \ b c a / \ b c / \ d eSuch a binary tree is full binary tree (a binary tree where every node has 0 or 2 children).

Question 16 |

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:

2 ^{h}−1 | |

2 ^{h−1} -1 | |

2 ^{h+1}-1 | |

2 ^{h+1} |

**Binary Trees**

**GATE-CS-2007**

**Discuss it**

Question 16 Explanation:

Question 17 |

The height of a tree is the length of the longest root-to-leaf path in it. The maximum and minimum number of nodes in a binary tree of height 5 are

63 and 6, respectively | |

64 and 5, respectively | |

32 and 6, respectively | |

31 and 5, respectively |

**Binary Trees**

**GATE-CS-2015 (Set 1)**

**Discuss it**

Question 17 Explanation:

Number of nodes is maximum for a perfect binary tree. A perfect binary tree of height h has 2^{h+1}- 1 nodes Number of nodes is minimum for a skewed binary tree. A perfect binary tree of height h has h+1 nodes.

Question 18 |

A binary tree T has 20 leaves. The number of nodes in T having two children is

18 | |

19 | |

17 | |

Any number between 10 and 20 |

**Binary Trees**

**GATE-CS-2015 (Set 2)**

**Discuss it**

Question 18 Explanation:

Sum of all degrees = 2 * |E|.
Here considering tree as a k-ary tree :

Sum of degrees of leaves + Sum of degrees for Internal Node except root + Root's degree = 2 * (No. of nodes - 1). Putting values of above terms, L + (I-1)*(k+1) + k = 2 * (L + I - 1) L + k*I - k + I -1 + k = 2*L + 2I - 2 L + K*I + I - 1 = 2*L + 2*I - 2 K*I + 1 - I = L (K-1)*I + 1 = L Given k = 2, L=20 ==> (2-1)*I + 1 = 20 ==> I = 19 ==> T has 19 internal nodes which are having two children.See Handshaking Lemma and Interesting Tree Properties for proof. This solution is contributed by

**Anil Saikrishna Devarasetty**Question 19 |

Consider a complete binary tree where the left and the right subtrees of the root are max-heaps. The lower bound for the number of operations to convert the tree to a heap is

Ω(logn) | |

Ω(n) | |

Ω(nlogn) | |

Ω(n ^{2}) |

**Binary Trees**

**GATE-CS-2015 (Set 2)**

**Discuss it**

Question 19 Explanation:

The answer to this question is simply max-heapify function. Time complexity of max-heapify is O(Log n) as it recurses at most through height of heap.

// A recursive method to heapify a subtree with root at given index // This method assumes that the subtrees are already heapified void MinHeap::MaxHeapify(int i) { int l = left(i); int r = right(i); int largest = i; if (l < heap_size && harr[l] < harr[i]) largest = l; if (r < heap_size && harr[r] < harr[smallest]) largest = r; if (largest != i) { swap(&harr[i], &harr[largest]); MinHeapify(largest); } }See Binary Heap for details.

Question 20 |

An array of integers of size n can be converted into a heap by adjusting the heaps rooted at each internal node of the complete binary tree starting at the node ⌊(n - 1) /2⌋, and doing this adjustment up to the root node (root node is at index 0) in the order ⌊(n - 1)/2⌋, ⌊(n - 3)/ 2⌋, ....., 0. The time required to construct a heap in this manner is

O(log n) | |

O(n) | |

O (n log log n) | |

O(n log n) |

**Binary Trees**

**GATE-IT-2004**

**Discuss it**

Question 20 Explanation:

The above statement is actually algorithm for building a Heap of an input array A.

BUILD-HEAP(A) heapsize := size(A); for i := floor(heapsize/2) downto 1 do HEAPIFY(A, i); end for ENDUpper bound of time complexity is O(n) for above algo See- http://www.geeksforgeeks.org/g-fact-85/

Question 21 |

In a binary tree, for every node the difference between the number of nodes in the left and right subtrees is at most 2. If the height of the tree is h > 0, then the minimum number of nodes in the tree is:

2 ^{h - 1} | |

2 ^{h - 1} + 1 | |

2 ^{h} - 1 | |

2 ^{h} |

**Binary Trees**

**Gate IT 2005**

**Discuss it**

Question 21 Explanation:

Let there be n(h) nodes at height h. In a perfect tree where every node has exactly two children, except leaves, following recurrence holds. n(h) = 2*n(h-1) + 1 In given case, the numbers of nodes are two less, therefore n(h) = 2*n(h-1) + 1 - 2 = 2*n(h-1) - 1 Now if try all options, only option (b) satisfies above recurrence. Let us see option (B) n(h) = 2^{h - 1}+ 1 So if we substitute n(h-1) = 2^{h-2}+ 1, we should get n(h) = 2^{h-1}+ 1 n(h) = 2*n(h-1) - 1 = 2*(2^{h-2}+ 1) -1 = 2^{h-1}+ 1.

Question 22 |

Breadth First Search (BFS) is started on a binary tree beginning from the root vertex. There is a vertex t at a distance four from the root. If t is the n-th vertex in this BFS traversal, then the maximum possible value of n is ________
[This Question was originally a Fill-in-the-blanks Question]

15 | |

16 | |

31 | |

32 |

**Binary Trees**

**GATE-CS-2016 (Set 2)**

**Discuss it**

Question 22 Explanation:

It would be node number 31 for given distance 4.
For example if we consider at distance 2, below highlighted node G can be the farthest node at position 7.

A / \ B C / \ / \ D E FG

Question 23 |

In a binary tree, the number of internal nodes of degree 1 is 5, and the number of internal nodes of degree 2 is 10. The number of leaf nodes in the binary tree is

10 | |

11 | |

12 | |

15 |

**Binary Trees**

**GATE IT 2006**

**Discuss it**

Question 23 Explanation:

In a binary tree, the number of leaf nodes is always 1 more than number of internal nodes with 2 children, refer http://www.geeksforgeeks.org/handshaking-lemma-and-interesting-tree-properties/
So,
Number of Leaf Nodes = Number of Internal nodes with 2 children + 1
Number of Leaf Nodes = 10 + 1
Number of Leaf Nodes = 11

Question 24 |

The following three are known to be the preorder, inorder and postorder sequences of a binary tree. But it is not known which is which.

MBCAFHPYK

KAMCBYPFH

MABCKYFPH

Pick the true statement from the following.

MBCAFHPYK

KAMCBYPFH

MABCKYFPH

Pick the true statement from the following.

I and II are preorder and inorder sequences, respectively | |

I and III are preorder and postorder sequences, respectively | |

II is the inorder sequence, but nothing more can be said about the other two sequences | |

II and III are the preorder and inorder sequences, respectively |

**Binary Trees**

**Tree Traversals**

**Gate IT 2008**

**Discuss it**

Question 24 Explanation:

The approach to solve this question is to first find 2 sequences whose first and last element is same. The reason being first element in the Pre-order of any binary tree is the root and last element in the Post-order of any binary tree is the root.
Looking at the sequences given,
Pre-order = KAMCBYPFH
Post-order = MBCAFHPYK
Left-over sequence MABCKYFPH will be in order.
Since we have all the traversals identified, let's try to draw the binary tree if possible.
I. Post order

II. Pre order

III. Inorder This solution is contributed by

II. Pre order

III. Inorder This solution is contributed by

**Pranjul Ahuja.**Question 25 |

A binary tree with n > 1 nodes has n

n

_{1}, n_{2}and n_{3}nodes of degree one, two and three respectively. The degree of a node is defined as the number of its neighbors.n

_{3}can be expressed asn _{1} + n_{2} - 1 | |

n _{1} - 2 | |

[((n _{1} + n_{2})/2)] | |

n _{2} - 1 |

**Binary Trees**

**Gate IT 2008**

**Discuss it**

Question 26 |

A binary tree with n > 1 nodes has n

Starting with the above tree, while there remains a node v of degree two in the tree, add an edge between the two neighbors of v and then remove v from the tree. How many edges will remain at the end of the process?

_{1}, n_{2}and n_{3}nodes of degree one, two and three respectively. The degree of a node is defined as the number of its neighbors.Starting with the above tree, while there remains a node v of degree two in the tree, add an edge between the two neighbors of v and then remove v from the tree. How many edges will remain at the end of the process?

2 * n _{1} - 3 | |

n _{2} + 2 * n_{1} - 2 | |

n _{3} - n_{2} | |

n _{2} + n_{1} - 2 |

**Binary Trees**

**Gate IT 2008**

**Discuss it**

There are 26 questions to complete.