Avl Generic code
Avl Generic code
void remove(T x) {
root = removeUtil(root, x); // Remove value from the tree
}
node* search(T x) {
return searchUtil(root, x); // Search for a value in the tree
}
void inorder() {
inorderUtil(root); // Perform inorder traversal
cout << endl;
}
private:
// Private helper methods for AVL tree operations
// Calculates the balance factor of a node (left subtree height - right subtree height)
int balanceFactor(node* head) {
if (head == NULL)
return 0;
return (height(head->left) - height(head->right));
}
return newhead;
}
return newhead;
}
// Recursively insert the value into the correct position in the tree
if (x < head->key)
head->left = insertUtil(head->left, x);
else if (x > head->key)
head->right = insertUtil(head->right, x);
// Left-Left case
if (balance > 1 && x < head->left->key)
return rightRotation(head);
// Right-Right case
if (balance < -1 && x > head->right->key)
return leftRotation(head);
// Left-Right case
if (balance > 1 && x > head->left->key) {
head->left = leftRotation(head->left);
return rightRotation(head);
}
// Right-Left case
if (balance < -1 && x < head->right->key) {
head->right = rightRotation(head->right);
return leftRotation(head);
}
if (x < head->key) {
head->left = removeUtil(head->left, x);
} else if (x > head->key) {
head->right = removeUtil(head->right, x);
} else {
// Node to be deleted is found
if (!head->left || !head->right) {
node* temp = head->left ? head->left : head->right;
delete head;
head = temp;
} else {
// Node has two children
node* temp = minNode(head->right);
head->key = temp->key;
head->right = removeUtil(head->right, temp->key);
}
}
// Left-Left case
if (balance > 1) {
if (balanceFactor(head->left) >= 0)
return rightRotation(head);
else {
head->left = leftRotation(head->left);
return rightRotation(head);
}
}
// Right-Right case
if (balance < -1) {
if (balanceFactor(head->right) <= 0)
return leftRotation(head);
else {
head->right = rightRotation(head->right);
return leftRotation(head);
}
}
int main() {
AVL<int> tree; // Create an AVL tree object
int choice, value;
do {
// Display menu options
cout << "Menu:" << endl;
cout << "1. Insert" << endl;
cout << "2. Remove" << endl;
cout << "3. Search" << endl;
cout << "4. Inorder Traversal" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1:
cout << "Enter value to insert: ";
cin >> value;
tree.insert(value); // Insert value into the tree
cout << "Value inserted successfully!" << endl;
break;
case 2:
cout << "Enter value to remove: ";
cin >> value;
tree.remove(value); // Remove value from the tree
cout << "Value removed successfully!" << endl;
break;
case 3:
cout << "Enter value to search: ";
cin >> value;
if (tree.search(value)) { // Search for a value
cout << "Value found!" << endl;
} else {
cout << "Value not found!" << endl;
}
break;
case 4:
cout << "Inorder Traversal: ";
tree.inorder(); // Print the tree using inorder traversal
break;
case 5:
cout << "Exiting program..." << endl;
break;
default:
cout << "Invalid choice, please try again." << endl;
}
} while(choice != 5); // Loop until the user chooses to exit
return 0;
}