AVL Tree Code
AVL Tree Code
#include <algorithm>
#include <cstdlib>
using namespace std;
class AVL
{
public:
node* root; // Root of the AVL tree
AVL() // Constructor initializes the tree with a null root
{
root = NULL;
}
// Function declarations
node* insert(node*, string, string); // Insert a word in the tree
node* deleteNode(node*, string); // Delete a word from the tree
void preorder(node*); // Preorder traversal of the tree
void inorder(node*); // Inorder traversal of the tree
node* RotateRight(node*); // Right rotation
node* RotateLeft(node*); // Left rotation
node* RR(node*); // Right-Right rotation
node* LL(node*); // Left-Left rotation
node* LR(node*); // Left-Right rotation
node* RL(node*); // Right-Left rotation
int height(node*); // Calculate height of a node
int BF(node*); // Calculate balance factor
void search(node*, string); // Search for a word in the tree
void modify(node*, string); // Modify the meaning of a word
node* findMin(node*); // Find the node with the minimum value
};
// Return 1 plus the maximum height of the left and right subtrees
return 1 + max(lh, rh);
}
// Left-Right rotation (used for balancing left-heavy nodes with a right child)
node* AVL::LR(node* T)
{
T->left = RotateLeft(T->left); // Perform left rotation on the left subtree
T = RotateRight(T); // Perform right rotation on the tree
return T;
}
// Right-Left rotation (used for balancing right-heavy nodes with a left child)
node* AVL::RL(node* T)
{
T->right = RotateRight(T->right); // Perform right rotation on the right subtree
T = RotateLeft(T); // Perform left rotation on the tree
return T;
}
// Insert function to add a word and its meaning into the AVL tree
node* AVL::insert(node* temp, string str_w, string str_m)
{
if (temp == NULL) // If tree is empty, create a new node
{
temp = new node;
temp->word = str_w;
temp->meaning = str_m;
temp->left = temp->right = NULL;
}
else
{
if (str_w.compare(temp->word) > 0) // If the word is greater, go to the right subtree
{
temp->right = insert(temp->right, str_w, str_m);
// If balance factor becomes -2, perform right rotation or right-left rotation
if (BF(temp) == -2)
temp = (str_w.compare(temp->right->word) > 0) ? RR(temp) : RL(temp);
}
else // If the word is smaller, go to the left subtree
{
temp->left = insert(temp->left, str_w, str_m);
// If balance factor becomes 2, perform left rotation or left-right rotation
if (BF(temp) == 2)
temp = (str_w.compare(temp->left->word) < 0) ? LL(temp) : LR(temp);
}
}
// Update the height of the node
temp->ht = height(temp);
return temp;
}
// Function to find the node with the minimum value in the tree
node* AVL::findMin(node* root) {
while (root->left != NULL) {
root = root->left; // Move left until the leftmost node is found
}
return root; // Return the leftmost node (minimum node)
}
if (str_w < root->word) { // If the word is smaller, search the left subtree
root->left = deleteNode(root->left, str_w);
} else if (str_w > root->word) { // If the word is greater, search the right subtree
root->right = deleteNode(root->right, str_w);
} else { // If the word is found, perform deletion
if (root->left == NULL) { // If the node has no left child, replace it with the right child
node* temp = root->right;
delete root;
return temp;
} else if (root->right == NULL) { // If the node has no right child, replace it with the left child
node* temp = root->left;
delete root;
return temp;
} else { // If the node has both children, replace it with the minimum value from the right subtree
node* temp = findMin(root->right);
root->word = temp->word;
root->meaning = temp->meaning;
root->right = deleteNode(root->right, temp->word);
}
}
// Update the height of the node after deletion
root->ht = height(root);
return root;
}