Question

Today I was studying AVL trees in Data Structures but got stuck in understanding LR and RL rotations. LL and RR rotations are quite intuitive and so easy to remember, but it seems to me that LR and RL rotation do not follow common sense, so I am having a hard time in remembering them. Should these rotations be crammed or is there any way to understand them? The book I am reading (Data Structures by Seymoure Lipschutz) says LR rotation is a combination of RR rotation followed by LL rotation. But I am unable to connect it. Here is the picture depicted in that book:

enter image description here

Between the second image and the final image what is happening, please explain if possible with this picture. I think if I understand LR then automatically will understand RL as the both are mirror image to one-another.

Was it helpful?

Solution

You don't understand it because it isn't correct, as pictured. It's not a even valid binary search tree. 37 cannot be the right (greater) child of 76 because it is less.

Initial insert

└── 44
    ├── (L) 30
    │   ├── (L) 16
    │   └── (R) 39
    │       └── (L) 37
    └── (R) 76

After left rotation on (30): (39) get rotated into it's parent's [30] spot, (39)'s child [37] becomes 30's child.

└── 44
    ├── (L) 39
    │   └── (L) 30
    │       ├── (L) 16
    │       └── (R) 37
    └── (R) 76

After a right rotation on (39): (39) is at the top of the tree and (44) becomes his right child.

└── 39
    ├── (L) 30
    │   ├── (L) 16
    │   └── (R) 37
    └── (R) 44
        └── (R) 76
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top