1

I encountered the following question: The Height of a BST constructed from the following list of values 10 , 5 , 4 , 3 , 2 , 1 , 0 , 9 , 13 , 11 , 12 , 16 , 20 , 30 , 40 , 14 will be:

A) 5
B) 6
C) 7
D) 8

Now to certain point I can construct the Tree with no problem because there aren't many choices to insert the values so from the first value 10 to the eleventh value 12 my tree would go like this:

                             10
                           /    \
                          5      13
                         / \     /
                        4   9   11  
                       /         \
                      3           12
                     /
                    2
                   /
                  1
                 /
                0

But after that, now I have to add the value 16 and I have two choices to make it the right child of the value 12 or to make it the right child of the value 13 and the height would differ according to this choice, so what's the right approach here?

Miriam Arbaji
  • 319
  • 1
  • 3
  • 17
  • 1
    Is there any more information for this problem? Multiple unique BST can be constructed from a list of numbers such as the one provided. If there is another constraint or more info, that could help reveal more about the correct answer from those provided. – cischa Dec 05 '19 at 23:38
  • Not really no, I'm Studying for an upcoming exam and this is a question from a previous year, So there's some missing piece of information right?? – Miriam Arbaji Dec 05 '19 at 23:40
  • 1
    The question is what is the insertion algorithm? – ggorlen Dec 05 '19 at 23:42
  • @ggorlen they didn't say anything about the insertion algorithm! – Miriam Arbaji Dec 05 '19 at 23:44

1 Answers1

1

A binary search tree is a binary tree (that is, every node has at most 2 children) where for each node in the tree, all nodes in the left subtree rooted at that node are less than the root of the subtree and all nodes in the right subtree rooted at that node are greater than the root of the subtree.

Your walkthrough so far assumes a "naive" insertion algorithm that makes no attempt at balancing the tree and inserts the nodes in sequence by recursively comparing against each node from the root. Under typical circumstances, you would not want to build such an unbalanced tree because the performance of operations devolves into O(n) time instead of the optimal O(log(n)) time, which defeats the purpose of the BST structure.

Making 16 the right child of 12 would be an invalid choice because 16 as a left child of 13 violates the BST property. By definition, every insertion must go in one location, so there are no choices to be made, at least following this naive algorithm.

Following through with your approach, it should be clear that the final height (longest root-to-leaf path) will be 7 in any case due to the unbalanced left branch.

However, if the question is to find the height of an optimal tree, then the correct answer is 5. The below illustrates a complete tree (that is, every level is full except the bottom level, and that level has all elements to the left) that can be built from the given data:

               11
       +---------------+      
       4              16
    +------+      +-------+
    2      9     14       30
  +---+  +---+  +---+   +---+
  1   3  5  10 12   13 20   40
+--
0  

Take a look at self-balancing binary search tree data structures for more information on algorithms that can build such a tree.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Because making `16` the right child of a left child `12` would make it an indirect left child to `13` correct?? – Miriam Arbaji Dec 05 '19 at 23:46
  • Yeah, the definition of a BST is that every element in the left subtree of the root is less than the root and every element in the right subtree is greater than the root, for all nodes in the tree. – ggorlen Dec 05 '19 at 23:49
  • Thanks a lot! I didn't think of it this way – Miriam Arbaji Dec 05 '19 at 23:52
  • 1
    No problem. I updated the post to add more information. Using an optimal insertion algorithm, it's possible to build a tree of height 5, and it's also easy to see how 6 and 8 are possible heights as well. – ggorlen Dec 06 '19 at 00:10