-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst.c
284 lines (229 loc) · 6.33 KB
/
st.c
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
#include"string.h"
#include<stdio.h>
#include<stdlib.h>
#include"st.h"
void init_st(st* root){
*root = NULL;
return;
}
void zig(st* root, node* key){
node *p = key, *q = key->parent, *temp = NULL;
temp = p->right;
p->parent = q->parent;
if(q->parent){
if(q->parent->left == q){
q->parent->left = p;
}
else{
q->parent->right = p;
}
}
q->parent = p;
p->right = q;
q->left = temp;
if(temp){
temp->parent = q;
}
if(p->parent == NULL){
*root = p;
}
return;
}
void zag(st* root, node* key){
node *p = key, *q = key->parent, *temp = NULL;
temp = p->left;
p->parent = q->parent;
if(q->parent){
if(q->parent->left == q){
q->parent->left = p;
}
else{
q->parent->right = p;
}
}
q->parent = p;
p->left = q;
q->right = temp;
if(temp){
temp->parent = q;
}
if(p->parent == NULL){
*root = p;
}
return;
}
void zig_zig(st* root, node* key){
//In this rotation, we first rotate the bond between the parent and the grandparent in right direction.
zig(root, key->parent);
//After that, rotation of bond between the child and the parent occurs in right direction.
zig(root, key);
return;
}
void zag_zag(st* root, node* key){
//In this rotation, we first rotate the bond between the parent and the grandparent in left direction.
zag(root, key->parent);
//After that, rotation of bond between the child and the parent occurs in left direction.
zag(root, key);
return;
}
void zig_zag(st* root, node* key){
//In this rotation, we first rotate the bond between the child and the parent in right direction.
zig(root, key);
//After that, rotation of bond between the child and the grandparent occurs in left direction.
zag(root, key);
return;
}
void zag_zig(st* root, node* key){
//In this rotation, we first rotate the bond between the child and the parent in left direction.
zag(root, key);
//After that, rotation of bond between the child and the grandparent occurs in right direction.
zig(root, key);
return;
}
void Splay(st* root, node* key){
//Splaying consists of 6 cases:
//1. Searched/Newly inserted node is left child of the root - Zig rotation
//2. Searched/Newly inserted node is right child of the root - Zag rotation
//3. Searched/Newly inserted node is left child of the parent and left child of its grandparent - Zig Zig rotation
//4. Searched/Newly inserted node is right child of the parent and right child of its grandparent - Zag Zag rotation
//5. Searched/Newly inserted node is left child of its parent and its parent is right child of its parent - Zig Zag rotation
//6. Searched/Newly inserted node is right child of its parent and its parent is left child of its parent - Zag Zig rotation
while (key->parent != NULL) {
if (key->parent->parent != NULL) {
if (key->parent->left == key) {
if (key->parent->parent->left == key->parent) {
//printf("\nCall to zig_zig function\n");
zig_zig(root, key);
} else {
//printf("\nCall to zig_zag function\n");
zig_zag(root, key);
}
} else {
if (key->parent->parent->left == key->parent) {
//printf("\nCall to zag_zig function\n");
zag_zig(root, key);
} else {
//printf("\nCall to zag_zag function\n");
zag_zag(root, key);
}
}
}
else if (key->parent != NULL) {
if (key->parent->left == key) {
//printf("\nCall to zig function\n");
zig(root, key);
} else {
//printf("\nCall to zag function\n");
zag(root, key);
}
}
}
return;
}
void InsertNode(st* root, char* data, char* mean){
st nn = (st)malloc(sizeof(node));
nn->left = NULL;
nn->right = NULL;
nn->parent = NULL;
strcpy(nn->word, data);
strcpy(nn->meaning, mean);
if(!(*root)){
*root = nn;
return;
}
node *p = NULL, *q = NULL;
p = *root;
while(p){
q = p;
if(strcmp(p->word, data) > 0)
p = p->left;
else if(strcmp(p->word, data) < 0)
p = p->right;
else
return;
}
nn->parent = q;
if(strcmp(q->word, data) > 0){
q->left = nn;
}
else{
q->right = nn;
}
Splay(root, nn);
return;
}
void Search(st* root, char* data){
int count = 1;
node* p = *root;
if(!p){
printf("No Details Found !!!\n");
return;
}
while (p != NULL){
if(strcmp(p->word, data) == 0){
printf("Word : %s Meaning : %s\n", p->word, p->meaning);
printf("Total number of Comparisions : %d\n", count);
Splay(root, p);
return;
}
else if(strcmp(p->word, data) > 0 ){
count++;
p = p->left;
}
else if(strcmp(p->word, data) < 0 ){
count++;
p = p->right;
}
}
printf("No Details Found !!!\n");
return;
}
void Delete(st* root, char* data){
node* p = *root;
if(!p){
printf("No Details Found !!!\n");
return;
}
while (p != NULL){
if(strcmp(p->word, data) == 0){
Splay(root, p);
node *del = NULL;
if(p->left == NULL){
del = p;
p = p->right;
p->parent = NULL;
}
else{
node *q = p->left;
while (q->right != NULL){
q = q->right;
}
Splay(&(p->left), q);
del = p;
p = p->left;
p->parent = NULL;
p->right = del->right;
del->right->parent = p;
}
*root = p;
free(del);
return;
}
else if(strcmp(p->word, data) > 0 ){
p = p->left;
}
else if(strcmp(p->word, data) < 0 ){
p = p->right;
}
}
printf("No Details Found !!!\n");
return;
}
void preorder(node *key, int *n){
if(!key)
return;
printf("| %-3d | %-16s | %-16s |\n", (*n)++, key->word, key->meaning);
preorder(key-> left, n);
preorder(key-> right, n);
return;
}