Changeset 83a5cba in mainline
- Timestamp:
- 2007-07-29T18:18:37Z (18 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- b76a2217
- Parents:
- 5dcee525
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/adt/avl.c
r5dcee525 r83a5cba 108 108 } 109 109 110 #define REBALANCE_INSERT_XX(DIR1, DIR2) \ 111 top->DIR1 = par->DIR2; \ 112 if (top->DIR1 != NULL) \ 113 top->DIR1->par = top; \ 114 par->par = top->par; \ 115 top->par = par; \ 116 par->DIR2 = top; \ 117 par->balance = 0; \ 118 top->balance = 0; \ 119 *dpc = par; 120 121 #define REBALANCE_INSERT_LL() REBALANCE_INSERT_XX(lft, rgt) 122 #define REBALANCE_INSERT_RR() REBALANCE_INSERT_XX(rgt, lft) 123 124 #define REBALANCE_INSERT_XY(DIR1, DIR2, SGN) \ 125 gpa = par->DIR2; \ 126 par->DIR2 = gpa->DIR1; \ 127 if (gpa->DIR1 != NULL) \ 128 gpa->DIR1->par = par; \ 129 gpa->DIR1 = par; \ 130 par->par = gpa; \ 131 top->DIR1 = gpa->DIR2; \ 132 if (gpa->DIR2 != NULL) \ 133 gpa->DIR2->par = top; \ 134 gpa->DIR2 = top; \ 135 gpa->par = top->par; \ 136 top->par = gpa; \ 137 \ 138 if (gpa->balance == -1 * SGN) { \ 139 par->balance = 0; \ 140 top->balance = 1 * SGN; \ 141 } else if (gpa->balance == 0) { \ 142 par->balance = 0; \ 143 top->balance = 0; \ 144 } else { \ 145 par->balance = -1 * SGN; \ 146 top->balance = 0; \ 147 } \ 148 gpa->balance = 0; \ 149 *dpc = gpa; 150 151 #define REBALANCE_INSERT_LR() REBALANCE_INSERT_XY(lft, rgt, 1) 152 #define REBALANCE_INSERT_RL() REBALANCE_INSERT_XY(rgt, lft, -1) 153 110 154 /** Insert new node into AVL tree. 111 155 * … … 146 190 147 191 /* 148 * Initialize new node.192 * Initialize the new node. 149 193 */ 150 194 newnode->key = key; … … 163 207 164 208 /* 165 * Insert new node into previously found leaf place.209 * Insert the new node into the previously found leaf position. 166 210 */ 167 211 *dpc = newnode; … … 210 254 * LL rotation. 211 255 */ 212 top->lft = par->rgt; 213 if (top->lft != NULL) 214 top->lft->par = top; 215 par->par = top->par; 216 top->par = par; 217 par->rgt = top; 218 par->balance = 0; 219 top->balance = 0; 220 *dpc = par; 256 REBALANCE_INSERT_LL(); 221 257 } else { 222 258 /* … … 225 261 ASSERT(par->balance == 1); 226 262 227 gpa = par->rgt; 228 par->rgt = gpa->lft; 229 if (gpa->lft != NULL) 230 gpa->lft->par = par; 231 gpa->lft = par; 232 par->par = gpa; 233 top->lft = gpa->rgt; 234 if (gpa->rgt != NULL) 235 gpa->rgt->par = top; 236 gpa->rgt = top; 237 gpa->par = top->par; 238 top->par = gpa; 239 240 if (gpa->balance == -1) { 241 par->balance = 0; 242 top->balance = 1; 243 } else if (gpa->balance == 0) { 244 par->balance = 0; 245 top->balance = 0; 246 } else { 247 par->balance = -1; 248 top->balance = 0; 249 } 250 gpa->balance = 0; 251 *dpc = gpa; 263 REBALANCE_INSERT_LR(); 252 264 } 253 265 } else if (top->balance == 2) { … … 257 269 * RR rotation. 258 270 */ 259 top->rgt = par->lft; 260 if (top->rgt != NULL) 261 top->rgt->par = top; 262 par->par = top->par; 263 top->par = par; 264 par->lft = top; 265 par->balance = 0; 266 top->balance = 0; 267 *dpc = par; 271 REBALANCE_INSERT_RR(); 268 272 } else { 269 273 /* … … 271 275 */ 272 276 ASSERT(par->balance == -1); 273 274 gpa = par->lft; 275 par->lft = gpa->rgt; 276 if (gpa->rgt != NULL) 277 gpa->rgt->par = par; 278 gpa->rgt = par; 279 par->par = gpa; 280 top->rgt = gpa->lft; 281 if (gpa->lft != NULL) 282 gpa->lft->par = top; 283 gpa->lft = top; 284 gpa->par = top->par; 285 top->par = gpa; 286 287 if (gpa->balance == 1) { 288 par->balance = 0; 289 top->balance = -1; 290 } else if (gpa->balance == 0) { 291 par->balance = 0; 292 top->balance = 0; 293 } else { 294 par->balance = 1; 295 top->balance = 0; 296 } 297 gpa->balance = 0; 298 *dpc = gpa; 277 278 REBALANCE_INSERT_RL(); 299 279 } 300 280 } else { … … 352 332 } 353 333 354 #define REBALANCE (DIR1, DIR2, SIGN)\334 #define REBALANCE_DELETE(DIR1, DIR2, SIGN) \ 355 335 if (cur->balance == -1 * SIGN) { \ 356 336 par->balance = 0; \ … … 375 355 cur->balance = 0; 376 356 377 #define REBALANCE_ LR() REBALANCE(lft, rgt, 1)378 #define REBALANCE_ RL() REBALANCE(rgt, lft, -1)357 #define REBALANCE_DELETE_LR() REBALANCE_DELETE(lft, rgt, 1) 358 #define REBALANCE_DELETE_RL() REBALANCE_DELETE(rgt, lft, -1) 379 359 380 360 /** Delete a node from the AVL tree. … … 516 496 * factor of the grand child (cur). 517 497 */ 518 REBALANCE_ RL();498 REBALANCE_DELETE_RL(); 519 499 520 500 /* … … 612 592 * factor of the grand child (cur). 613 593 */ 614 REBALANCE_ LR();594 REBALANCE_DELETE_LR(); 615 595 616 596 /*
Note:
See TracChangeset
for help on using the changeset viewer.