-1

I'm learning about AVL trees in class. Then I try to create one tree and then delete the nodes. But I don't know what method to choose to balance the tree correctly.

Ex. 22,13,19,20,50,18,33,49
Then subtract 20.

This is my tree with 20 removed, but not balanced at 22, but I'm not going to balance it using SLL or DRL methods.

    19  
 /      \  
13       22  <-----here  
\          \ 
18         49  
          /  \ 
         33  50

I want to know how to adjust the balance. Please give me some advice. Thank you.

jabaa
  • 5,844
  • 3
  • 9
  • 30
Eris
  • 1
  • 3
  • 1
    You could right rotate the node containing 49 and then left rotate the node containing 22. What is SLL and DRL? I recommend avoiding abbreviations. Not only that I don't know them, I can't even find them in the Internet. – jabaa Aug 30 '23 at 16:01
  • @trincot I meant right rotate to 19 - 22 - 33 - 49 - 50. The final result is a left rotation, but I split it into one right rotation and one left rotation. – jabaa Aug 30 '23 at 16:08
  • @jabaa What you mentioned is the DRL itself. That is, turn right first. Then turn left but i just want to know Can it be rotated left only or not? (rotate left for 49 and above are the parent nodes) Sorry that I'm not good at communicating. – Eris Aug 30 '23 at 16:13
  • Can you add a link to these abbreviations? You can left rotate the node containing 22. trincot removed the link. It contains a description. I try to find it: https://en.m.wikipedia.org/wiki/Tree_rotation#Illustration – jabaa Aug 30 '23 at 16:23
  • I don't see any need for a right rotation? – trincot Aug 30 '23 at 16:31
  • @trincot No, there is no need. But it's simpler to do it in the head. The left rotation in your answer is better, but less intuitive. – jabaa Aug 30 '23 at 16:36
  • You need a "double rotation" (the combination of a left then right roations, or right then left rotations). – Jean-Baptiste Yunès Aug 31 '23 at 14:36

1 Answers1

1

Indeed, if you insert those values in that order into an empty AVL tree, and then delete value 20, you get into that situation:

       __19__  
      /      \  
    13        22  <-----    
      \         \ 
       18        49  
                /  \ 
              33    50

While rebalancing you always start at the current node where the deletion took place, as this is the lowest point in the tree that could have an imbalance.

The principle is that you solve that imbalance, and then check the ancestors, working your way up the tree towards the root until there is no more imbalance. This is called retracing.

Although one could argue that a rotation at a higher level could sometimes also work, that is not how it is done.

In this case the subtree rooted at 22 is not in balance.

To restore the balance, we need a simple rotation to the left (i.e. anti-clockwise) at node 22, lifting up its right child.

See Wikipedia on tree rotations which has this image for illustrating simple rotations:

enter image description here

In your case you'll want to rotate left, so we start at the right side of the above image, with P=22, Q=49, B=33, and after the rotation you'll get the left-side situation:

       __19__  
      /      \  
    13        49  <----- after rotation-to-left
      \      /  \ 
       18  22    50  
             \ 
              33

And now the node 19 would be checked (as we work upwards), but its balance factor is fine, so no more action is needed.

trincot
  • 317,000
  • 35
  • 244
  • 286