1.

Consider the original array 17 8 12 4 26.How many comparisons are needed to construct the BST on the original array?(a) 5(b) 4(c) 7(d) 10This question was addressed to me at a job interview.The above asked question is from Sorting in chapter Sorting of Data Structures & Algorithms II

Answer»

The correct answer is (d) 10

Explanation: Original ARRAY is 17 8 12 4 26. The BST built on this array is shown in the figure below.

To built the BST, we travel down the TREE until a leaf is reached. THEREFORE, for every element we compare the element with the internal nodes until we the leaves and then once again compare the element with its parent to decide WHETHER it is right child or left child. So, for given array we need to perform 10 comparisons to build the BST.



Discussion

No Comment Found

Related InterviewSolutions