-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebalance.c
269 lines (253 loc) · 7.77 KB
/
rebalance.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
/*
+------------------------------------------------------------------+
| Copyright (C) 2023 Roger P. Johnson, [email protected] |
| |
| This file is part of libbst. |
| |
| libbst is free software: you can redistribute it and/or modify |
| it under the terms of the GNU Lessor General Public License as |
| published by the Free Software Foundation, either version 3 of |
| the License, or (at your option) any later version. |
| |
| libbst 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 Lessor General Public License for more details. |
| |
| You should have received a copy of the GNU Lessor General Public |
| License along with libbst. If not, see www.gnu.org/licenses. |
+------------------------------------------------------------------+
*/
#ifndef BST_HDR
#include "bst.h"
#endif
static char *RCSid[] = { "$Id: rebalance.c,v 2.2 1999/01/27 01:17:06 roger Exp $" };
void rbal(t_node ** treeroot, t_node * a, t_node * f, t_node * q, t_node * b, int d)
{
/*******************************************************************************
* A private library function that will rbal the AVL tree to a balanced state
* after an insertion is performed and leaves the tree unbalanced.
*
* Input Parameters
* =================
* a : Pointer to pointer (t_node) to the nearest ancestor
* with a balance factor equal to +1 or -1.
* b : Pointer to pointer (t_node) to a's left or right
* subtree depending on whether the new node was inserted in
* a.left or a.right subtree.
* d : Flag for which side of the subtree of a the new node was
* inserted on.
* d = +1: right side; d = -1: left side.
* f : Pointer to pointer (t_node) to the parent of a.
* q : Pointer to pointer (t_node) to the parent of new node.
* treeroot : Pointer to pointer (t_node) to the current root tree.
*
* Output Parameters
* =================
* None. Tree is rbald to a balanced state via rotations.
*
* Global Variables
* =================
* None.
*******************************************************************************/
t_node *c;
/* Upon Entry:
* d = +1 implies a left imbalance in the tree: H(l)-H(r) = +1
* d = -1 implies a right imbalance in the tree: H(l)-H(r) = -1
* a is pointing to the nearest ancestor with a balance factor + or - 1
* f is a's parent node or NULL
* b is pointing to a's left subtree (d = +1) or a's right subtree (d = -1)
* c replaces a as the parent node of the subtree being rotated
*/
if (d == +1)
/* Left imbalance H(l)-H(r) = +1 */
if (b->tn_bf == +1) {
/* LL ROTATION */
#ifdef DEBUG_SHOWREBALANCE
printf(">> LL << rotation\n");
#endif
/* LINKS */
a->tn_llink = b->tn_rlink;
b->tn_rlink = a;
b->tn_ulink = a->tn_ulink;
a->tn_ulink = b;
if (a->tn_llink != NULL)
a->tn_llink->tn_ulink = a;
/* TAGS */
b->tn_tag = a->tn_tag;
a->tn_tag = RIGHT_SON;
if (a->tn_llink != NULL)
a->tn_llink->tn_tag = LEFT_SON;
/* BALANCE FACTORS */
a->tn_bf = 0;
b->tn_bf = 0;
} else {
/* LR ROTATION */
/* LINKS */
c = b->tn_rlink;
b->tn_rlink = c->tn_llink;
a->tn_llink = c->tn_rlink;
c->tn_llink = b; /* c is now new root of subtree */
c->tn_rlink = a;
switch (c->tn_bf) { /* readjust affected balance factors */
/* LR(b) */
case +1: /* UPLINKS */
#ifdef DEBUG_SHOWREBALANCE
printf(">> LR(b) << rotation\n");
#endif
c->tn_ulink = a->tn_ulink;
b->tn_ulink = c;
a->tn_ulink = c;
b->tn_rlink->tn_ulink = b;
if (a->tn_llink != NULL)
a->tn_llink->tn_ulink = a;
/* TAGS */
c->tn_tag = a->tn_tag;
a->tn_tag = RIGHT_SON;
b->tn_rlink->tn_tag = RIGHT_SON;
if (a->tn_llink != NULL)
a->tn_llink->tn_tag = LEFT_SON;
/* BALANCE FACTORS */
a->tn_bf = -1;
b->tn_bf = 0;
break;
/* LR(a) */
case 0: /* UPLINKS */
#ifdef DEBUG_SHOWREBALANCE
printf(">> LR(a) << rotation\n");
#endif
c->tn_ulink = a->tn_ulink;
b->tn_ulink = c;
a->tn_ulink = c;
/* TAGS */
c->tn_tag = a->tn_tag;
a->tn_tag = RIGHT_SON;
/* BALANCE FACTORS */
a->tn_bf = 0;
b->tn_bf = 0;
break;
/* LR(c) */
case -1: /* UPLINKS */
#ifdef DEBUG_SHOWREBALANCE
printf(">> LR(c) << rotation\n");
#endif
c->tn_ulink = a->tn_ulink;
b->tn_ulink = c;
a->tn_ulink = c;
a->tn_llink->tn_ulink = a;
if (b->tn_rlink != NULL)
b->tn_rlink->tn_ulink = b;
/* TAGS */
c->tn_tag = a->tn_tag;
a->tn_tag = RIGHT_SON;
a->tn_llink->tn_tag = LEFT_SON;
if (b->tn_rlink != NULL)
b->tn_rlink->tn_tag = RIGHT_SON;
/* BALANCE FACTORS */
a->tn_bf = 0;
b->tn_bf = 1;
break;
} /* switch */
c->tn_bf = 0;
b = c; /* copy pointer for wrap up at the end */
} /* else */
else /* d = -1 */
/* right imbalance H(l)-H(r) = -1 */ if (b->tn_bf == -1) {
/* RR ROTATION */
#ifdef DEBUG_SHOWREBALANCE
printf(">> RR << rotation\n");
#endif
/* LINKS */
a->tn_rlink = b->tn_llink;
b->tn_llink = a;
/* UP LINKS */
b->tn_ulink = a->tn_ulink;
a->tn_ulink = b;
if (a->tn_rlink != NULL)
a->tn_rlink->tn_ulink = a;
/* TAGS */
b->tn_tag = a->tn_tag;
a->tn_tag = LEFT_SON;
if (a->tn_rlink != NULL)
a->tn_rlink->tn_tag = RIGHT_SON;
/* BALANCE FACTORS */
a->tn_bf = 0;
b->tn_bf = 0;
} else {
/* RL ROTATION */
/* LINKS */
c = b->tn_llink;
b->tn_llink = c->tn_rlink;
a->tn_rlink = c->tn_llink;
c->tn_rlink = b; /* c is now new parent of subtree */
c->tn_llink = a;
switch (c->tn_bf) { /* readjust affected balance factors */
/* RL(c) */
case +1: /* UP LINKS */
#ifdef DEBUG_SHOWREBALANCE
printf(">> RL(c) << rotation\n");
#endif
c->tn_ulink = a->tn_ulink;
a->tn_ulink = c;
b->tn_ulink = c;
a->tn_rlink->tn_ulink = a;
if (b->tn_llink != NULL)
b->tn_llink->tn_ulink = b;
/* TAGS */
c->tn_tag = a->tn_tag;
a->tn_tag = LEFT_SON;
a->tn_rlink->tn_tag = RIGHT_SON;
if (b->tn_llink != NULL)
b->tn_llink->tn_tag = LEFT_SON;
/* BALANCE FACTORS */
a->tn_bf = 0;
b->tn_bf = -1;
break;
/* RL(a) */
case 0: /* UP LINKS */
#ifdef DEBUG_SHOWREBALANCE
printf(">> RL(a) << rotation\n");
#endif
c->tn_ulink = a->tn_ulink;
a->tn_ulink = c;
b->tn_ulink = c;
/* TAGS */
c->tn_tag = a->tn_tag;
a->tn_tag = LEFT_SON;
/* BALANCE FACTORS */
a->tn_bf = 0;
b->tn_bf = 0;
break;
/* RL(b) */
case -1: /* UP LINKS */
#ifdef DEBUG_SHOWREBALANCE
printf(">> RL(b) << rotation\n");
#endif
c->tn_ulink = a->tn_ulink;
a->tn_ulink = c;
b->tn_ulink = c;
b->tn_llink->tn_ulink = b;
if (a->tn_rlink != NULL)
a->tn_rlink->tn_ulink = a;
/* TAGS */
c->tn_tag = a->tn_tag;
a->tn_tag = LEFT_SON;
b->tn_llink->tn_tag = LEFT_SON;
if (a->tn_rlink != NULL)
a->tn_rlink->tn_tag = RIGHT_SON;
/* BALANCE FACTORS */
a->tn_bf = +1;
b->tn_bf = 0;
break;
} /* switch */
c->tn_bf = 0;
b = c; /* copy pointer for wrap up at the end */
} /* else */
/* wrap up */
if (f == NULL)
*treeroot = b; /* new tree root */
else if (a == f->tn_llink)
f->tn_llink = b; /* finish linking from f to new f.left LL */
else if (a == f->tn_rlink)
f->tn_rlink = b; /* finish linking from f to new f.right LL */
}