Given an empty AVL tree, how would you construct AVL tree when a set of numbers are given without performing any rotations?

Category: QuestionsGiven an empty AVL tree, how would you construct AVL tree when a set of numbers are given without performing any rotations?
Editor">Editor Staff asked 4 weeks ago

Given an empty AVL tree, how would you construct AVL tree when a set of numbers are given without performing any rotations?
 
(a) just build the tree with the given input
 
(b) find the median of the set of elements given, make it as root and construct the tree
 
(c) use trial and error
 
(d) use dynamic programming to build the tree
 
Question is taken from AVL Tree topic in portion Binary Trees of Data Structures & Algorithms I
 
This question was posed to me in an interview.

1 Answers
Editor">Editor Staff answered 4 weeks ago

Right option is (b) find the median of the set of elements given, make it as root and construct the tree
 
Easy explanation – Sort the given input, find the median element among them, make it as root and construct left and right subtrees with elements lesser and greater than the median element recursively. this ensures the subtrees differ only by height 1.