Practical No 10
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* rightRotate(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;
Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
return node;
}
return root;
}
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.
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...