3

I'm working on an AVL tree assignment and I have a quick question about their definition - we're given a sorted list, and we have to generate an AVL tree from it in O(n) time. I've completed this (thanks to other help from StackOverflow!), but my result, while a valid AVL tree, is different from the result of the example provided. Are multiple AVL trees able to be generated from the same sorted list?

Thanks!

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
Jake
  • 3,142
  • 4
  • 30
  • 48

2 Answers2

3

Yes. Consider the degenerate case of a tree with only two nodes. In this case, either node can be the root, and the other will be a leaf. The two are equivalent as far as overall balance goes.

enter image description here

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Ah perfect, this was the tiny concept I was missing. There are so many things to learn with these trees! Thank you so much for your help! – Jake Oct 30 '11 at 20:16
2

Yes, for instance, these are two possible AVL trees for <1,2,3,4,5>:

(2 1 (3 4 5))

and

(4 (2 1 3) 5)

where (a T1 T2) denotes a tree with root a, left tree T1 and left right T2.

anumi
  • 3,869
  • 2
  • 22
  • 22