Question

After reading about AVL trees I can't get one question out of my head. If we have a sorted list of numbers, e.g. [1,2,3,4,5] and we insert them into the AVL tree, wouldn't the tree stay unblanaced because it will go 1-2-3-4-5 (i.e. they will all become right child).

I am asking this because I know that in AVL tree for every internal node v of T, the heights of the children of v can differ by at most 1.

But if we have only 1 child for each node, how can we do this comparison?

Was it helpful?

Solution

An empty tree has height 0, so in your example after adding 1-2-3 the left child of 1 had height 0 and the right had 2, triggering a rotation that makes 2 the root.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top