-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplay_Tree.cpp
321 lines (267 loc) Β· 11.1 KB
/
Splay_Tree.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <iostream>
using namespace std;
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
class Node{
public:
int data;
string info;
Node *left;
Node *right;
int height;
};
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int max(int a, int b){
return (a > b)? a : b;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int height(Node *N){
if (N == NULL)
return 0;
return N->height;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
Node* minValueNode(Node* n){
while (n->left != NULL)
n = n->left;
return n;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
Node* newNode(int keydata, string keyinfo){
Node* node = new Node();
node->data = keydata;
node->info = keyinfo;
node->left = NULL;
node->right = NULL;
node->height = 1;
return(node);
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
Node *rightRotate(Node *x){
Node *y = x->left;
x->left = y->right;
y->right = x;
Node *T2 = x->right;
// Rotation
y->right = x;
x->left = T2;
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return y;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
Node *leftRotate(Node *x){
Node *y = x->right;
x->right = y->left;
y->left = x;
Node *T2 = y->left;
// Rotation
y->left = x;
x->right = T2;
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return y;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// This function brings the key at root if key is present in tree. If key is not present, then it brings the last accessed item at root. This function modifies the tree and returns the new root
Node *splay(Node *root, int key){
// Base cases: root is NULL or key is present at root
if (root == NULL || root->data == key)
return root;
// Key lies in left subtree
if (root->data > key){
// Key is not in tree, we are done
if (root->left == NULL) return root;
// Zig-Zig (Left Left)
if (root->left->data > key){
// First recursively bring the key as root of left-left
root->left->left = splay(root->left->left, key);
// Do first rotation for root, second rotation is done after else
root = rightRotate(root);
}
// Zig-Zag (Left Right)
else if (root->left->data < key) {
// First recursively bring the key as root of left-right
root->left->right = splay(root->left->right, key);
// Do first rotation for root->left
if (root->left->right != NULL)
root->left = leftRotate(root->left);
}
// Do second rotation for root
return (root->left == NULL)? root: rightRotate(root);
}
// Key lies in right subtree
else{
// Key is not in tree, we are done
if (root->right == NULL) return root;
// Zag-Zig (Right Left)
if (root->right->data > key){
// Bring the key as root of right-left
root->right->left = splay(root->right->left, key);
// Do first rotation for root->right
if (root->right->left != NULL)
root->right = rightRotate(root->right);
}
// Zag-Zag (Right Right)
else if (root->right->data < key){
// Bring the key as root of right-right and do first rotation
root->right->right = splay(root->right->right, key);
root = leftRotate(root);
}
// Do second rotation for root
return (root->right == NULL)? root: leftRotate(root);
}
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
Node* insNode(Node* root, int keydata, string keyinfo){
// Simple Case: If tree is empty
if (root == NULL) return newNode(keydata, keyinfo);
// Bring the closest leaf node to root
root = splay(root, keydata);
// If key is already present, then return
if (root->data == keydata) return root;
// Otherwise allocate memory for new node
Node *newnode = newNode(keydata, keyinfo);
// If root's key is greater, make root as right child of newnode and copy the left child of root to newnode
if (root->data > keydata){
newnode->right = root;
newnode->left = root->left;
root->left = NULL;
}
// If root's key is smaller, make root as left child of newnode and copy the right child of root to newnode
else{
newnode->left = root;
newnode->right = root->right;
root->right = NULL;
}
return newnode; // newnode becomes new root
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
Node* delNode(Node* root, int key){
//base case
if (root == NULL){
return root;
}
//if key to be deleted is smaller than the root's data, then it lies in the left subtree
if (key < root->data){
root->left = delNode(root->left, key);
}
//if key to be deleted is greater than the root's data, then it lies in the right subtree
else if (key > root->data){
root->right = delNode(root->right, key);
}
//if key is the same as the root's data, then this is the node to be deleted
else{
root = splay(root, key);
//node has no child
if (root->left == NULL && root->right == NULL){
delete root;
root = NULL;
}
//node with only one child
//right child
else if (root->left == NULL){
Node* temp = root;
root = root->right;
delete temp;
}
//left child
else if (root->right == NULL){
Node* temp = root;
root = root->left;
delete temp;
}
//node with two children
else{
Node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = delNode(root->right, temp->data);
}
}
return root;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void inorder(Node* n){ //LDR
if (n != NULL){
inorder(n->left);
cout << n->data << " \n";
cout << n->info << "\n";
inorder(n->right);
}
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void preorder(Node* n){ //DLR
if (n != NULL){
cout << n->data << " \n";
cout << n->info << "\n";
preorder(n->left);
preorder(n->right);
}
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void postorder(Node* n){ //LRD
if (n != NULL){
postorder(n->left);
postorder(n->right);
cout << n->data << " \n";
cout << n->info << "\n";
}
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int CountInternalNodes(Node* root){
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 0;
return 1+CountInternalNodes(root->left)+CountInternalNodes(root->right);
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int main(){
Node* root = NULL;
int ch, a, b, count;
string c;
cout << "1 βββββ> Insert a Node \n";
cout << "2 βββββ> Delete a Node \n";
cout << "3 βββββ> Traversal : Inorder \n";
cout << "4 βββββ> Traversal : Preorder \n";
cout << "5 βββββ> Traversal : Postorder \n";
cout << "6 βββββ> Number of Internal Nodes \n";
cout << "7 βββββ> Exit\n\n";
do{
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch){
case (1):
cout << "Enter the value to be Inserted to the Tree : ";
cin >> a;
cout << "Enter it's description : ";
cin >> c;
root = insNode(root, a, c);
break;
case (2):
cout << "Enter the element to be Deleted from the Tree : ";
cin >> b;
delNode(root, b);
break;
case (3):
inorder(root);
cout << endl;
break;
case (4):
preorder(root);
cout << endl;
break;
case (5):
postorder(root);
cout << endl;
break;
case (6):
count = CountInternalNodes(root);
cout << "Number of Internal Nodes : " << count << endl;
case (7):
break;
default:
cout << "Enter a value between 1 and 7";
}
}while(ch != 7);
return 0;
}