0

I am wondering if there is fundamental issue for AVL tree re-balancing. According to several tutorials, for AVL insertion, maximum 2 rotates it can be balanced. However, it may depend what is called as balanced. Follow link to see the tree.

Originally it has 6 elements. Assume that we inserted the last value as 3 or 4.5 or 5.5 or 6.5. Anyway, it will be inserted on the left side of the bottom. As total 7 element tree, for perfect balancing, I will consider it has only 3 rows.

This will force the new root is 6 or 6.5 (if we insert 6.5). I really cannot figure out a way to rebalancing it within 2 rotations. If we only depends on the "balance" definition, 4-rows is still called balanced, but it will result more searching time.

Am I missing something?

In case the picture got deleted, below is a text version:

                               7
              5                                9
           4     6                         8        Empty_slot
       3 or 4.5 or 5.5 or 6.5

Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
Lino
  • 3
  • 1

1 Answers1

2

As you can read on wikipedia

In an AVL tree, the heights of the two child subtrees of any node differ by at most one

That doesn't imply that you have a complete tree ! See examples of balances tree in the wikipedia page linked.

So for any of the insertion you propose you tree will still be balanced (for the common definition of balanced) after the insertion

on the one side of the root you'll have the subtree

      5
  4       6        height 2 
3   #   #   #

and on the oder side you would have the subtree

  9
8   #              height 1

As thoses 2 subtrees only have a difference of height of 1, so it's ok ... and you can check that this property is true for all nodes

Amxx
  • 3,020
  • 2
  • 24
  • 45
  • Agree with you on that: it is still balanced, from AVL tree definition. My real question is if that means lots of empty spots when it goes to deeper? – Lino Apr 29 '15 at 19:01
  • The "balanced" aspect of AVL means that the number of empty spots is limited. It's a compromise between to much empty spots (which increases the traversal time) and doing to much rotations (which increases the insertion time) – Amxx Apr 29 '15 at 19:04
  • Thanks Amxx. I will buy it. – Lino May 01 '15 at 15:12