0% found this document useful (0 votes)
15 views

Lecture No 16 - AVL Tree

Uploaded by

sp22-bcs-139
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture No 16 - AVL Tree

Uploaded by

sp22-bcs-139
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

AVL

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.

Balance Factor = (Height of Left Subtree - Height of Right Subtree)


or (Height of Right Subtree - Height of Left Subtree)

• The self-balancing property of an AVL tree is maintained by the balance


factor.
• The value of the balance factor should always be -1, 0, or +1.
Example
Rotation in AVL
• In rotation operation, the positions of the nodes of a subtree are
interchanged.

• There are four types of rotations:


• Left Rotation
• Right Rotation
• Left-Right Rotation
• Right-Left Rotation
Left Rotation
• When a node is added to the right subtree of the right subtree if the
tree gets out of balance, we do a single left rotation.
Right Rotation
• If a node is added to the left subtree of the left subtree, the AVL tree
may get out of balance, we do a single right rotation
Left-Right Rotation
• A left-right rotation is a combination in which first left rotation takes
place after the right rotation executes.
Right-Left Rotation
• A right-left rotation is a combination in which first right rotation takes
place after the left rotation executes
Insertion

Initial tree for insertion node to be inserted


Insertion

Inserting the new node


Finding the location to insert
Insertion

Updating the balance factor after insertion Balancing the tree with rotation
Insertion

Balancing the tree with rotation Final balanced tree


Deletion

There are three cases for deleting a node:

• If nodeToBeDeleted is the leaf node (ie. does not have any


child), then remove nodeToBeDeleted.

• If nodeToBeDeleted has one child, then substitute the


contents of nodeToBeDeleted with that of the child.
Remove the child.

• If nodeToBeDeleted has two children, find the in-order


Locating the node to be deleted successor w of nodeToBeDeleted (ie. node with a minimum
value of key in the right subtree).
Deletion

Finding the successor Substitute the node to be deleted


Deletion

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);
}

You might also like