7.3AVL Tree
Right rotation (the LL case)
If a node's balance factor has reached 2, its left side is two levels taller. There is no need to rebuild the tree: changing three links is enough. This is called a right rotation, and it is the first tool of the AVL tree. This lesson shows how a right rotation works when the left child's left side has grown, the LL case.
- How to recognise the LL case from the balance factors
- Which three links change in a right rotation and where the T2 subtree goes
- Why the heights are updated from the bottom up after the rotation
1Idea
Call the node whose balance broke y and its left child x. If y has balance factor 2 and x has 1 or 0, the extra height is on the left side of the left child. This is called left-left, or LL for short. The fix is a single rotation: x moves up and y goes down to its right.
One problem is left: x may already have a right subtree, call it T2. Once y moves to the right of x, there is no room for T2. But the left side of y is now empty, and T2 fits there exactly: every value in T2 is larger than x (it was on x's right) and smaller than y (it was on y's left).
| Link | Before | After |
|---|---|---|
x->right | T2 | y |
y->left | x | T2 |
| The parent's link | y | x (through the return value) |
The inorder order stays the same: first x's left, then x, then T2, then y, then y's right. A rotation leaves a binary search tree still a binary search tree. Only the shape changes: the left side gets one level shorter and the right side one level taller.
The function returns the new root of the subtree. The caller updates the parent's link, as in root->left = rotateRight(root->left);. The heights are updated in order too: first y, which went down, then x above it. In the opposite order x would use the old height of y.
A rotation changes only a few pointers; it does not care how many nodes the tree has. So it takes O(1) and does not add to the cost of insertion or deletion.
2Step by step
In the drawing 40 has balance factor 2 and its left child 20 has 1: the LL case. T2 is the right subtree of 20, that is 30. The pointer names are the variables in the code.
- Start.
yis 40, whose balance broke. Its left side has height 2 and its right side 0. x = y->left: the node that will move up is 20.T2 = x->right: the right subtree of 20, which is 30, is kept in a variable, because this link is about to be overwritten.x->right = y: 40 became the right child of 20. 40's left link still points to 20 (dashed); the next line fixes that.y->left = T2: 30 moved to the left of 40. The loop is gone and the tree has its new shape: 20 at the top.update(y): the children of 40 are 30 and 50, both leaves. Height 1, balance factor 0.update(x): the children of 20 are 10 (height 1) and 40 (height 1). Height 2, balance factor 0.return x: the new root of the subtree is 20. The inorder order is 5, 10, 20, 30, 40, 50; no value lost its place.
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* rotateRight(struct AVLNode* y) { struct AVLNode* x = y->left; // the left child will move up struct AVLNode* T2 = x->right; // x's right subtree will change hands x->right = y; // y goes down to the right of x y->left = T2; // T2 moves to the left of y update(y); // first y, which is now lower update(x); // then the new root x return x; // 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 rotateRight(AVLNode y) { AVLNode x = y.left; // the left child will move up AVLNode T2 = x.right; // x's right subtree will change hands x.right = y; // y goes down to the right of x y.left = T2; // T2 moves to the left of y update(y); // first y, which is now lower update(x); // then the new root x return x; // 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_right(y): x = y.left # the left child will move up T2 = x.right # x's right subtree will change hands x.right = y # y goes down to the right of x y.left = T2 # T2 moves to the left of y update(y) # first y, which is now lower update(x) # then the new root x return x # the new root of the subtreeThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
A rotation changes two links and updates the heights of two nodes. It does not depend on the size of the tree: O(1).
Two local variables are used: O(1).
In the LL case the rotated subtree gets one level shorter and returns to its height before the insertion. That is why one rotation is enough after an insertion.
4Common mistake
A common mistake is overwriting x->right without keeping T2:
struct AVLNode* x = y->left;x->right = y; // 30 is losty->left = x->right; // y->left = y: a node pointing to itselfThe second line deletes the only link to 30 and the third links y to itself. Another mistake is ignoring the return value: if you write rotateRight(root); and do not update the parent's link, the tree is still entered through the old root y, which is now lower down, and x with its left side is cut off.
5Try it
30, 20, 10 are inserted into an empty tree in that order. After a right rotation at 30, which value is the root?
The answer is B. After the insertions 20 is to the left of 30 and 10 is to the left of 20: the LL case. A right rotation moves the left child 20 up to the root; 10 stays on its left and 30 on its right.
Show the answer
The answer is B. After the insertions 20 is to the left of 30 and 10 is to the left of 20: the LL case. A right rotation moves the left child 20 up to the root; 10 stays on its left and 30 on its right.
In a right rotation, where does the old right subtree of x (T2) go?
The answer is B. y goes down to the right of x and its left side becomes empty. The values in T2 are larger than x and smaller than y; by the BST rule they fit exactly on the left of y.
Show the answer
The answer is B. y goes down to the right of x and its left side becomes empty. The values in T2 are larger than x and smaller than y; by the BST rule they fit exactly on the left of y.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.