ADSA
Lab Assignment - 9
Implement AVL tree insertion and deletion.
Code :
#include <iostream>
#include <algorithm>
using namespace std;
// Structure for a node of the AVL tree
struct Node {
int key;
Node* left;
Node* right;
int height;
};
// Function to get height of the AVL tree
int height(Node* node) {
if (node == nullptr)
return 0;
return node->height;
}
// Function to get the balance factor of a node
int balanceFactor(Node* node) {
if (node == nullptr)
return 0;
return height(node->left) - height(node->right);
}
// Function to create a new node with the given key
Node* createNode(int key) {
Node* newNode = new Node();
newNode->key = key;
newNode->left = nullptr;
newNode->right = nullptr;
newNode->height = 1; // new node is initially at height 1
return newNode;
}
// Function to perform right rotation
Node* rotateRight(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;
}
// Function to perform left rotation
Node* rotateLeft(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return y;
}
// Function to insert a node into AVL tree
Node* insertNode(Node* node, int key) {
// Perform standard BST insertion
if (node == nullptr)
return createNode(key);
if (key < node->key)
node->left = insertNode(node->left, key);
else if (key > node->key)
node->right = insertNode(node->right, key);
else // Duplicate keys not allowed
return node;
// Update height of current node
node->height = 1 + max(height(node->left), height(node->right));
// Get the balance factor
int balance = balanceFactor(node);
// Perform rotations if needed
// Left Left Case
if (balance > 1 && key < node->left->key)
return rotateRight(node);
// Right Right Case
if (balance < -1 && key > node->right->key)
return rotateLeft(node);
// Left Right Case
if (balance > 1 && key > node->left->key) {
node->left = rotateLeft(node->left);
return rotateRight(node);
}
// Right Left Case
if (balance < -1 && key < node->right->key) {
node->right = rotateRight(node->right);
return rotateLeft(node);
}
// No rotation needed
return node;
}
// Function to nd the node with minimum key value in a subtree rooted with given node
Node* minValueNode(Node* node) {
Node* current = node;
// Loop down to nd the leftmost leaf
while (current->left != nullptr)
current = current->left;
return current;
}
// Function to delete a node with given key from AVL tree
Node* deleteNode(Node* root, int key) {
// Perform standard BST delete
if (root == nullptr)
return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
// Node with only one child or no child
if (root->left == nullptr || root->right == nullptr) {
Node* temp = root->left ? root->left : root->right;
// No child case
if (temp == nullptr) {
temp = root;
root = nullptr;
} else // One child case
*root = *temp; // Copy the contents of the non-empty child
delete temp;
} else {
fi
fi
// Node with two children: Get the inorder successor (smallest in the right subtree)
Node* temp = minValueNode(root->right);
// Copy the inorder successor's data to this node
root->key = temp->key;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->key);
}
}
// If the tree had only one node then return
if (root == nullptr)
return root;
// Update height of the current node
root->height = 1 + max(height(root->left), height(root->right));
// Get the balance factor
int balance = balanceFactor(root);
// Perform rotations if needed
// Left Left Case
if (balance > 1 && balanceFactor(root->left) >= 0)
return rotateRight(root);
// Left Right Case
if (balance > 1 && balanceFactor(root->left) < 0) {
root->left = rotateLeft(root->left);
return rotateRight(root);
}
// Right Right Case
if (balance < -1 && balanceFactor(root->right) <= 0)
return rotateLeft(root);
// Right Left Case
if (balance < -1 && balanceFactor(root->right) > 0) {
root->right = rotateRight(root->right);
return rotateLeft(root);
}
return root;
}
// Function to print the AVL tree in-order traversal
void inorderTraversal(Node* root) {
if (root == nullptr)
return;
inorderTraversal(root->left);
cout << root->key << " ";
inorderTraversal(root->right);
}
int main() {
Node* root = nullptr;
// Inserting nodes
root = insertNode(root, 10);
root = insertNode(root, 20);
root = insertNode(root, 30);
root = insertNode(root, 40);
root = insertNode(root, 50);
root = insertNode(root, 25);
cout << "Inorder traversal of the AVL tree: ";
inorderTraversal(root);
cout << endl;
// Deleting nodes
root = deleteNode(root, 20);
cout << "Inorder traversal after deleting 20: ";
inorderTraversal(root);
cout << endl;
root = deleteNode(root, 30);
cout << "Inorder traversal after deleting 30: ";
inorderTraversal(root);
cout << endl;
return 0;
}
Output :