Practical Implementation
Practical Implementation
#include <string>
using namespace std;
else if (x > curr -> cWord) // If the word to be deleted is greater, search in the right subtree
curr -> right = deletE (curr -> right, x);
else if (x < curr -> cWord) // If the word to be deleted is smaller, search in the left subtree
curr -> left = deletE (curr -> left, x);
else if (curr -> right == NULL || curr -> left == NULL) { // Node has one or no child
curr = curr -> left ? curr -> left : curr -> right; // Replace current node with its non-null child
cout << "\nWord deleted Successfully!\n";
}
// Right rotation
AVLnode* AVLtree::RR (AVLnode* curr) {
AVLnode* temp = curr -> left; // Save the left child
curr -> left = temp -> right; // Move the left child's right subtree to the left of the current node
temp -> right = curr; // Move the current node to the right of the left child
curr -> iHt = max(height(curr -> left), height(curr -> right)) + 1; // Update the height of the current
node
temp -> iHt = max(height(temp -> left), height(temp -> right)) + 1; // Update the height of the temp
node
return temp; // Return the new root of the subtree
}
// Left rotation
AVLnode* AVLtree::LL (AVLnode* curr) {
AVLnode* temp = curr -> right; // Save the right child
curr -> right = temp -> left; // Move the right child's left subtree to the right of the current node
temp -> left = curr; // Move the current node to the left of the right child
curr -> iHt = max(height(curr -> left), height(curr -> right)) + 1; // Update the height of the current
node
temp -> iHt = max(height(temp -> left), height(temp -> right)) + 1; // Update the height of the temp
node
return temp; // Return the new root of the subtree
}
// Right-Left rotation
AVLnode* AVLtree::RL (AVLnode* curr) {
curr -> right = RR (curr -> right); // Perform right rotation on right child
return LL (curr); // Perform left rotation on current node
}
// Left-Right rotation
AVLnode* AVLtree::LR (AVLnode* curr) {
curr -> left = LL (curr -> left); // Perform left rotation on left child
return RR (curr); // Perform right rotation on current node
}
switch (ch) {
case 1:
cout << "\nEnter Word: ";
cin >> word; // Input word
cout << "\nEnter Meaning: ";
cin >> mean; // Input meaning
avl.Root = avl.insert (avl.Root, word, mean); // Insert the word in the AVL tree
break;
case 2:
cout << "\nInorder Traversal:\n\tWORD\tMEANING";
avl.inOrder (avl.Root); // Display the inorder traversal
cout << "\n\nPreorder Traversal:\n\tWORD\tMEANING";
avl.preOrder (avl.Root); // Display the preorder traversal
cout << '\n';
break;
case 3:
cout << "\nEnter the word to be deleted : ";
cin >> word; // Input word to delete
avl.Root = avl.deletE (avl.Root, word); // Delete the word from the AVL tree
break;
case 4:
exit (0); // Exit the program
break;
}
} while (ch != 4); // Repeat until user chooses to exit
return 0;
}