0% found this document useful (0 votes)
42 views7 pages

Practical No 10

The document contains a C++ implementation of an AVL tree-based dictionary that allows users to insert, delete, update, display, and search for keywords and their meanings. It includes functions for balancing the tree after insertions and deletions, as well as methods for displaying the dictionary in ascending and descending order. The main function provides a user interface for interacting with the dictionary through various choices.

Uploaded by

Kiran janjal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

Practical No 10

The document contains a C++ implementation of an AVL tree-based dictionary that allows users to insert, delete, update, display, and search for keywords and their meanings. It includes functions for balancing the tree after insertions and deletions, as well as methods for displaying the dictionary in ascending and descending order. The main function provides a user interface for interacting with the dictionary through various choices.

Uploaded by

Kiran janjal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical No 10

#include <iostream>
#include <string>
using namespace std;

struct Node {
string keyword;
string meaning;
Node* left;
Node* right;
int height;
};

int height(Node* n) {
return n ? n->height : 0;
}

int getBalance(Node* n) {
return n ? height(n->left) - height(n->right) : 0;
}

Node* newNode(string keyword, string meaning) {


Node* node = new Node();
node->keyword = keyword;
node->meaning = meaning;
node->left = node->right = nullptr;
node->height = 1;
return node;
}

Node* rightRotate(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;

y->height = 1 + max(height(y->left), height(y->right));


x->height = 1 + max(height(x->left), height(x->right));
return x;
}

Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;

x->height = 1 + max(height(x->left), height(x->right));


y->height = 1 + max(height(y->left), height(y->right));
return y;
}

Node* insert(Node* node, string keyword, string meaning) {


if (!node) return newNode(keyword, meaning);
if (keyword < node->keyword)
node->left = insert(node->left, keyword, meaning);
else if (keyword > node->keyword)
node->right = insert(node->right, keyword, meaning);
else {
cout << "Keyword already exists. Updating meaning.\n";
node->meaning = meaning;
return node;
}

node->height = 1 + max(height(node->left), height(node->right));


int balance = getBalance(node);

if (balance > 1 && keyword < node->left->keyword)


return rightRotate(node);
if (balance < -1 && keyword > node->right->keyword)
return leftRotate(node);
if (balance > 1 && keyword > node->left->keyword) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && keyword < node->right->keyword) {
node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

Node* minValueNode(Node* node) {


Node* current = node;
while (current->left)
current = current->left;
return current;
}

Node* deleteNode(Node* root, string keyword) {


if (!root) return root;
if (keyword < root->keyword)
root->left = deleteNode(root->left, keyword);
else if (keyword > root->keyword)
root->right = deleteNode(root->right, keyword);
else {
if (!root->left || !root->right) {
Node* temp = root->left ? root->left : root->right;
if (!temp) {
temp = root;
root = nullptr;
} else
*root = *temp;
delete temp;
} else {
Node* temp = minValueNode(root->right);
root->keyword = temp->keyword;
root->meaning = temp->meaning;
root->right = deleteNode(root->right, temp->keyword);
}
}

if (!root) return root;

root->height = 1 + max(height(root->left), height(root->right));


int balance = getBalance(root);

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

void displayAscending(Node* root) {


if (root) {
displayAscending(root->left);
cout << root->keyword << " : " << root->meaning << endl;
displayAscending(root->right);
}
}

void displayDescending(Node* root) {


if (root) {
displayDescending(root->right);
cout << root->keyword << " : " << root->meaning << endl;
displayDescending(root->left);
}
}

int search(Node* root, string key, int& comparisons) {


while (root) {
comparisons++;
if (key == root->keyword) {
cout << "Found: " << root->keyword << " : " << root->meaning << endl;
return 1;
} else if (key < root->keyword)
root = root->left;
else
root = root->right;
}
cout << "Keyword not found." << endl;
return 0;
}

int main() {
Node* root = nullptr;
int choice;
string key, meaning;

do {
cout << "\n1. Insert\n2. Delete\n3. Update\n4. Display Ascending\n5. Display Descending\n6. Search\n7.
Exit\nEnter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter keyword: ";
cin >> key;
cout << "Enter meaning: ";
cin.ignore(); getline(cin, meaning);
root = insert(root, key, meaning);
break;
case 2:
cout << "Enter keyword to delete: ";
cin >> key;
root = deleteNode(root, key);
break;
case 3:
cout << "Enter keyword to update: ";
cin >> key;
cout << "Enter new meaning: ";
cin.ignore(); getline(cin, meaning);
root = insert(root, key, meaning); // insert will update if exists
break;
case 4:
cout << "Dictionary in Ascending Order:\n";
displayAscending(root);
break;
case 5:
cout << "Dictionary in Descending Order:\n";
displayDescending(root);
break;
case 6:
cout << "Enter keyword to search: ";
cin >> key;
int comparisons = 0;
search(root, key, comparisons);
cout << "Comparisons made: " << comparisons << endl;
break;
case 7:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (choice != 7);

return 0;
}
Output:- 1. Insert
2. Delete
3. Update
4. Display Ascending
5. Display Descending
6. Search
7. Exit
Enter choice: 1
Enter keyword: tree
Enter meaning: A data structure with nodes.

1. Insert
2. Delete
3. Update
4. Display Ascending
5. Display Descending
6. Search
7. Exit
Enter choice: 1
Enter keyword: algorithm
Enter meaning: A step-by-step procedure to solve a problem.

1. Insert
2. Delete
3. Update
4. Display Ascending
5. Display Descending
6. Search
7. Exit
Enter choice: 1
Enter keyword: binary
Enter meaning: A base-2 numeral system.

1. Insert
2. Delete
3. Update
4. Display Ascending
5. Display Descending
6. Search
7. Exit
Enter choice: 4
Dictionary in Ascending Order:
algorithm : A step-by-step procedure to solve a problem.
binary : A base-2 numeral system.
tree : A data structure with nodes.

1. Insert
2. Delete
3. Update
4. Display Ascending
5. Display Descending
6. Search
7. Exit
Enter choice: 5
Dictionary in Descending Order:
tree : A data structure with nodes.
binary : A base-2 numeral system.
algorithm : A step-by-step procedure to solve a problem.

1. Insert
2. Delete
3. Update
4. Display Ascending
5. Display Descending
6. Search
7. Exit
Enter choice: 3
Enter keyword to update: binary
Enter new meaning: A numeric system with only 0 and 1.

Keyword already exists. Updating meaning.

1. Insert
2. Delete
3. Update
4. Display Ascending
5. Display Descending
6. Search
7. Exit
Enter choice: 4
Dictionary in Ascending Order:
algorithm : A step-by-step procedure to solve a problem.
binary : A numeric system with only 0 and 1.
tree : A data structure with nodes.

1. Insert
2. Delete
3. Update
4. Display Ascending
5. Display Descending
6. Search
7. Exit
Enter choice: 2
Enter keyword to delete: tree

1. Insert
2. Delete
3. Update
4. Display Ascending
5. Display Descending
6. Search
7. Exit
Enter choice: 4
Dictionary in Ascending Order:
algorithm : A step-by-step procedure to solve a problem.
binary : A numeric system with only 0 and 1.

1. Insert
2. Delete
3. Update
4. Display Ascending
5. Display Descending
6. Search
7. Exit
Enter choice: 6
Enter keyword to search: binary
Found: binary : A numeric system with only 0 and 1.
Comparisons made: 2

1. Insert
2. Delete
3. Update
4. Display Ascending
5. Display Descending
6. Search
7. Exit
Enter choice: 7
Exiting...

You might also like