# Binary Trees

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:

Number of nodes of binary tree will be maximum only when tree is

**full complete**, therefore answer is 2^(i)-1 So, option (A) is true.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. We can find the number of binary tree by Catalan number number: Here n = 3 Number of binary tree = (

^{2n}C_{n})/ n+1 = (^{2*3}C_{3})/ 4+1 = 5. So, option (B) is correct.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 this post)

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 . 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

There are 52 questions to complete.