algoritmauzmanı
Sign in
TREN
7.3AVL Tree
Lesson 7.3.4

Double rotations: LR and RL

The right and left rotations work when the extra height is on the outside: on the left of the left child or the right of the right child. If it is on the inside, say on the right of the left child, a single rotation only tips the tree over to the other side. This lesson solves those two cases, LR and RL, with two rotations and gathers all four cases into one rebalance function.

What you will learn
  • How to tell the four imbalance cases (LL, RR, LR, RL) apart from the balance factors
  • How a double rotation is made of two single rotations
  • The rebalance function that insertion and deletion will use
7 minTime O(1)Memory O(1)C · Java · Python

1Idea

Let 10 be on the left of 30 and 20 on the right of 10. 30 has balance factor 2, but its left child 10 has -1: the extra height is on the right of the left child. This is called left-right, LR. If you rotate right at 30, 10 moves up to the root, 30 goes down to its right and 20 moves to the left of 30: now the tree leans right and the root's balance factor is -2. A single rotation only mirrors the problem.

The fix takes two steps. First you rotate the left child left: 20 moves up and 10 goes down to its left. Now 20 is on the left of 30 and 10 on the left of 20: the familiar LL case. Then you rotate right at 30 and 20 moves up to the root. The middle value, 20, always ends up on top.

The RL case is the mirror of this: the node has balance factor -2 and its right child has 1. First the right child is rotated right, then the node is rotated left:

103020
RL: 30 on the right of 10, 20 on the left of 30. 10 has balance factor -2 and 30 has 1.
102030
First step: a right rotation at 30. The case turned into RR.
201030
Second step: a left rotation at 10. The middle value 20 moved up to the root.
CaseNode's balance factorChild's balance factorWhat is done
LL2Left child ≥ 0Right rotation at the node
LR2Left child -1Left at the left child, then right at the node
RR-2Right child ≤ 0Left rotation at the node
RL-2Right child 1Right at the right child, then left at the node

rebalance codes this table. It first updates the node's height and looks at its balance factor. If the node is left-heavy, it checks which way the left child leans, rotates the child first if needed, then rotates the node. If the node is balanced, it returns it unchanged. Insertion and deletion will call this function at every node on the way back up.

If the child's balance factor is 0, it is treated like LL or RR: one rotation is enough. This never happens after an insertion, only after a deletion; that is why the condition is written as < 0 and > 0, not <= 0.

2Step by step

The drawing calls rebalance(30). 10 is on the left of 30 and 20 on the right of 10: the LR case.

30h=2 df=21020n
  1. update(n) finds height 2 and balance factor 2 for 30: left-heavy.
  2. The left child 10 has balance factor -1: the extra height is inside, on the right of the left child. This is the LR case.
  3. First step: n->left = rotateLeft(n->left). 20 moved up and 10 went down to its left. The case turned into LL.
  4. Second step: return rotateRight(n). 20 moved up to the root and 30 went down to its right.
  5. Result: 20 at the root, height 1, every balance factor 0. The caller links this new root to its parent.

struct AVLNode {    int data;    int height;    struct AVLNode* left;    struct AVLNode* right;}; int height(struct AVLNode* n) {    return n == NULL ? -1 : n->height;           // an empty subtree is -1} void update(struct AVLNode* n) {    int l = height(n->left);    int r = height(n->right);    n->height = 1 + (l > r ? l : r);              // one more than the taller child} int balanceFactor(struct AVLNode* n) {    return height(n->left) - height(n->right);} struct AVLNode* rotateRight(struct AVLNode* y) {    // 7.3.2    struct AVLNode* x = y->left;    y->left = x->right;    x->right = y;    update(y);    update(x);    return x;} struct AVLNode* rotateLeft(struct AVLNode* x) {     // 7.3.3    struct AVLNode* y = x->right;    x->right = y->left;    y->left = x;    update(x);    update(y);    return y;} struct AVLNode* rebalance(struct AVLNode* n) {    update(n);    int bf = balanceFactor(n);    if (bf > 1) {                                // left-heavy        if (balanceFactor(n->left) < 0)          // LR: first rotate the left child left            n->left = rotateLeft(n->left);        return rotateRight(n);                   // LL, or the second step of LR    }    if (bf < -1) {                               // right-heavy        if (balanceFactor(n->right) > 0)         // RL: first rotate the right child right            n->right = rotateRight(n->right);        return rotateLeft(n);                    // RR, or the second step of RL    }    return n;                                    // balanced: nothing changes}
class AVLNode {    int data;    int height;    AVLNode left, right;    AVLNode(int data) { this.data = data; }} int height(AVLNode n) {    return n == null ? -1 : n.height;            // an empty subtree is -1} void update(AVLNode n) {    int l = height(n.left);    int r = height(n.right);    n.height = 1 + Math.max(l, r);                // one more than the taller child} int balanceFactor(AVLNode n) {    return height(n.left) - height(n.right);} AVLNode rotateRight(AVLNode y) {                    // 7.3.2    AVLNode x = y.left;    y.left = x.right;    x.right = y;    update(y);    update(x);    return x;} AVLNode rotateLeft(AVLNode x) {                     // 7.3.3    AVLNode y = x.right;    x.right = y.left;    y.left = x;    update(x);    update(y);    return y;} AVLNode rebalance(AVLNode n) {    update(n);    int bf = balanceFactor(n);    if (bf > 1) {                                // left-heavy        if (balanceFactor(n.left) < 0)           // LR: first rotate the left child left            n.left = rotateLeft(n.left);        return rotateRight(n);                   // LL, or the second step of LR    }    if (bf < -1) {                               // right-heavy        if (balanceFactor(n.right) > 0)          // RL: first rotate the right child right            n.right = rotateRight(n.right);        return rotateLeft(n);                    // RR, or the second step of RL    }    return n;                                    // balanced: nothing changes}
class AVLNode:    def __init__(self, data):        self.data = data        self.height = 0        self.left = None        self.right = None def height(n):    return -1 if n is None else n.height  # an empty subtree is -1 def update(n):    l = height(n.left)    r = height(n.right)    n.height = 1 + max(l, r)  # one more than the taller child def balance_factor(n):    return height(n.left) - height(n.right) def rotate_right(y):  # 7.3.2    x = y.left    y.left = x.right    x.right = y    update(y)    update(x)    return x def rotate_left(x):  # 7.3.3    y = x.right    x.right = y.left    y.left = x    update(x)    update(y)    return y def rebalance(n):    update(n)    bf = balance_factor(n)    if bf > 1:  # left-heavy        if balance_factor(n.left) < 0:  # LR: first rotate the left child left            n.left = rotate_left(n.left)        return rotate_right(n)  # LL, or the second step of LR    if bf < -1:  # right-heavy        if balance_factor(n.right) > 0:  # RL: first rotate the right child right            n.right = rotate_right(n.right)        return rotate_left(n)  # RR, or the second step of RL    return n  # balanced: nothing changes

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(1)

At most two rotations and a few height updates: O(1).

Memory
O(1)

A constant number of local variables: O(1).

rebalance reads the heights stored in the nodes; it does not walk the subtrees. Its cost does not depend on the size of the tree.

4Common mistake

A common mistake is rotating right straight away in the LR case. The root changes, but the tree is not balanced:

// 30, 10, 20 inserted: LRroot = rotateRight(root);   // root 10, 30 on its right, 20 on the left of 30// 10 now has balance factor -2: the tree leans the other way

You must look at which way the child leans first: if the node is left-heavy and its left child is right-heavy, two rotations are done. Another mistake is forgetting to store the first rotation's return value in n->left; then 30 still points to 10 and 20 is lost.

5Try it

50 has balance factor -2 and its right child 70 has 1. Which rotations are done?

Show the answer

The answer is B. The node is right-heavy and the right child is left-heavy: the RL case. First the right child 70 is rotated right, which turns the case into RR; then 50 is rotated left.

When 30, 10, 20 are inserted into an empty AVL tree, which value is the root?

Show the answer

The answer is B. Inserting 20 creates the LR case at 30. In a double rotation the middle value moves up to the root: 20, with 10 on its left and 30 on its right.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.