-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrims.cpp
243 lines (207 loc) · 5.99 KB
/
Prims.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
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 the complexity for finding a keyword.
Name : Advait Naik
Roll No : 21354
*/
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class Node{
int key, height = 1;
Node *left, *right;
public:
Node(int val){
this -> key = val;
this -> left = nullptr;
this -> right = nullptr;
}
friend class AVL;
};
class AVL{
Node *root = nullptr;
int getHeight(Node *node){
if(node == nullptr){
return 0;
}
return node -> height;
}
void changeHeight(Node *node){
node -> height = 1 + max(getHeight(node -> left) , getHeight(node -> right));
}
int balanceFactor(Node *node){
if(node == nullptr){
return 0;
}
return getHeight(node -> left) - getHeight(node -> right);
}
Node *llRotation(Node *node){
Node *newRoot = node -> left;
node -> left = newRoot -> right;
newRoot -> right = node;
changeHeight(node);
changeHeight(newRoot);
return newRoot;
}
Node *rrRotation(Node *node){
Node *newRoot = node -> right;
node -> right = newRoot -> left;
newRoot -> left = node;
changeHeight(node);
changeHeight(newRoot);
return newRoot;
}
Node *lrRotation(Node *node){
node -> left = rrRotation(node -> left);
return llRotation(node);
}
Node *rlRotation(Node *node){
node -> right = llRotation(node -> right);
return rrRotation(node);
}
Node *balanceNode(Node *node){
if(balanceFactor(node) == 2){
if(balanceFactor(node -> left) < 0){
node = lrRotation(node);
}
else{
node = llRotation(node);
}
}
else if(balanceFactor(node) == -2){
if(balanceFactor(node -> right) > 0){
node = rlRotation(node);
}
else{
node = rrRotation(node);
}
}
changeHeight(node);
return node;
}
Node *insertNode(Node *node,int val){
if(node == nullptr){
node = new Node(val);
return node;
}
if(val < node -> key){
node -> left = insertNode(node -> left,val);
}
else if(val > node -> key){
node -> right = insertNode(node -> right,val);
}
else{
return node;
}
return balanceNode(node);
}
Node *deleteNode(Node *node,int x){
if(node == nullptr){
return nullptr;
}
if(node -> key == x){
if(node -> right != nullptr && node -> left != nullptr){
Node *smallestRST = node -> right;
while(smallestRST -> left != nullptr){
smallestRST = smallestRST -> left;
}
node -> key = smallestRST -> key;
node -> right = deleteNode(node -> right,smallestRST -> key);
}
if(node -> key == x && node -> left == nullptr && node -> right != nullptr){
Node *rightChild = node -> right;
delete node;
return rightChild;
}
else if(node -> key == x && node -> left != nullptr && node -> right == nullptr){
Node *leftChild = node -> left;
delete node;
return leftChild;
}
else if(node -> key == x){
delete node;
return nullptr;
}
}
else{
if(x < node -> key){
node -> left = deleteNode(node -> left,x);
}
else{
node -> right = deleteNode(node -> right,x);
}
}
int bf = balanceFactor(node);
if(bf != 2 && bf != -2){
return node;
}
else{
node = balanceNode(node);
return node;
}
}
Node* search( int value ) {
Node* current = this -> root ;
while( current != nullptr ) {
if( current -> key > value ) {
current = current -> left ;
}
else if( current -> key < value ) {
current = current -> right ;
}
else {
return current ;
}
}
return nullptr ;
}
public:
void insert(int val){
this -> root = insertNode(this -> root,val);
}
void bfs(){
queue <Node *> q;
q.push(this -> root);
while(!q.empty()){
Node *current = q.front();
cout<<current -> key<<" ";
q.pop();
if(current -> left != nullptr){
q.push(current -> left);
}
if(current -> right != nullptr){
q.push(current -> right);
}
}
cout<<endl;
}
void deleteKey(int val){
this -> root = deleteNode(this -> root , val);
}
void searchKey(int val){
Node *ans = search(val);
if(ans == nullptr){
cout<<"Key not present in tree."<<endl;
}
else{
cout<<"Key present in tree."<<endl;
}
}
};
int main(){
AVL t;
t.insert(23);
t.insert(12);
t.insert(46);
t.insert(57);
t.insert(90);
t.bfs();
t.searchKey(57);
t.searchKey(11);
t.deleteKey(23);
t.bfs();
return 0;
}