Avl Tree Code For Insertion and Deletion
Avl Tree Code For Insertion and Deletion
#include <iostream>
using namespace std;
// 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;
// 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 node;
}
// Update height
root->height = max(getHeight(root->left), getHeight(root->right)) + 1;
return root;
}
// 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);
return 0;
}