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

Avl Tree Code For Insertion and Deletion

Uploaded by

amjadrimsha851
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Avl Tree Code For Insertion and Deletion

Uploaded by

amjadrimsha851
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

AVL TREE CODE FOR INSERTION AND DELETION:

#include <iostream>
using namespace std;

// Node structure for AVL Tree


struct AVLNode {
int key;
AVLNode* left;
AVLNode* right;
int height;
};

// Function to get the height of a node


int getHeight(AVLNode* node) {
return node ? node->height : 0;
}

// Function to get the balance factor of a node


int getBalance(AVLNode* node) {
return node ? getHeight(node->left) - getHeight(node->right) : 0;
}

// Function to create a new node


AVLNode* createNode(int key) {
AVLNode* node = new AVLNode();
node->key = key;
node->left = node->right = nullptr;
node->height = 1; // New node is initially added at leaf
return node;
}
// Right rotation
AVLNode* rightRotate(AVLNode* y) {
cout << "Performing right rotation on node with key " << y->key << endl;
AVLNode* x = y->left;
AVLNode* T2 = x->right;

// Perform rotation
x->right = y;
y->left = T2;

// Update heights
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;

return x; // New root


}

// Left rotation
AVLNode* leftRotate(AVLNode* x) {
cout << "Performing left rotation on node with key " << x->key << endl;
AVLNode* y = x->right;
AVLNode* T2 = y->left;

// Perform rotation
y->left = x;
x->right = T2;

// Update heights
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;

return y; // New root


}

// Function to insert a key into the AVL tree


AVLNode* insert(AVLNode* node, int key) {
// Perform standard BST insertion
if (!node) {
cout << "Inserting key " << key << endl;
return createNode(key);
}

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else // Duplicate keys are not allowed
return node;

// Update the height of this ancestor node


node->height = max(getHeight(node->left), getHeight(node->right)) + 1;

// Get the balance factor


int balance = getBalance(node);

// Balance the tree if needed


if (balance > 1 && key < node->left->key)
return rightRotate(node);
if (balance < -1 && key > node->right->key)
return leftRotate(node);

if (balance > 1 && key > node->left->key) {


node->left = leftRotate(node->left);
return rightRotate(node);
}

if (balance < -1 && key < node->right->key) {


node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

// Function to get the minimum value node in a tree


AVLNode* minValueNode(AVLNode* node) {
AVLNode* current = node;
while (current->left)
current = current->left;
return current;
}

// Function to delete a key from the AVL tree


AVLNode* deleteNode(AVLNode* root, int key) {
// Perform standard BST deletion
if (!root) 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 one child or no child
if (!root->left || !root->right) {
AVLNode* temp = root->left ? root->left : root->right;
if (!temp) {
temp = root;
root = nullptr;
} else
*root = *temp;
delete temp;
} else {
// Node with two children
AVLNode* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
}

if (!root) return root;

// Update height
root->height = max(getHeight(root->left), getHeight(root->right)) + 1;

// Get balance factor


int balance = getBalance(root);

// Balance the tree if needed


if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);

if (balance > 1 && getBalance(root->left) < 0) {


root->left = leftRotate(root->left);
return rightRotate(root);
}

if (balance < -1 && getBalance(root->right) <= 0)


return leftRotate(root);

if (balance < -1 && getBalance(root->right) > 0) {


root->right = rightRotate(root->right);
return leftRotate(root);
}

return root;
}

// Function to print the tree in-order


void inOrder(AVLNode* root) {
if (root) {
inOrder(root->left);
cout << root->key << " ";
inOrder(root->right);
}
}

// Main function
int main() {
AVLNode* root = nullptr;
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 10);
root = insert(root, 25);
root = insert(root, 50);

cout << "In-order traversal after insertion: ";


inOrder(root);
cout << endl;

root = deleteNode(root, 20);


cout << "In-order traversal after deletion of 20: ";
inOrder(root);
cout << endl;

return 0;
}

You might also like