0% found this document useful (0 votes)
5 views8 pages

DSL 9

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)
5 views8 pages

DSL 9

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/ 8

ASSIGNMENT No- 09

Name- Om Santosh Songire


Roll No - 128
Batch - B3

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

int height(Node* node) {


return node ? node-
>height : 0;
}

int getBalance(Node* node)


{
return node ?
height(node->left) -
height(node->right) : 0;
}
int max(int a, int b) {
return (a > b) ? a :
b;
}

// 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);

if (key < root-


>keyword) root->left
= insert(root-
>left, key, meaning);
else if (key > root-
>keyword)
root->right = insert(root-
>right, key, meaning)
else {
root->meaning =
meaning; // Update if key
exists
return root;
}

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

if (balance > 1 && key


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

return root;
}

// Get min value node


Node*
minValueNode(Node*
node)
{
Node* current = node;
while (current->left !=
nullptr)
current = current-
>left; return current;
}

// 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);

if (balance > 1 &&


getBalance(root->left) >= 0)
return rightRotate(root); if
(balance < -1 &&
getBalance(root->right) <= 0)
return leftRotate(root);
if (balance > 1 &&
getBalance(root->left) < 0) {
root->left =
leftRotate(root->left);
return rightRotate(root);
}
if (balance < -1 &&
getBalance(root->right) > 0) {
root->right =
rightRotate(root->right);
return leftRotate(root);
}

return root;
}

// Inorder Display Ascending


void displayAsc(Node* root)
{
if (root) {
displayAsc(root->left);
cout << root->keyword
<< " : " << root->meaning <<
endl;
displayAsc(root->right);
}
}

// Reverse Inorder Display


Descending
void displayDesc(Node* root) {
if (root) {
displayDesc(root->right);
cout << root->keyword
<< " : " << root->meaning <<
endl;
displayDesc(root->left);
}
}

// Search with comparisons Node* search(Node* root, string


key, int& comparisons)
{
if (!root) return nullptr;
comparisons++;
if (key == root->keyword)
return root;
else if (key < root-
>keyword)
return search(root->left,
key, comparisons);
else
return search(root-
>right, key, comparisons);
}

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

// Main Menu int


main() {
Node* root = nullptr;
AVLTree tree;

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:

cout << "Enter keyword to


delete: ";
cin >> key;
root =
deleteNode(root, key);
cout << "Keyword
deleted if existed.\n";
break;

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:

--- Dictionary AVL Menu ---

1. Add Keyword

Enter choice: 1

Enter keyword: apple

Enter meaning: a fruit

Keyword inserted.

Enter choice: 4

Dictionary in Ascending Order:

apple : a fruit

Enter choice: 6

Enter keyword to search: apple Keyword

found in 1 comparisons.

Enter your choice: 2


Sorted Keys: [10, 20, 30, 40]
Probabilities: [0.1, 0.2, 0.4, 0.3]

You might also like