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

AVL Tree Code

The document contains a C++ implementation of an AVL tree, which is a self-balancing binary search tree. It includes functionalities for inserting, deleting, searching, and modifying words along with their meanings in a dictionary format. The main function provides a user interface to perform various operations on the AVL tree.
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)
10 views

AVL Tree Code

The document contains a C++ implementation of an AVL tree, which is a self-balancing binary search tree. It includes functionalities for inserting, deleting, searching, and modifying words along with their meanings in a dictionary format. The main function provides a user interface to perform various operations on the AVL tree.
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/ 8

#include <iostream>

#include <algorithm>
#include <cstdlib>
using namespace std;

// Node structure for AVL tree


class node
{
public:
string word, meaning; // Word and its meaning in the dictionary
int ht; // Height of the node
node* left, * right; // Pointers to left and right children
};

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
};

// Function to calculate the height of a subtree


int AVL::height(node* temp)
{
if (temp == NULL)
return 0; // Height of a NULL node is 0

// Calculate the height of the left and right subtrees


int lh = height(temp->left);
int rh = height(temp->right);

// Return 1 plus the maximum height of the left and right subtrees
return 1 + max(lh, rh);
}

// Function to calculate the balance factor of a node


int AVL::BF(node* temp)
{
if (temp == NULL)
return 0;
// Balance factor is the difference in height between left and right subtrees
return (height(temp->left) - height(temp->right));
}

// Right rotation function (used in RR and RL rotations)


node* AVL::RotateRight(node* parent) {
node* temp = parent->left; // Take the left child of parent as the new root
parent->left = temp->right; // Move the right child of the new root to the left child of the parent
temp->right = parent; // Parent becomes the right child of the new root

// Update the height of the parent and the new root


parent->ht = height(parent);
temp->ht = height(temp);
return temp; // Return the new root
}

// Left rotation function (used in LL and LR rotations)


node* AVL::RotateLeft(node* parent) {
node* temp = parent->right; // Take the right child of parent as the new root
parent->right = temp->left; // Move the left child of the new root to the right child of the parent
temp->left = parent; // Parent becomes the left child of the new root

// Update the height of the parent and the new root


parent->ht = height(parent);
temp->ht = height(temp);
return temp; // Return the new root
}

// Right-Right rotation (used for balancing right-heavy nodes)


node* AVL::RR(node* T)
{
T = RotateLeft(T); // Perform a left rotation
return T;
}

// Left-Left rotation (used for balancing left-heavy nodes)


node* AVL::LL(node* T)
{
T = RotateRight(T); // Perform a right rotation
return T;
}

// 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)
}

// Delete function to remove a word from the AVL tree


node* AVL::deleteNode(node* root, string str_w) {
if (root == NULL)
return NULL; // If the tree is empty, return NULL

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;
}

// Preorder traversal (root -> left -> right)


void AVL::preorder(node* root)
{
if (root != NULL)
{
cout << root->word << "(Bf=" << BF(root) << ") "; // Print the word and balance factor
preorder(root->left); // Visit left subtree
preorder(root->right); // Visit right subtree
}
}

// Inorder traversal (left -> root -> right)


void AVL::inorder(node* root)
{
if (root != NULL)
{
inorder(root->left); // Visit left subtree
cout << root->word << "(Bf=" << BF(root) << ") "; // Print the word and balance factor
inorder(root->right); // Visit right subtree
}
}
// Function to search for a word in the AVL tree
void AVL::search(node* root, string str_w)
{
if (root == NULL) {
cout << "Word not found" << endl;
return;
}

if (str_w.compare(root->word) < 0) { // If word is smaller, search left subtree


search(root->left, str_w);
} else if (str_w.compare(root->word) > 0) { // If word is greater, search right subtree
search(root->right, str_w);
} else { // Word found
cout << "Word: " << root->word << endl;
cout << "Meaning: " << root->meaning << endl;
}
}

// Function to modify the meaning of a word in the AVL tree


void AVL::modify(node* root, string str_w) {
if (root == NULL) {
cout << "Word not found" << endl;
return;
}

if (str_w.compare(root->word) < 0) { // If word is smaller, search left subtree


modify(root->left, str_w);
} else if (str_w.compare(root->word) > 0) { // If word is greater, search right subtree
modify(root->right, str_w);
} else { // Word found, modify its meaning
cout << "Enter new meaning: ";
cin.ignore(); // Clear the input buffer before using getline
getline(cin, root->meaning); // Get the new meaning
}
}

// Main function to drive the program


int main()
{
AVL Tree; // Create an instance of the AVL tree
int ch;
string str1, str2;
cout << "\tOPERATIONS ON AVL TREE\t" << endl;
while (true)
{
// Display the menu for user
cout << "\n1. Create tree" << endl;
cout << "2. Add word" << endl;
cout << "3. Display tree" << endl;
cout << "4. Delete word" << endl;
cout << "5. Search word" << endl;
cout << "6. Modify meaning" << endl;
cout << "7. Exit" << endl;
cout << "Enter choice: ";
cin >> ch;
cin.ignore(); // Avoid leftover '\n' in the input buffer
switch (ch)
{
case 1:
case 2:
cout << "Enter word: ";
cin >> str1;
cin.ignore(); // Clear input buffer before using getline
cout << "Enter meaning: ";
getline(cin, str2);
Tree.root = Tree.insert(Tree.root, str1, str2);
break;
case 3:
cout << "Preorder: ";
Tree.preorder(Tree.root); // Display tree in preorder
cout << endl;
cout << "Inorder: ";
Tree.inorder(Tree.root); // Display tree in inorder
cout << endl;
break;
case 4:
cout << "Enter word: ";
cin >> str1;
Tree.root = Tree.deleteNode(Tree.root, str1); // Delete the word from the tree
break;
case 5:
cout << "Enter word: ";
cin >> str1;
Tree.search(Tree.root, str1); // Search for a word in the tree
break;
case 6:
cout << "Enter word: ";
cin >> str1;
Tree.modify(Tree.root, str1); // Modify the meaning of a word
break;
case 7:
exit(1); // Exit the program
break;
}
}
return 0;
}

You might also like