7.3AVL Tree
Left rotation (the RR case)
A left rotation is the mirror image of the right rotation. A node with balance factor -2 has a right side two levels taller; if the extra height is on the right of the right child, the RR case, the right child moves up and the node goes down to its left. The code is the same with left and right swapped.
- How to recognise the RR case from the balance factors
- Which links change in a left rotation and where T2 ends up
- Why inserting values in increasing order always calls for left rotations
1Idea
This time call the node whose balance broke x and its right child y. If x has balance factor -2 and y has -1 or 0, the extra height is on the right side of the right child: right-right, or RR for short. The fix is a single left rotation: y moves up and x goes down to its left.
The left subtree of y, T2, moves to the right of x. This keeps the BST rule: the values in T2 are larger than x (it was on x's right) and smaller than y (it was on y's left). The links that change mirror those of the right rotation:
| Link | Before | After |
|---|---|---|
y->left | T2 | x |
x->right | y | T2 |
| The parent's link | x | y (through the return value) |
The RR case shows up most when values arrive in increasing order. After inserting 10, 20, 30, 20 is to the right of 10 and 30 to the right of 20, and 10 has balance factor -2. An ordinary binary search tree starts turning into a chain here; the AVL tree rotates left at 10 and 20 moves up to the root.
In exams the names of the right and left rotations can get mixed up between books. Some name a rotation after the direction of the child that moves up. The safest way is to also write which node moves up: "the left rotation that moves 40 up", for example.
2Step by step
In the drawing 20 has balance factor -2 and its right child 40 has -1: the RR case. T2 is the left subtree of 40, that is 30.
- Start.
xis 20, whose balance broke. Its right side has height 2 and its left side 0. y = x->right: the node that will move up is 40.T2 = y->left: the left subtree of 40, which is 30, is kept in a variable.y->left = x: 20 became the left child of 40. 20's right link still points to 40 (dashed).x->right = T2: 30 moved to the right of 20. The tree has its new shape: 40 at the top.update(x): the children of 20 are 10 and 30, both leaves. Height 1, balance factor 0.update(y): the children of 40 are 20 (height 1) and 50 (height 1). Height 2, balance factor 0.return y: the new root of the subtree is 40. The inorder order 10, 20, 30, 40, 50, 60 is kept.
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} struct AVLNode* rotateLeft(struct AVLNode* x) { struct AVLNode* y = x->right; // the right child will move up struct AVLNode* T2 = y->left; // y's left subtree will change hands y->left = x; // x goes down to the left of y x->right = T2; // T2 moves to the right of x update(x); // first x, which is now lower update(y); // then the new root y return y; // the new root of the subtree}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} AVLNode rotateLeft(AVLNode x) { AVLNode y = x.right; // the right child will move up AVLNode T2 = y.left; // y's left subtree will change hands y.left = x; // x goes down to the left of y x.right = T2; // T2 moves to the right of x update(x); // first x, which is now lower update(y); // then the new root y return y; // the new root of the subtree}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 rotate_left(x): y = x.right # the right child will move up T2 = y.left # y's left subtree will change hands y.left = x # x goes down to the left of y x.right = T2 # T2 moves to the right of x update(x) # first x, which is now lower update(y) # then the new root y return y # the new root of the subtreeThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Two links change and two heights are updated: O(1).
Two local variables: O(1).
It costs the same as a right rotation. Insertion and deletion in an AVL tree stay O(log n) because rotations finish in constant time.
4Common mistake
A common mistake is updating the heights in the wrong order:
update(y); // y uses the old height of x (2): gets 3update(x); // x is right: 1y is now above x and computes its own height from x. If x has not been updated yet, y comes out one too high, and the balance factors of the nodes above are computed wrongly. The rule: first the node that went down, then the node that went up.
5Try it
When 10, 20, 30 are inserted into an empty AVL tree in that order, which rotation is done?
The answer is B. After 30 is inserted, 10 has balance factor -2 and 20 has -1: the RR case. A left rotation is done at 10, whose balance broke, and 20 moves up to the root.
Show the answer
The answer is B. After 30 is inserted, 10 has balance factor -2 and 20 has -1: the RR case. A left rotation is done at 10, whose balance broke, and 20 moves up to the root.
In a left rotation, where does the old left subtree of y (T2) go?
The answer is A. x goes down to the left of y, and the right side of x becomes empty. The values in T2 are larger than x and smaller than y, so they fit on the right of x.
Show the answer
The answer is A. x goes down to the left of y, and the right side of x becomes empty. The values in T2 are larger than x and smaller than y, so they fit on the right of x.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.