DSL 9
DSL 9
A Dictionary stores keywords & its meanings. Provide facility for adding new
keywords, deleting keywords, updating values of any entry. Provide facility to
display whole data sorted in ascending/ descending order. Also find how
many maximum comparisons may require for finding any keyword. Use Height
balance tree and find complexity of finding keyword.
Program:
#include
<iostream>
#include <string>
using namespace
std;
struct Node {
string keyword,
meaning; Node* left;
Node*
right; int
height;
Node(string k, string m) {
keyword = k;
meaning = m;
left = right = nullptr;
height = 1;
}
};
// Rotations
Node* rightRotate(Node* y)
{
Node* x = y->left;
Node* T = x-
>right;
x->right =
y; y->left =
T;
y->height = max(height(y-
>left), height(y->right)) +
1; x->height =
max(height(x-
>left), height(x->right)) +
1;
return x;
}
Node* leftRotate(Node*
x) { Node* y = x->right;
Node* T = y->left;
y->left = x;
x->right =
T;
x->height = max(height(x-
>left), height(x->right)) +
1; y->height =
max(height(y-
>left), height(y->right)) + 1;
return y;
}
// Insert
Node* insert(Node* root,
string key, string
meaning) {
if (!root)
return new Node(key,
meaning);
root->height = 1 +
max(height(root-
>left), height(root-
>right));
int balance =
getBalance(root);
return root;
}
// Delete
Node*
deleteNode(Node* root,
string key) {
if (!root) return root;
if (key < root->keyword)
root->left =
deleteNode(root->left, key);
else if (key > root-
>keyword)
root->right =
deleteNode(root->right,
key);
else {
if (!root->left || !root-
>right) {
Node* temp = root-
>left ? root->left : root-
>right;
delete root;
return temp;
}
Node* temp =
minValueNode(root->right);
root->keyword = temp-
>keyword;
root->meaning = temp-
>meaning;
root->right =
deleteNode(root->right,
temp->keyword);
}
root->height = 1 +
max(height(root->left),
height(root->right));
int balance =
getBalance(root);
return root;
}
// Update
bool update(Node* root,
string key, string
newMeaning) {
int dummy = 0;
Node* node = search(root,
key, dummy);
if (node) {
node->meaning =
newMeaning;
return true;
}
return false;
}
int choice;
string key, meaning;
while (true) {
cout << "\n--- Dictionary
AVL Menu ---\n";
cout << "1. Add
Keyword\n2. Delete
Keyword\n3. Update
Meaning\n4. Display
Ascending\n";
cout << "5. Display
Descending\n6. Search
Keyword\n7. Show
Complexity\n8. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case1:
cout << "Enter
keyword: ";
cin >> key;
cout << "Enter
meaning: ";
cin.ignore();
getline(cin,
meaning);
root = insert(root,
key, meaning);
cout << "Keyword
inserted.\n";
break;
case2:
case 3:
cout << "Enter
keyword to update: ";
cin >> key;
cout << "Enter new
meaning: ";
cin.ignore();
getline(cin,
meaning);
if (update(root, key,
meaning))
cout << "Keyword
updated.\n";
else
cout << "Keyword
not found.\n";
break;
case 4:
cout << "Dictionary
in Ascending Order:\n";
displayAsc(root);
break;
case 5:
cout << "Dictionary
in Descending Order:\n";
displayDesc(root);
break;
case 6:
cout << "Enter
keyword to search: ";
cin >> key;
int comparisons;
comparisons = 0;
if (search(root, key,
comparisons))
cout << "Keyword
found in " << comparisons
<< " comparisons.\n";
else
cout << "Keyword
not found. Comparisons: "
<< comparisons << endl;
break;
case 7:
cout << "Search
Time Complexity in AVL Tree:
O(log n)\n";
break;
case 8:
cout <<
"Exiting...\n";
return 0;
default:
cout << "Invalid
choice.\n";
}
}
return 0;
}
Output:
1. Add Keyword
Enter choice: 1
Keyword inserted.
Enter choice: 4
apple : a fruit
Enter choice: 6
found in 1 comparisons.