-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl_tree_string_mapping.c
309 lines (257 loc) · 6.41 KB
/
avl_tree_string_mapping.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
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
#include "avl_tree_string_mapping.h"
unsigned int ground_found; //flag
unsigned int current_mapping; //the latest mapping used, either new or old
Avl_node *root_node;
int insert_node(char *x) {
int i;
/*check whether the node is the ground */
if(!strcmp(x, "0")) {
ground_found = 1;
return 0;
}
for(i = 0; x[i] != '\0'; i++){ //Convert to lower Case
x[i] = tolower(x[i]);
}
root_node = insert(root_node, x);
return current_mapping;
}
inline Avl_node * insert(Avl_node *T, char *x) {
if(T == NULL) { //new node
T = (Avl_node*)malloc(sizeof(Avl_node));
if(T == NULL) {
printf("Could not allocate new node.\n");
printf("Terminating.\n");
exit(-1);
}
strcpy(T->name, x);
current_mapping = T->mapping = ++circuit_simulation.number_of_nodes;
T->left=NULL;
T->right=NULL;
#if DEBUG
printf("Node Mapping, %s -> %d.\n", T -> name, T -> mapping);
#endif
circuit_simulation.plot_settings = (plotSettings *) realloc(circuit_simulation.plot_settings, (circuit_simulation.number_of_nodes + 1) * sizeof(plotSettings));
if(circuit_simulation.plot_settings == NULL) {
printf("Could not allocate memory.\n");
printf("Terminating.\n");
exit(-1);
}
strcpy((circuit_simulation.plot_settings + circuit_simulation.number_of_nodes) -> name, T -> name);
(circuit_simulation.plot_settings + circuit_simulation.number_of_nodes) -> dc_sweep_plot = DISABLED;
(circuit_simulation.plot_settings + circuit_simulation.number_of_nodes) -> transient_plot = DISABLED;
(circuit_simulation.plot_settings + circuit_simulation.number_of_nodes) -> ac_plot = DISABLED;
}
else {
if(strcasecmp(x, T->name) > 0) {// insert in right subtree
T->right=insert(T->right, x);
if(BF(T)==-2) {
if(strcasecmp(x, T->right->name) > 0) {
T=RR(T);
}
else {
T=RL(T);
}
}
}
else {
if(strcasecmp(x, T->name) < 0) { // insert in left subtree
T->left=insert(T->left, x);
if(BF(T)==2) {
if(strcasecmp(x, T->left->name) < 0) {
T=LL(T);
}
else {
T=LR(T);
}
}
}
else { //already in the tree
current_mapping = T-> mapping;
}
}
}
T->ht=height(T);
return(T);
}
int search_for_node(char *x) {
int i;
if(!strcmp(x, "0") && ground_found == 0) { // requested ground for plotting, but ground was not found yet
return -1;
}
else if(!strcmp(x, "0") && ground_found == 1) { // ground was found, return its mapping
return 0;
}
for(i = 0; x[i] != '\0'; i++){ //Convert to lower Case
x[i] = tolower(x[i]);
}
return search_for_node_mapping(root_node, x);
}
inline int search_for_node_mapping(Avl_node *T, char *x) {
if(T == NULL) {
return -1;
}
else {
if(strcmp(x, T->name) > 0) {
return search_for_node_mapping(T->right, x);
}
else if(strcmp(x, T->name) < 0) {
return search_for_node_mapping(T->left, x);
}
else {
return T -> mapping;
}
}
}
void free_avl_tree(void) {
free_tree_space(root_node);
}
inline void free_tree_space(Avl_node *T) {
if(T != NULL) {
free_tree_space(T->left);
free_tree_space(T->right);
free(T);
}
}
inline Avl_node * Delete(Avl_node *T, char *x)
{ Avl_node *p;
if(T==NULL)
{
return NULL;
}
else
if(strcmp(x, T->name) > 0)
{
T->right=Delete(T->right, x);
if(BF(T)==2) {
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
}
else
if(strcmp(x, T->name) < 0)
{
T->left=Delete(T->left,x);
if(BF(T)==-2) {//Rebalance during windup
if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
}
else
{
//data to be deleted is found
if(T->right !=NULL)
{ //delete its inorder succesor
p=T->right;
while(p->left != NULL)
p=p->left;
strcpy(T->name, p->name);
T->right=Delete(T->right,p->name);
if(BF(T)==2) {//Rebalance during windup
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
}
else
return(T->left);
}
T->ht=height(T);
return(T);
}
inline int height(Avl_node *T)
{
int lh,rh;
if(T==NULL)
return(0);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
if(lh>rh)
return(lh);
return(rh);
}
inline Avl_node * rotateright(Avl_node *x)
{
Avl_node *y;
y=x->left;
x->left=y->right;
y->right=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
inline Avl_node * rotateleft(Avl_node *x)
{
Avl_node *y;
y=x->right;
x->right=y->left;
y->left=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
inline Avl_node * RR(Avl_node *T)
{
T=rotateleft(T);
return(T);
}
inline Avl_node * LL(Avl_node *T)
{
T=rotateright(T);
return(T);
}
inline Avl_node * LR(Avl_node *T)
{
T->left=rotateleft(T->left);
T=rotateright(T);
return(T);
}
inline Avl_node * RL(Avl_node *T)
{
T->right=rotateright(T->right);
T=rotateleft(T);
return(T);
}
inline int BF(Avl_node *T)
{
int lh,rh;
if(T==NULL)
return(0);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
return(lh-rh);
}
void preorder(Avl_node *T)
{
if(T!=NULL)
{
printf(" %s(Bf=%d)",T->name,BF(T));
preorder(T->left);
preorder(T->right);
}
}
void inorder(Avl_node *T)
{
if(T!=NULL)
{
inorder(T->left);
printf(" %s(Bf=%d)",T->name,BF(T));
inorder(T->right);
}
}