avlTree.definition
avlTree.descriptionPart1
avlTree.descriptionPart2
avlTree.keyPointsTitle
- avlTree.keyPoint1
- avlTree.keyPoint2
- avlTree.keyPoint3
- avlTree.keyPoint4
avlTree.rotationsTitle
LLavlTree.rotationLL
RRavlTree.rotationRR
LRavlTree.rotationLR
RLavlTree.rotationRL
avlTree.exampleTitle
300
200
400
100
250
350
500
avlTree.exampleCaption
avlTree.syntaxTitle
// Java
class Node {
int value, height;
Node left, right;
Node(int v) { value = v; height = 1; }
}
class AVLTree {
Node root;
int height(Node n) { return n == null ? 0 : n.height; }
int balance(Node n) { return height(n.left) - height(n.right); }
Node rightRotate(Node y) {
Node x = y.left;
y.left = x.right;
x.right = y;
y.height = 1 + Math.max(height(y.left), height(y.right));
x.height = 1 + Math.max(height(x.left), height(x.right));
return x;
}
}# Python
class Node:
def __init__(self, value):
self.value = value
self.height = 1
self.left = self.right = None
class AVLTree:
def height(self, n):
return n.height if n else 0
def balance(self, n):
return self.height(n.left) - self.height(n.right) if n else 0
def right_rotate(self, y):
x = y.left
y.left = x.right
x.right = y
y.height = 1 + max(self.height(y.left), self.height(y.right))
x.height = 1 + max(self.height(x.left), self.height(x.right))
return x// C++
struct Node {
int value, height;
Node *left, *right;
Node(int v) : value(v), height(1), left(nullptr), right(nullptr) {}
};
class AVLTree {
int height(Node* n) { return n ? n->height : 0; }
int balance(Node* n) { return height(n->left) - height(n->right); }
Node* rightRotate(Node* y) {
Node* x = y->left;
y->left = x->right;
x->right = y;
y->height = 1 + max(height(y->left), height(y->right));
x->height = 1 + max(height(x->left), height(x->right));
return x;
}
};// JavaScript
class Node {
constructor(value) {
this.value = value;
this.height = 1;
this.left = this.right = null;
}
}
class AVLTree {
height(n) { return n ? n.height : 0; }
balance(n) { return this.height(n?.left) - this.height(n?.right); }
rightRotate(y) {
const x = y.left;
y.left = x.right;
x.right = y;
y.height = 1 + Math.max(this.height(y.left), this.height(y.right));
x.height = 1 + Math.max(this.height(x.left), this.height(x.right));
return x;
}
}Interactive AVL Tree Visualization
🌲
Tree is empty. Insert a value to begin.
Nodes: 0Height: 0
Idle
Visiting
Found
Inserting
Removing
Unbalanced
Rotating
Balance Factor:
0 (Balanced)
+/-1 (Acceptable)
+/-2 (Needs Rotation)
Note: AVL trees maintain balance by ensuring the height difference between left and right subtrees is at most 1. Four rotation types (LL, RR, LR, RL) restore balance after insertions and deletions.
Time Complexity
| Operation | Average Case | Worst Case | Description |
|---|---|---|---|
| Insert | O(log n) | O(log n) | BST insert + max 2 rotations for rebalancing |
| Remove | O(log n) | O(log n) | BST delete + up to O(log n) rotations |
| Find | O(log n) | O(log n) | Binary search through balanced tree |
| Find Min | O(log n) | O(log n) | Traverse to leftmost node |
| Find Max | O(log n) | O(log n) | Traverse to rightmost node |
Guaranteed O(log n): AVL trees maintain strict balance, ensuring height is always O(log n).
Rotation cost: At most 2 rotations after insert, up to O(log n) rotations after delete.
Space complexity: O(n) for storing n nodes, O(log n) for recursive operations.
avlTree.continueLearning
enhanced.author.title
enhanced.author.writtenBy:enhanced.author.teamName
enhanced.author.reviewedBy:enhanced.author.reviewerName
enhanced.author.lastUpdated:February 2, 2026