// Produced by ChatGPT
#include <stdio.h>
#include <stdlib.h>
// Define structure for AVL Tree Node
struct Node {
int key;
struct Node *left;
struct Node *right;
int height;
};
// Helper function to get the height of a node
int height(struct Node *N) {
return (N == NULL) ? 0 : N->height;
}
// Helper function to get the maximum of two integers
int max(int a, int b) {
return (a > b) ? a : b;
}
// Create a new AVL Tree Node
struct Node *newNode(int key) {
struct Node *node = (struct Node *)malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // New node is initially added at leaf
return node;
}
// Right rotate subtree rooted with y
struct Node *rightRotate(struct Node *y) {
struct Node *x = y->left;
struct 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 new root
return x;
}
// Left rotate subtree rooted with x
struct Node *leftRotate(struct Node *x) {
struct Node *y = x->right;
struct 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 new root
return y;
}
// Get the balance factor of a node
int getBalance(struct Node *N) {
return (N == NULL) ? 0 : height(N->left) - height(N->right);
}
// Insert a node into the AVL tree and balance it
struct Node *insert(struct Node *node, int key) {
// Perform the normal BST insertion
if (node == NULL)
return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else // Equal keys are not allowed in AVL tree
return node;
// Update the height of this ancestor node
node->height = 1 + max(height(node->left), height(node->right));
// Get the balance factor of this ancestor node to check if unbalanced
int balance = getBalance(node);
// If the node is unbalanced, then balance the tree
// Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);
// Right Right Case
if (balance < -1 && key > node->right->key)
return leftRotate(node);
// Left Right Case
if (balance > 1 && key > node->left->key) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
// Right Left Case
if (balance < -1 && key < node->right->key) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
// Return the unchanged node pointer
return node;
}
// Find the node with the minimum key value (used in delete operation)
struct Node *minValueNode(struct Node *node) {
struct Node *current = node;
while (current->left != NULL)
current = current->left;
return current;
}
// Delete a node from the AVL tree and balance it
struct Node *deleteNode(struct Node *root, int key) {
// Perform standard BST delete
if (root == NULL)
return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
if ((root->left == NULL) || (root->right == NULL)) {
struct Node *temp = root->left ? root->left : root->right;
if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp; // Copy the contents of the non-empty child
free(temp);
} else {
struct Node *temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
}
if (root == NULL)
return root;
// Update height of the current node
root->height = 1 + max(height(root->left), height(root->right));
// Get the balance factor of this node
int balance = getBalance(root);
// If the node is unbalanced, then balance the tree
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;
}
// A utility function to print the preorder traversal of the tree
void preOrder(struct Node *root) {
if (root != NULL) {
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
int main() {
struct Node *root = NULL;
// Insert nodes
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
printf("Preorder traversal of the AVL tree is:\n");
preOrder(root);
printf("\n");
// Delete nodes
root = deleteNode(root, 20);
printf("Preorder traversal after deletion of 20:\n");
preOrder(root);
printf("\n");
return 0;
}