More than one rotation needed to balance an AVL Tree?

雨燕双飞 提交于 2019-11-30 09:55:45

For insert 1 rotation is at most.
For delete the number of rotation is bounded by O(log(n)). Log(n) is the height of the tree.
More explanation on AVL deletion. When you delete a node from AVL you might cause the tree unbalanced, which you have to trace back to the point where it is unbalanced. If the unbalanced point is the root. You have to rebalance the tree from top to bottom.

For insertion I believe one is enough.

but for deletion: consider this tree:

                 50
              /      \
            25       75
           /   \    /   \
          15   40  60    80
               /  /  \    \
              35 55  65   90
                     /
                    62

delete 15 , first the 25's balance factor is destructed, one rotation:

                 50
              /      \
            35       75
           /   \    /   \
          25  40  60    80
                  /  \    \
                 55  65   90
                     /
                    62

but still, we have to check, now the tree's root's balance factor is destructed, have to be rotated again:

                 60
              /      \
            50       75
           /   \     /  \
          35   55   65   80
         /  \       /     \
        25  40     62     90
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!