-
Notifications
You must be signed in to change notification settings - Fork 84
/
CWAVL.c
520 lines (428 loc) · 14 KB
/
CWAVL.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include "CWCommon.h"
int compareEthAddr(unsigned char * staAddr, unsigned char * AVLaddr) {
int index=0;
for(index=0; index<ETH_ALEN; index++)
{
if((int)staAddr[index] > (int)AVLaddr[index])
return 1;
if((int)staAddr[index] < (int)AVLaddr[index])
return -1;
}
//Gli indirizzi sono uguali
return 0;
}
/*
remove all nodes of an AVL tree
*/
void AVLdispose(nodeAVL* t)
{
if( t != NULL )
{
AVLdispose( t->left );
AVLdispose( t->right );
free( t );
}
}
/*
find a specific nodeAVL's key in the tree
*/
nodeAVL* AVLfind(unsigned char * staAddr, nodeAVL* t )
{
if(staAddr == NULL || t == NULL )
return NULL;
if(compareEthAddr(staAddr, t->staAddr) < 0)
return AVLfind(staAddr, t->left );
else if(compareEthAddr(staAddr, t->staAddr) > 0)
return AVLfind(staAddr, t->right );
else
return t;
}
nodeAVL* AVLfindWTPNode(nodeAVL* t, int index)
{
if(t == NULL)
return NULL;
if(t->index == index)
return t;
AVLfindWTPNode(t->left, index);
AVLfindWTPNode(t->right, index);
/*
else if(compareEthAddr(staAddr, t->staAddr) < 0)
return AVLfindWTPNode(staAddr, t->left, index);
else if(compareEthAddr(staAddr, t->staAddr) > 0)
return AVLfindWTPNode(staAddr, t->right, index);
else
return NULL;
*/
}
/*
find minimum nodeAVL's key
*/
nodeAVL* AVLfind_min( nodeAVL* t )
{
if( t == NULL )
return NULL;
else if( t->left == NULL )
return t;
else
return AVLfind_min( t->left );
}
/*
find maximum nodeAVL's key
*/
nodeAVL* AVLfind_max( nodeAVL* t )
{
if( t != NULL )
while( t->right != NULL )
t = t->right;
return t;
}
/*
get the height of a nodeAVL
*/
int AVLheight( nodeAVL* n )
{
if( n == NULL )
return -1;
else
return n->height;
}
/*
get maximum value of two integers
*/
int AVLmax( int l, int r)
{
return l > r ? l: r;
}
/*
perform a rotation between a k2 nodeAVL and its left child
note: call single_rotate_with_left only if k2 nodeAVL has a left child
*/
nodeAVL* AVLsingle_rotate_with_left( nodeAVL* k2 )
{
nodeAVL* k1 = NULL;
k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
k2->height = AVLmax( AVLheight( k2->left ), AVLheight( k2->right ) ) + 1;
k1->height = AVLmax( AVLheight( k1->left ), k2->height ) + 1;
return k1; /* new root */
}
/*
perform a rotation between a nodeAVL (k1) and its right child
note: call single_rotate_with_right only if
the k1 nodeAVL has a right child
*/
nodeAVL* AVLsingle_rotate_with_right( nodeAVL* k1 )
{
nodeAVL* k2;
k2 = k1->right;
k1->right = k2->left;
k2->left = k1;
k1->height = AVLmax( AVLheight( k1->left ), AVLheight( k1->right ) ) + 1;
k2->height = AVLmax( AVLheight( k2->right ), k1->height ) + 1;
return k2; /* New root */
}
/*
perform the left-right double rotation,
note: call double_rotate_with_left only if k3 nodeAVL has
a left child and k3's left child has a right child
*/
nodeAVL* AVLdouble_rotate_with_left( nodeAVL* k3 )
{
/* Rotate between k1 and k2 */
k3->left = AVLsingle_rotate_with_right( k3->left );
/* Rotate between K3 and k2 */
return AVLsingle_rotate_with_left( k3 );
}
/*
perform the right-left double rotation
notes: call double_rotate_with_right only if k1 has a
right child and k1's right child has a left child
*/
nodeAVL* AVLdouble_rotate_with_right( nodeAVL* k1 )
{
/* rotate between K3 and k2 */
k1->right = AVLsingle_rotate_with_left( k1->right );
/* rotate between k1 and k2 */
return AVLsingle_rotate_with_right( k1 );
}
/*
insert a new nodeAVL into the tree
*/
nodeAVL* AVLinsert(int index, unsigned char * staAddr, unsigned char * BSSID, int radioID, nodeAVL* t )
{
if(staAddr == NULL)
return NULL;
if( t == NULL )
{
/* Create and return a one-nodeAVL tree */
t = (nodeAVL*)malloc(sizeof(nodeAVL));
if( t == NULL )
{
CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL);
return NULL;
}
t->index = index;
t->radioID = radioID;
CW_COPY_MEMORY(t->staAddr, staAddr,ETH_ALEN);
if(BSSID != NULL)
CW_COPY_MEMORY(t->BSSID, BSSID,ETH_ALEN);
t->height = 0;
t->left = t->right = NULL;
}
else if(compareEthAddr(staAddr, t->staAddr) < 0)
{
t->left = AVLinsert(index, staAddr, BSSID, radioID, t->left );
if( AVLheight( t->left ) - AVLheight( t->right ) == 2 )
if( compareEthAddr(staAddr, t->left->staAddr) < 0 )
t = AVLsingle_rotate_with_left( t );
else
t = AVLdouble_rotate_with_left( t );
}
else if(compareEthAddr(staAddr, t->staAddr) > 0)
{
t->right = AVLinsert(index, staAddr, BSSID, radioID, t->right );
if( AVLheight( t->right ) - AVLheight( t->left ) == 2 )
if( compareEthAddr(staAddr, t->right->staAddr) > 0)
t = AVLsingle_rotate_with_right( t );
else
t = AVLdouble_rotate_with_right( t );
}
/* Else X is in the tree already; we'll do nothing */
t->height = AVLmax( AVLheight( t->left ), AVLheight( t->right ) ) + 1;
return t;
}
/* Given a non-empty binary search tree, return the node with minimum
key value found in that tree. Note that the entire tree does not
need to be searched. */
struct nodeAVL * AVLminValueNode(struct nodeAVL* node)
{
struct nodeAVL* current = node;
/* loop down to find the leftmost leaf */
while (current->left != NULL)
current = current->left;
return current;
}
struct nodeAVL* AVLdeleteNode(struct nodeAVL* root, unsigned char * staAddr, int radioID)
{
if(staAddr == NULL)
return NULL;
// STEP 1: PERFORM STANDARD BST DELETE
if (root == NULL)
return root;
// If the key to be deleted is smaller than the root's key,
// then it lies in left subtree
if ( compareEthAddr(staAddr, root->staAddr) < 0 )
root->left = AVLdeleteNode(root->left, staAddr, radioID);
// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
else if( compareEthAddr(staAddr, root->staAddr) > 0 )
root->right = AVLdeleteNode(root->right, staAddr, radioID);
// if key is same as root's key, then This is the node
// to be deleted. If radioID is the same
else
{
if(root->radioID == radioID)
{
// node with only one child or no child
if(
((root->left == NULL) || (root->right == NULL)) &&
(root->radioID == radioID)
)
{
struct nodeAVL *temp = root->left ? root->left : root->right;
// No child case
if(temp == NULL)
{
temp = root;
root = NULL;
}
else // One child case
*root = *temp; // Copy the contents of the non-empty child
free(temp);
CWPrintEthernetAddress(staAddr, "STA deleted from AVL");
}
else
{
// node with two children: Get the inorder successor (smallest
// in the right subtree)
struct nodeAVL* temp = AVLminValueNode(root->right);
// Copy the inorder successor's data to this node
root->index = temp->index;
root->radioID = temp->radioID;
CW_COPY_MEMORY(root->staAddr, temp->staAddr, ETH_ALEN);
CW_COPY_MEMORY(root->BSSID, temp->BSSID, ETH_ALEN);
// Delete the inorder successor
root->right = AVLdeleteNode(root->right, temp->staAddr, temp->radioID);
}
}
else
{
CWLog("AVL find STA[%02x:%02x:%02x:%02x:%02x:%02x] node to delete, but radioID value (%d) is different from input radioID(%d). So AVL doesn't delete node", (int)staAddr[0], (int)staAddr[1], (int)staAddr[2], (int)staAddr[3], (int)staAddr[4], (int)staAddr[5],
root->radioID, radioID);
}
}
// If the tree had only one node then return
if (root == NULL)
return root;
// STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
root->height = AVLmax(AVLheight(root->left), AVLheight(root->right)) + 1;
// STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to check whether
// this node became unbalanced)
int balance = AVLgetBalance(root);
// If this node becomes unbalanced, then there are 4 cases
// Left Left Case
if (balance > 1 && AVLgetBalance(root->left) >= 0)
return AVLsingle_rotate_with_right(root);
// Left Right Case
if (balance > 1 && AVLgetBalance(root->left) < 0)
{
root->left = AVLsingle_rotate_with_left(root->left);
return AVLsingle_rotate_with_right(root);
}
// Right Right Case
if (balance < -1 && AVLgetBalance(root->right) <= 0)
return AVLsingle_rotate_with_left(root);
// Right Left Case
if (balance < -1 && AVLgetBalance(root->right) > 0)
{
root->right = AVLsingle_rotate_with_right(root->right);
return AVLsingle_rotate_with_left(root);
}
return root;
}
struct nodeAVL* AVLdeleteNodeWithoutRadioID(struct nodeAVL* root, struct nodeAVL* nodeToDelete)
{
if(nodeToDelete->staAddr == NULL)
return NULL;
// STEP 1: PERFORM STANDARD BST DELETE
if (root == NULL)
return root;
// If the key to be deleted is smaller than the root's key,
// then it lies in left subtree
if ( compareEthAddr(nodeToDelete->staAddr, root->staAddr) < 0 )
root->left = AVLdeleteNodeWithoutRadioID(root->left, nodeToDelete);
// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
else if( compareEthAddr(nodeToDelete->staAddr, root->staAddr) > 0 )
root->right = AVLdeleteNodeWithoutRadioID(root->right, nodeToDelete);
// if key is same as root's key, then This is the node
// to be deleted. If radioID is the same
else
{
// node with only one child or no child
if((root->left == NULL) || (root->right == NULL))
{
struct nodeAVL *temp = root->left ? root->left : root->right;
CWPrintEthernetAddress(root->staAddr, "Delete STA from AVL");
// No child case
if(temp == NULL)
{
temp = root;
root = NULL;
}
else // One child case
{
*root = *temp; // Copy the contents of the non-empty child
}
free(temp);
temp = NULL;
}
else
{
// node with two children: Get the inorder successor (smallest
// in the right subtree)
struct nodeAVL* temp = AVLminValueNode(root->right);
CWPrintEthernetAddress(root->staAddr, "Removing STA from AVL");
// Copy the inorder successor's data to this node
root->index = temp->index;
root->radioID = temp->radioID;
CW_COPY_MEMORY(root->staAddr, temp->staAddr, ETH_ALEN);
CW_COPY_MEMORY(root->BSSID, temp->BSSID, ETH_ALEN);
CWPrintEthernetAddress(temp->staAddr, "Node to move");
// Delete the inorder successor
root->right = AVLdeleteNodeWithoutRadioID(root->right, temp);
}
}
// If the tree had only one node then return
if (root == NULL)
return root;
CWLog("root != NULL");
// STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
root->height = AVLmax(AVLheight(root->left), AVLheight(root->right)) + 1;
// STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to check whether
// this node became unbalanced)
int balance = AVLgetBalance(root);
// If this node becomes unbalanced, then there are 4 cases
// Left Left Case
if (balance > 1 && AVLgetBalance(root->left) >= 0)
return AVLsingle_rotate_with_right(root);
// Left Right Case
if (balance > 1 && AVLgetBalance(root->left) < 0)
{
root->left = AVLsingle_rotate_with_left(root->left);
return AVLsingle_rotate_with_right(root);
}
// Right Right Case
if (balance < -1 && AVLgetBalance(root->right) <= 0)
return AVLsingle_rotate_with_left(root);
// Right Left Case
if (balance < -1 && AVLgetBalance(root->right) > 0)
{
root->right = AVLsingle_rotate_with_right(root->right);
return AVLsingle_rotate_with_left(root);
}
return root;
}
/*
data data of a nodeAVL
*/
/*
int get(nodeAVL* n)
{
return n->data;
}
*/
// Get Balance factor of node N
int AVLgetBalance(struct nodeAVL *N)
{
if (N == NULL)
return 0;
return AVLheight(N->left) - AVLheight(N->right);
}
/*
Recursively display AVL tree or subtree
*/
void AVLdisplay_avl(nodeAVL* t)
{
if (t == NULL)
return;
CWLog("[%d] - %02x:%02x:%02x:%02x:%02x:%02x",t->index, (int)t->staAddr[0], (int)t->staAddr[1], (int)t->staAddr[2], (int)t->staAddr[3], (int)t->staAddr[4], (int)t->staAddr[5]);
if(t->left != NULL)
CWLog("[L: %d] - %02x:%02x:%02x:%02x:%02x:%02x",t->left->index, (int)t->left->staAddr[0], (int)t->left->staAddr[1], (int)t->left->staAddr[2], (int)t->left->staAddr[3], (int)t->left->staAddr[4], (int)t->left->staAddr[5]);
if(t->right != NULL)
CWLog("[R: %d] - %02x:%02x:%02x:%02x:%02x:%02x",t->right->index, (int)t->right->staAddr[0], (int)t->right->staAddr[1], (int)t->right->staAddr[2], (int)t->right->staAddr[3], (int)t->right->staAddr[4], (int)t->right->staAddr[5]);
CWLog("\n");
AVLdisplay_avl(t->left);
AVLdisplay_avl(t->right);
}
/*
nodeAVL * AVLremoveWTP(nodeAVL* root, int WTPIndex)
{
if (root == NULL)
return NULL;
while(root != NULL)
{
if(root->index == WTPIndex)
AVLdeleteNode(struct nodeAVL* root, unsigned char * staAddr, int radioID)
}
CWLog("[%d] - %02x:%02x:%02x:%02x:%02x:%02x",t->index, (int)t->staAddr[0], (int)t->staAddr[1], (int)t->staAddr[2], (int)t->staAddr[3], (int)t->staAddr[4], (int)t->staAddr[5]);
if(t->left != NULL)
CWLog("[L: %d] - %02x:%02x:%02x:%02x:%02x:%02x",t->left->index, (int)t->left->staAddr[0], (int)t->left->staAddr[1], (int)t->left->staAddr[2], (int)t->left->staAddr[3], (int)t->left->staAddr[4], (int)t->left->staAddr[5]);
if(t->right != NULL)
CWLog("[R: %d] - %02x:%02x:%02x:%02x:%02x:%02x",t->right->index, (int)t->right->staAddr[0], (int)t->right->staAddr[1], (int)t->right->staAddr[2], (int)t->right->staAddr[3], (int)t->right->staAddr[4], (int)t->right->staAddr[5]);
CWLog("\n");
AVLdisplay_avl(t->left);
AVLdisplay_avl(t->right);
}
*/