This repository has been archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
avl.c
510 lines (430 loc) · 11.5 KB
/
avl.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
/*
* Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
* 2002, 2003, 2004
* Ohio University.
*
* ---
*
* Starting with the release of tcptrace version 6 in 2001, tcptrace
* is licensed under the GNU General Public License (GPL). We believe
* that, among the available licenses, the GPL will do the best job of
* allowing tcptrace to continue to be a valuable, freely-available
* and well-maintained tool for the networking community.
*
* Previous versions of tcptrace were released under a license that
* was much less restrictive with respect to how tcptrace could be
* used in commercial products. Because of this, I am willing to
* consider alternate license arrangements as allowed in Section 10 of
* the GNU GPL. Before I would consider licensing tcptrace under an
* alternate agreement with a particular individual or company,
* however, I would have to be convinced that such an alternative
* would be to the greater benefit of the networking community.
*
* ---
*
* This file is part of Tcptrace.
*
* Tcptrace was originally written and continues to be maintained by
* Shawn Ostermann with the help of a group of devoted students and
* users (see the file 'THANKS'). The work on tcptrace has been made
* possible over the years through the generous support of NASA GRC,
* the National Science Foundation, and Sun Microsystems.
*
* Tcptrace is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Tcptrace is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tcptrace (in the file 'COPYING'); if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Author: Ramani Yellapragada
* School of Electrical Engineering and Computer Science
* Ohio University
* Athens, OH
* http://www.tcptrace.org/
*/
#include "tcptrace.h"
static char const GCC_UNUSED copyright[] =
"@(#)Copyright (c) 2004 -- Ohio University.\n";
static char const GCC_UNUSED rcsid[] =
"@(#)$Header$";
/* Local routines for handling AVL tree balancing after inserting a node */
static void SnapRotLeft(ptp_snap **n);
static void SnapRotRight(ptp_snap **n);
static enum AVLRES SnapLeftGrown(ptp_snap **n);
static enum AVLRES SnapRightGrown(ptp_snap **n);
/* Local routines for handling AVL tree balancing after removing a node */
static enum AVLRES SnapLeftShrunk(ptp_snap **n);
static enum AVLRES SnapRightShrunk(ptp_snap **n);
static int SnapFindHighest(ptp_snap *target, ptp_snap **n, enum AVLRES *res);
static int SnapFindLowest(ptp_snap *target, ptp_snap **n, enum AVLRES *res);
/*
* SnapRotLeft - perform counter clockwise rotation
*/
static void
SnapRotLeft(
ptp_snap **n)
{
ptp_snap *tmp = *n;
if (debug > 4)
printf("SnapRotLeft(): Rotating the AVL tree counter clockwise\n");
*n = (*n)->right;
tmp->right = (*n)->left;
(*n)->left = tmp;
}
/*
* SnapRotRight - perform clockwise rotation
*/
static void
SnapRotRight(
ptp_snap **n)
{
ptp_snap *tmp = *n;
if (debug > 4)
printf("SnapRotRight(): Rotating the AVL tree clockwise\n");
*n = (*n)->left;
tmp->left = (*n)->right;
(*n)->right = tmp;
}
/* SnapLeftGrown - For balancing an AVL tree after insertion
* Input is the address of a node. The node's left subtree has grown
* due to insertion.
*/
static enum AVLRES
SnapLeftGrown(
ptp_snap **n)
{
if (debug > 4)
printf("SnapLeftGrown(): Balancing the AVL tree because left subtree\
has grown after insertion\n");
switch ((*n)->skew) {
case LEFT:
if ((*n)->left->skew == LEFT) {
(*n)->skew = (*n)->left->skew = EQUAL1;
SnapRotRight(n);
}
else {
switch ((*n)->left->right->skew) {
case LEFT:
(*n)->skew = RIGHT;
(*n)->left->skew = EQUAL1;
break;
case RIGHT:
(*n)->skew = EQUAL1;
(*n)->left->skew = LEFT;
break;
default:
(*n)->skew = EQUAL1;
(*n)->left->skew = EQUAL1;
}
(*n)->left->right->skew = EQUAL1;
SnapRotLeft(&(*n)->left);
SnapRotRight(n);
}
return OK;
case RIGHT:
(*n)->skew = EQUAL1;
return OK;
default:
(*n)->skew = LEFT;
return BALANCE;
}
}
/* SnapRightGrown - For balancing an AVL tree after insertion
* Input is the address of a node. The node's right subtree has grown
* due to insertion.
*/
static enum AVLRES
SnapRightGrown(
ptp_snap **n)
{
if (debug > 4)
printf("SnapRightGrown(): Balancing the AVL tree because right subtree\
has grown after insertion\n");
switch ((*n)->skew) {
case LEFT:
(*n)->skew = EQUAL1;
return OK;
case RIGHT:
if ((*n)->right->skew == RIGHT) {
(*n)->skew = (*n)->right->skew = EQUAL1;
SnapRotLeft(n);
}
else {
switch ((*n)->right->left->skew) {
case RIGHT:
(*n)->skew = LEFT;
(*n)->right->skew = EQUAL1;
break;
case LEFT:
(*n)->skew = EQUAL1;
(*n)->right->skew = RIGHT;
break;
default:
(*n)->skew = EQUAL1;
(*n)->right->skew = EQUAL1;
}
(*n)->right->left->skew = EQUAL1;
SnapRotRight(&(*n)->right);
SnapRotLeft(n);
}
return OK;
default:
(*n)->skew = RIGHT;
return BALANCE;
}
}
/*
* SnapInsert - insert a node into the AVL tree
* and balance the AVL tree
*/
enum AVLRES
SnapInsert(
ptp_snap **root,
ptp_snap *new_node)
{
enum AVLRES tmp;
if (debug > 4)
printf("SnapInsert(): Inserting a node in the AVL tree\n");
if (!(*root)) {
*root = new_node;
(*root)->left = (*root)->right = NULL;
(*root)->skew = EQUAL1;
return BALANCE;
}
else if (AVL_WhichDir(&new_node->addr_pair, &((*root)->addr_pair)) == LT) {
if ((tmp = SnapInsert(&(*root)->left, new_node)) == BALANCE) {
return SnapLeftGrown(root);
}
return tmp;
}
else if (AVL_WhichDir(&new_node->addr_pair, &((*root)->addr_pair)) == RT) {
if ((tmp = SnapInsert(&(*root)->right, new_node)) == BALANCE) {
return SnapRightGrown(root);
}
return tmp;
}
return 0;
}
/*
* SnapLeftShrunk - For balancing an AVL tree after removing a node
* Input is the address of a node. The node's left subtree has shrunk
* due to removal and might have made the tree unbalanced.
*/
static enum AVLRES
SnapLeftShrunk(
ptp_snap **n)
{
if (debug > 4)
printf("SnapLeftshrunk(): Balancing the AVL tree because left subtree\
has shrunk after removal\n");
switch ((*n)->skew) {
case LEFT:
(*n)->skew = EQUAL1;
return BALANCE;
case RIGHT:
if ((*n)->right->skew == RIGHT) {
(*n)->skew = (*n)->right->skew = EQUAL1;
SnapRotLeft(n);
return BALANCE;
}
else if ((*n)->right->skew == EQUAL1) {
(*n)->skew = RIGHT;
(*n)->right->skew = LEFT;
SnapRotLeft(n);
return OK;
}
else {
switch ((*n)->right->left->skew) {
case LEFT:
(*n)->skew = EQUAL1;
(*n)->right->skew = RIGHT;
break;
case RIGHT:
(*n)->skew = LEFT;
(*n)->right->skew = EQUAL1;
break;
default:
(*n)->skew = EQUAL1;
(*n)->right->skew = EQUAL1;
}
(*n)->right->left->skew = EQUAL1;
SnapRotRight(& (*n)->right);
SnapRotLeft(n);
return BALANCE;
}
default:
(*n)->skew = RIGHT;
return OK;
}
}
/*
* SnapRightShrunk - For balancing an AVL tree after removing a node
* Input is the address of a node. The node's right subtree has shrunk
* due to removal and might have made the tree unbalanced.
*/
static enum AVLRES
SnapRightShrunk(
ptp_snap **n)
{
if (debug > 4)
printf("SnapRightShrunk(): Balancing the AVL tree because right subtree\
has shrunk after removal\n");
switch ((*n)->skew) {
case RIGHT:
(*n)->skew = EQUAL1;
return BALANCE;
case LEFT:
if ((*n)->left->skew == LEFT) {
(*n)->skew = (*n)->left->skew = EQUAL1;
SnapRotRight(n);
return BALANCE;
}
else if ((*n)->left->skew == EQUAL1) {
(*n)->skew = LEFT;
(*n)->left->skew = RIGHT;
SnapRotRight(n);
return OK;
}
else {
switch ((*n)->left->right->skew) {
case LEFT:
(*n)->skew = RIGHT;
(*n)->left->skew = EQUAL1;
break;
case RIGHT:
(*n)->skew = EQUAL1;
(*n)->left->skew = LEFT;
break;
default:
(*n)->skew = EQUAL1;
(*n)->left->skew = EQUAL1;
}
(*n)->left->right->skew = EQUAL1;
SnapRotLeft(& (*n)->left);
SnapRotRight(n);
return BALANCE;
}
default:
(*n)->skew = LEFT;
return OK;
}
}
/*
* SnapFindHighest - replace a node with a subtree's highest-ranking item
*/
static int
SnapFindHighest(
ptp_snap *target,
ptp_snap **n,
enum AVLRES *res)
{
ptp_snap *tmp;
if (debug > 4)
printf("SnapFindHighest(): Replacing a node with a subtree's\
highest ranking item \n");
*res = BALANCE;
if (! (*n)) {
return 0;
}
if ((*n)->right) {
if (!SnapFindHighest(target, & (*n)->right, res)) {
return 0;
}
if (*res == BALANCE) {
*res = SnapRightShrunk(n);
}
return 1;
}
target->addr_pair = (*n)->addr_pair;
target->ptp = (*n)->ptp;
tmp = *n;
(*n) = (*n)->left;
return 1;
}
/*
* SnapFindLowest - replace a node with a subtree's lowest-ranking item
*/
static int
SnapFindLowest(
ptp_snap *target,
ptp_snap **n,
enum AVLRES *res)
{
ptp_snap *tmp;
if (debug > 4)
printf("SnapFindLowest(): Replacing a node with a subtree's\
lowest ranking item \n");
*res = BALANCE;
if (!(*n)) {
return 0;
}
if ((*n)->left) {
if (!SnapFindLowest(target, & (*n)->left, res)) {
return 0;
}
if (*res == BALANCE) {
*res = SnapLeftShrunk(n);
}
return 1;
}
target->addr_pair = (*n)->addr_pair;
target->ptp = (*n)->ptp;
tmp = *n;
*n = (*n)->right;
return 1;
}
/*
* SnapRemove - remove a node from the AVL tree
* and balance the AVL tree
*/
enum AVLRES
SnapRemove(
ptp_snap **root,
tcp_pair_addrblock addr)
{
enum AVLRES tmp = BALANCE;
if (debug > 4)
printf("SnapRemove(): Removing a node from the AVL tree\n");
if (!(*root)) {
return 0;
}
if (AVL_WhichDir(&addr, &((*root)->addr_pair)) == LT) {
if ((tmp = SnapRemove(&(*root)->left, addr)) == BALANCE) {
return SnapLeftShrunk(root);
}
return tmp;
}
if (AVL_WhichDir(&addr, &((*root)->addr_pair)) == RT) {
if ((tmp = SnapRemove(&(*root)->right, addr)) == BALANCE) {
return SnapRightShrunk(root);
}
return tmp;
}
if ((*root)->left) {
if (SnapFindHighest(*root, &((*root)->left), &tmp)) {
if (tmp == BALANCE) {
tmp = SnapLeftShrunk(root);
}
return tmp;
}
}
if ((*root)->right) {
if (SnapFindLowest(*root, &((*root)->right), &tmp)) {
if (tmp == BALANCE) {
tmp = SnapRightShrunk(root);
}
return tmp;
}
}
*root = NULL;
return BALANCE;
}