简体   繁体   中英

Extra Cases in AVL Trees

AVL Trees seem to have four kinds of transformations: Left-Left, Left-Right, Right-Left, and Right-Right. However, it seems like there could be other circumstances as well. I submit this as Left-Balanced:

在此处输入图片说明

Neither Left nor Right rotations can balance this tree. What algorithm would one use to balance it?

Both LL and LR can be applied here

        50
       /  \
     40    55
    /  \
  35    45
  / \   / \
34  36 44 46

After first LR turn:

           50
          /  \
        45   55
       /  \
     40    46
    /  \
  35    44
  / \
34  36

After second LR turn:

        45
       /  \
     40    50
    / \    / \
  35  44  46  55
 / \
34  36

This is the valid AVL tree. Note that

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

You can also do the LL turn:

     40
    /  \
  35    50
  / \   / \
34  36 45  55
       /\
     44  46

Again this is the valid AVL tree.

I think you can't get the left-balance case. Because if you built the left-balance one, you may get the left-left or left-right first, then the tree would rotate and keep balance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM