Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
AVL Tree class in Javascript
An AVL Tree is a self-balancing binary search tree where the heights of left and right subtrees differ by at most one. This implementation provides insertion with automatic rebalancing through rotations.
Complete AVL Tree Implementation
class AVLTree {
constructor() {
// Initialize a root element to null
this.root = null;
}
getBalanceFactor(root) {
return this.getHeight(root.left) - this.getHeight(root.right);
}
getHeight(root) {
let height = 0;
if (root === null || typeof root == "undefined") {
height = -1;
} else {
height = Math.max(this.getHeight(root.left), this.getHeight(root.right)) + 1;
}
return height;
}
insert(data) {
let node = new this.Node(data);
// Check if the tree is empty
if (this.root === null) {
// Insert as the first element
this.root = node;
} else {
this.root = insertHelper(this, this.root, node);
}
}
inOrder() {
inOrderHelper(this.root);
}
}
AVLTree.prototype.Node = class {
constructor(data, left = null, right = null) {
this.data = data;
this.left = left;
this.right = right;
}
};
function insertHelper(self, root, node) {
if (root === null) {
root = node;
} else if (node.data < root.data) {
// Go left!
root.left = insertHelper(self, root.left, node);
// Check for balance factor and perform appropriate rotation
if (root.left !== null && self.getBalanceFactor(root) > 1) {
if (node.data < root.left.data) {
root = rotationLL(root);
} else {
root = rotationLR(root);
}
}
} else if (node.data > root.data) {
// Go Right!
root.right = insertHelper(self, root.right, node);
// Check for balance factor and perform appropriate rotation
if (root.right !== null && self.getBalanceFactor(root) < -1) {
if (node.data > root.right.data) {
root = rotationRR(root);
} else {
root = rotationRL(root);
}
}
}
return root;
}
function inOrderHelper(root) {
if (root !== null) {
inOrderHelper(root.left);
console.log(root.data);
inOrderHelper(root.right);
}
}
function rotationLL(node) {
let tmp = node.left;
node.left = tmp.right;
tmp.right = node;
return tmp;
}
function rotationRR(node) {
let tmp = node.right;
node.right = tmp.left;
tmp.left = node;
return tmp;
}
function rotationLR(node) {
node.left = rotationRR(node.left);
return rotationLL(node);
}
function rotationRL(node) {
node.right = rotationLL(node.right);
return rotationRR(node);
}
Usage Example
// Create AVL tree and insert values
let avl = new AVLTree();
avl.insert(10);
avl.insert(5);
avl.insert(15);
avl.insert(3);
avl.insert(7);
console.log("In-order traversal of AVL tree:");
avl.inOrder();
In-order traversal of AVL tree: 3 5 7 10 15
Key Features
Balance Factor: The difference between left and right subtree heights. Must be -1, 0, or 1.
Rotations: Four types maintain balance - LL (left-left), RR (right-right), LR (left-right), and RL (right-left) rotations.
Height Calculation: Recursively computes the maximum depth from root to leaf nodes.
Rotation Types
| Rotation | When Used | Description |
|---|---|---|
| LL (Left-Left) | Balance factor > 1, left-heavy | Right rotation on unbalanced node |
| RR (Right-Right) | Balance factor | Left rotation on unbalanced node |
| LR (Left-Right) | Left subtree is right-heavy | Left rotation on left child, then right rotation |
| RL (Right-Left) | Right subtree is left-heavy | Right rotation on right child, then left rotation |
Conclusion
This AVL Tree implementation ensures O(log n) operations through automatic balancing. The rotation functions maintain the tree's balanced property after each insertion, providing efficient search and insertion performance.
