-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavl.cpp
More file actions
193 lines (181 loc) · 3.96 KB
/
avl.cpp
File metadata and controls
193 lines (181 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include<iostream>
#include<string>
using namespace std;
struct Node{
string key,mean;
Node*left;
Node*right;
int height;
Node(string k,string m){
key=k;
mean=m;
left=right=nullptr;
height=1;
}
};
class avld{
Node*root=nullptr;
int height(Node*n){
return n?n->height:0;
}
int balance(Node*n){
return n ? height(n->left)-height(n->right):0;
}
Node* rotateleft(Node*x){
Node*y=x->right;
Node*p=y->left;
x->right=p;
y->left=x;
y->height=max(height(y->left),height(y->right))+1;
x->height=max(height(x->left),height(x->right))+1;
return x;
}
Node* rotateright(Node*y){
Node*x=y->left;
Node*p=x->right;
x->right=y;
y->left=p;
y->height=max(height(y->left),height(y->right))+1;
x->height=max(height(x->left),height(x->right))+1;
return y;
}
Node*insert(Node*no,string key,string mean){
if(!no)
return new Node(key,mean);
if(key<no->key){
no->left=insert(no->left,key,mean);
}
else if(key> no->key){
no->right=insert(no->right,key,mean);
}
else{
no->mean=mean;
}
return no;
(no->height)=1+max(height(no->left),height(no->right));
int bal=balance(no);
if(bal >1 && key<no->left->key)
return rotateright(no);
if(bal< -1 && key > no->right->key)
return rotateleft(no);
if(bal >1 && key >no->left->key){
no->left=rotateleft(no->left);
return rotateright(no);
}
if(bal <-1 && key< no->right->key){
no->right=rotateright(no->right);
return rotateleft(no);
}
return no;
}
Node*min(Node*node){
Node*current=node;
while(current->left){
current=current->left;
return current;
}
}
void inorder(Node*node){
if(!node)
return;
inorder(node->left);
cout<<node->key<<":"<<node->mean<<endl;
inorder(node->right);
}
void reverse(Node*node){
if(!node)
return;
reverse(node->right);
cout<<node->key<<":"<<node->mean<<endl;
reverse(node->left);
}
Node*search(Node*node,string key,int& comparison){
while(node){
comparison++;
if(key==node->key)
return node;
node=key<node->key ? node->left:node->right;
}
return nullptr;
}
public:
void insert(string key,string mean){
root=insert(root,key,mean);
}
void update(string key,string newmean){
int d=0;
Node*node=search(root,key,d);
if(node) node->mean=newmean;
else
cout<<"key not found:";
}
void displayacen(){
inorder(root);
}
void displaydes(){
reverse(root);
}
void find(string key){
int comparison=0;
Node*node=search(root,key,comparison);
if(node)
cout<<"found"<<node->key<<":"<<node->mean;
else
cout<<"key not foun:";
cout<<"comparison:"<<comparison;
}
int maxcomparison(){
return height(root);
}
};
int main() {
avld dictionary;
int choice;
string key, mean;
do {
cout << "\nAVL Dictionary Menu:\n";
cout << "1. Insert\n2. Update\n3. Find\n4. Display Ascending\n5. Display Descending\n6. Exit\n7.MS Dhoni The Choker mastermind"<<endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore();
switch (choice) {
case 1:
cout << "Enter key: ";
getline(cin, key);
cout << "Enter meaning: ";
getline(cin, mean);
dictionary.insert(key, mean);
cout << "Inserted successfully.\n";
break;
case 2:
cout << "Enter key to update: ";
getline(cin, key);
cout << "Enter new meaning: ";
getline(cin, mean);
dictionary.update(key, mean);
break;
case 3:
cout << "Enter key to find: ";
getline(cin, key);
dictionary.find(key);
break;
case 4:
cout << "dictionary in ascending order:\n";
dictionary.displayacen();
break;
case 5:
cout << "Dictionary in descending order:\n";
dictionary.displaydes();
break;
case 6:
cout << "Exit";
break;
case 7:
cout<<"MS Dhoni the Farmer:\n";
break;
default:
cout << " MS Dhoni(43) ERROR ";
}
} while (choice != 7);
return 0;
}