Changeset 1b20da0 in mainline for kernel/generic/src/adt/avl.c
- Timestamp:
- 2018-02-28T17:52:03Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 3061bc1
- Parents:
- df6ded8
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:26:03)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:52:03)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/adt/avl.c
rdf6ded8 r1b20da0 76 76 else if (p->key < key) 77 77 p = p->rgt; 78 else 78 else 79 79 return p; 80 80 } … … 102 102 * Iteratively descend to the leftmost leaf in the tree. 103 103 */ 104 while (p->lft != NULL) 104 while (p->lft != NULL) 105 105 p = p->lft; 106 106 … … 156 156 * @param t AVL tree. 157 157 * @param newnode New node to be inserted. 158 */ 158 */ 159 159 void avltree_insert(avltree_t *t, avltree_node_t *newnode) 160 { 161 avltree_node_t *par; 160 { 161 avltree_node_t *par; 162 162 avltree_node_t *gpa; 163 163 avltree_node_t *top; … … 170 170 /* 171 171 * Creating absolute key. 172 */ 172 */ 173 173 key = newnode->key + t->base; 174 174 … … 223 223 if (top->par == NULL) { 224 224 dpc = &t->root; 225 } else { 225 } else { 226 226 if (top->par->lft == top) 227 227 dpc = &top->par->lft; … … 255 255 */ 256 256 REBALANCE_INSERT_LL(); 257 } else { 257 } else { 258 258 /* 259 259 * LR rotation. … … 263 263 REBALANCE_INSERT_LR(); 264 264 } 265 } else if (top->balance == 2) { 265 } else if (top->balance == 2) { 266 266 par = top->rgt; 267 267 if (par->balance == 1) { … … 278 278 REBALANCE_INSERT_RL(); 279 279 } 280 } else { 280 } else { 281 281 /* 282 282 * Balance is not broken, insertion is finised. … … 303 303 * @param ro Read only operation; do not modify any tree pointers. This is 304 304 * useful for tracking direction via the dir pointer. 305 * 305 * 306 306 * @return Zero if w became the new root of the tree, otherwise return 307 307 * non-zero. … … 313 313 if (u->par == NULL) { 314 314 if (!ro) 315 t->root = w; 315 t->root = w; 316 316 return 0; 317 } else { 317 } else { 318 318 if (u->par->lft == v) { 319 319 if (!ro) … … 362 362 * Because multiple identical keys are allowed, the parent pointers are 363 363 * essential during deletion. 364 * 364 * 365 365 * @param t AVL tree structure. 366 366 * @param node Address of the node which will be deleted. … … 379 379 if (node->rgt) { 380 380 /* 381 * Replace the node with its only right son. 381 * Replace the node with its only right son. 382 382 * 383 383 * Balance of the right son will be repaired in the … … 388 388 gpa = cur; 389 389 dir = RIGHT; 390 cur->balance = node->balance; 390 cur->balance = node->balance; 391 391 } else { 392 392 if (node->par == NULL) { … … 421 421 * Cutting off of the found node has two cases that 422 422 * depend on its left son. 423 */ 423 */ 424 424 if (cur->lft) { 425 425 /* … … 429 429 gpa->par = cur->par; 430 430 dir = LEFT; 431 gpa->balance = cur->balance; 431 gpa->balance = cur->balance; 432 432 } else { 433 433 dir = RIGHT; … … 436 436 cur->par->rgt = cur->lft; 437 437 cur->lft = node->lft; 438 cur->lft->par = cur; 439 } else { 438 cur->lft->par = cur; 439 } else { 440 440 /* 441 441 * The left son of the node hasn't got a right son. The … … 443 443 */ 444 444 dir = LEFT; 445 gpa = cur; 445 gpa = cur; 446 446 } 447 447 if (node->rgt) 448 node->rgt->par = cur; 448 node->rgt->par = cur; 449 449 cur->rgt = node->rgt; 450 450 cur->balance = node->balance; … … 473 473 */ 474 474 break; 475 } else if (gpa->balance == 2) { 475 } else if (gpa->balance == 2) { 476 476 /* 477 477 * Bad balance, heights of left and right … … 538 538 (void) repair(t, par, gpa, par, 539 539 NULL, false); 540 break; 540 break; 541 541 } else { 542 542 par->balance = 0; … … 559 559 gpa = gpa->par; 560 560 } 561 } else { 561 } else { 562 562 /* 563 563 * Deletion was made in the right subtree. … … 605 605 break; 606 606 gpa = cur->par; 607 } else { 607 } else { 608 608 /* 609 609 * LL rotation. … … 681 681 node = node->lft; 682 682 683 avltree_delete(t, node); 683 avltree_delete(t, node); 684 684 685 685 return true;
Note:
See TracChangeset
for help on using the changeset viewer.