Skip to main content

GATE CSE 2016Data Structures › Trees & Binary Search TreesVerified

GATE CSE 2016 PYQ: The number of ways in which the numbers 1 , 2 , 3 , 4 , 5 , 6 , 7 1, 2, 3, 4, 5, 6, 7 1 , 2 , 3 , 4 , 5 , 6…

The number of ways in which the numbers 1,2,3,4,5,6,71, 2, 3, 4, 5, 6, 7 can be inserted in an empty binary search tree, such that the resulting tree has height 6, is _________.

Note: The height of a tree with a single node is 0.

    Answer

    64

    Approach

    A binary search tree on 7 nodes with height 6 must be a single chain: all 7 nodes lie on the longest root-to-leaf path. Count the insertion orders that force this chain by choosing, at each step, the current minimum or maximum remaining key.

    For the tree to have height 6 with only 7 nodes, it must be completely skewed, i.e. a single chain. The root must be an extreme value (1 or 7), giving 2 choices, and then each subsequent insertion must be either the current minimum or maximum of the remaining keys, giving 2 choices for each of the next 5 insertions; the final insertion is forced. Thus the answer is \(2^6 = 64\).

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.