GATE / CS ARCHIVE

GATE CSE 2016 SET 2 / QUESTION 40

GATE CSE 2016 Set 2 Q40

Data Structures · Binary Search & Balanced Trees · Full GATE CSE 2016 Set 2 paper

GATE 2016 Set 2, Q40 2 marks · NAT
Report issue

Binary Search & Balanced Trees

Question 40, GATE 2016: How many permutations of 1,…,7, inserted into an empty BST, produce height 6? Height counts edges, so a one-node tree has height 0.
Tap to enlarge
Answer & solution

Official answer: 64.0 : 64.0

Our answer: 64

Height6 with seven nodes means every node lies on one chain. The first inserted value must be the smallest or largest remaining key; otherwise both subtrees would be nonempty and the tree could not be a chain. Recursively choose either endpoint for each of the first six insertions, with the last forced. This gives 26=64 distinct insertion orders.

For a chain, all remaining keys must fall on one side of each successive root. Thus the smallest or largest key is chosen repeatedly. This counts insertion permutations, not merely final shapes. For three keys the valid orders are 123,132,312,321, illustrating the recurrence C(k)=2C(k−1), C(1)=1.

Original paper, page 36 · Answer key