+1 vote
in Data Structures & Algorithms I by (110k points)
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 Answer

+1 vote
by (737k points)
selected by
 
Best answer
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.

Related questions

Welcome to TalkJarvis QnA, a question-answer community website for the people by the people. On TalkJarvis QnA you can ask your doubts, curiosity, questions and whatever going in your mind either related to studies or others. Experts and people from different fields will answer.

Categories

...