Lecture No 16 - AVL Tree
Lecture No 16 - AVL Tree
AVL Tree
• AVL tree is a self-balancing binary search tree in which each node
maintains extra information called a balance factor whose value is
either -1, 0 or +1.
• AVL tree got its name after its inventor Georgy Adelson-Velsky and
Landis.
Balance Factor
• The balance factor of a node in an AVL tree is the difference between
the height of the left subtree and that of the right subtree of that node.
Updating the balance factor after insertion Balancing the tree with rotation
Insertion
Remove w Update bf
Deletion
• Rebalance the tree if the balance factor of any of the nodes is not equal to -1, 0 or 1.
• If balanceFactor of currentNode > 1,
• If balanceFactor of leftChild >= 0, do right rotation.
• Else do left-right rotation.
• If balanceFactor of currentNode < -1, Avl tree final
• If balanceFactor of rightChild <= 0, do left rotation.
• Else do right-left rotation.
#include <iostream> // New node creation
using namespace std; Node *newNode(int key) {
Node *node = new Node();
Code class Node { node->key = key;
node->left = NULL;
public:
int key; node->right = NULL;
Node *left; node->height = 1;
Node *right; return node;
int height; }
}; // Rotate right
Node *rightRotate(Node *y) {
int max(int a, int b); Node *x = y->left;
Node *T2 = x->right;
// Calculate height x->right = y;
int height(Node *N) { y->left = T2;
if (N == NULL) y->height = max(height(y->left),
return 0; height(y->right)) +
return N->height; 1;
} x->height = max(height(x->left),
height(x->right)) +
int max(int a, int b) { 1;
return (a > b) ? a : b; return x;
} }
// Rotate left // Insert a node
Node *leftRotate(Node *x) { Node *insertNode(Node *node, int key) {
Node *y = x->right; // Find the correct postion and insert the node
Node *T2 = y->left; if (node == NULL)
y->left = x; return (newNode(key));
x->right = T2; if (key < node->key)
x->height = max(height(x->left), node->left = insertNode(node->left, key);
height(x->right)) + else if (key > node->key)
1; node->right = insertNode(node->right, key);
y->height = max(height(y->left), else
height(y->right)) + return node; }
1; if (balanceFactor < -1) {
return y; // Update the balance factor of each node and if (key > node->right->key) {
} // balance the tree return leftRotate(node);
node->height = 1 + max(height(node->left), } else if (key < node->right->key) {
height(node->right)); node->right = rightRotate(node->right);
// Get the balance factor of each int balanceFactor = getBalanceFactor(node); return leftRotate(node);
node if (balanceFactor > 1) { }
int getBalanceFactor(Node *N) { if (key < node->left->key) { }
if (N == NULL) return rightRotate(node); return node;
return 0; } else if (key > node->left->key) { }
return height(N->left) - node->left = leftRotate(node->left);
height(N->right); return rightRotate(node);
} }
// Node with minimum value if ((root->left == NULL) || (root->right == NULL)) {
Node *nodeWithMimumValue(Node *node) { Node *temp = root->left ? root->left : root->right;
Node *current = node; if (temp == NULL) {
while (current->left != NULL) temp = root;
current = current->left; root = NULL;
return current; } else
} *root = *temp;
free(temp);
} else {
Node *temp = nodeWithMimumValue(root->right);
// Delete a node root->key = temp->key;
Node *deleteNode(Node *root, int key) { root->right = deleteNode(root->right, temp->key);
// Find the node and delete it }
if (root == NULL) }
return root;
if (key < root->key)
root->left = deleteNode(root->left, key); if (root == NULL)
else if (key > root->key) return root;
root->right = deleteNode(root->right, key);
else {
// Update the balance factor of each node and // Print the tree
// balance the tree void printTree(Node *root) {
root->height = 1 + max(height(root->left), height(root->right)); cout << root->key << endl;
int balanceFactor = getBalanceFactor(root); printTree(root->left);
if (balanceFactor > 1) { printTree(root->right);
if (getBalanceFactor(root->left) >= 0) { }
return rightRotate(root); }
} else {
root->left = leftRotate(root->left); int main() {
return rightRotate(root); Node *root = NULL;
} root = insertNode(root, 33);
} root = insertNode(root, 13);
if (balanceFactor < -1) { root = insertNode(root, 53);
if (getBalanceFactor(root->right) <= 0) { root = insertNode(root, 9);
return leftRotate(root); root = insertNode(root, 21);
} else { root = insertNode(root, 61);
root->right = rightRotate(root->right); root = insertNode(root, 8);
return leftRotate(root); root = insertNode(root, 11);
} printTree(root);
} root = deleteNode(root, 13);
return root; cout << "After deleting " << endl;
} printTree(root);
}