Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. /* Delete the the node and the info within it */
  2. void avlDeleteNode(AVLTree *tree, AVLNode *node)
  3. {
  4. tree->destroyElem(node->elem);
  5. free(node);
  6. }
  7.  
  8. /* Rebalance the tree after delete */
  9. void avlDeleteFixUp(AVLTree* tree, AVLNode* y)
  10. {
  11. while (y != tree->root) {
  12. /* Update the height and get the balance factor */
  13. y->height = max(y->l->height, y->r->height) + 1;
  14. int balance = avlGetBalance(y);
  15.  
  16. /* Rebalance the tree */
  17. if (balance > 1) {
  18. if (avlGetBalance(y->l) >= 0) {
  19. /* Single right rotation */
  20. avlRightRotate(tree, y);
  21. } else {
  22. /* Double right rotation */
  23. avlLeftRotate(tree, y->l);
  24. avlRightRotate(tree, y);
  25. }
  26. } else if (balance < -1) {
  27. if (avlGetBalance(y->l) <= 0) {
  28. /* Single left rotation */
  29. avlLeftRotate(tree, y);
  30. } else {
  31. /* Double left rotation */
  32. avlRightRotate(tree, y->r);
  33. avlLeftRotate(tree, y);
  34. }
  35. }
  36.  
  37. /* Move upward */
  38. y = y->p;
  39. }
  40. }
  41.  
  42. /* Delete a node from the tree or from the list */
  43. void avlDelete(AVLTree* tree, Item1 elem)
  44. {
  45. /* Get the node we need to delete */
  46. /* sNode = searchedNode */
  47. /* pNode = parentNode */
  48. /* cNode = childNode */
  49. AVLNode *sNode = avlExactQuery(tree, elem);
  50. AVLNode *pNode = sNode->p;
  51. AVLNode *cNode;
  52. tree->size--;
  53.  
  54. /* Duplicate case: delete a node from the list */
  55. if (sNode != sNode->end) {
  56. /* Set up the new links betweekn the nodes */
  57. sNode->next = sNode->end->next;
  58. sNode->end->next->prev = sNode;
  59.  
  60. /* Create a copy & move the end & destroy */
  61. AVLNode *toDelete = sNode->end;
  62. sNode->end = sNode->end->prev;
  63. avlDeleteNode(tree, toDelete);
  64. return;
  65. }
  66.  
  67. /* Get the number of the childs of the node */
  68. int childsNumber = (sNode->l != tree->nil) + (sNode->r != tree->nil);
  69.  
  70. /* Simple node case: delete from the tree & list */
  71. switch (childsNumber) {
  72. case 0:
  73. /* Leaf: Detatch the child */
  74. if (pNode->l == sNode)
  75. pNode->l = tree->nil;
  76. else
  77. pNode->r = tree->nil;
  78. break;
  79. case 1:
  80. /* Get the left or right child */
  81. cNode = (sNode->l != tree->nil) ? sNode->l : sNode->r;
  82.  
  83. /* Set up the links */
  84. cNode->p = sNode->p;
  85. if (sNode->p->l == sNode)
  86. sNode->p->l = cNode;
  87. else
  88. sNode->p->r = cNode;
  89. break;
  90. case 2:
  91. /* Get the pivot */
  92. pNode = avlMinimum(tree, sNode->r);
  93.  
  94. /* Move the childs */
  95. pNode->l = sNode->l;
  96. pNode->r = sNode->r;
  97. sNode->l->p = pNode;
  98. sNode->r->p = pNode;
  99.  
  100. /* Set up the parents */
  101. pNode->p = sNode->p;
  102. if (sNode->p->l == sNode)
  103. sNode->p->l = pNode;
  104. else
  105. sNode->p->r = pNode;
  106.  
  107. /* Remove self references */
  108. if (pNode->r == pNode)
  109. pNode->r = tree->nil;
  110. if (pNode->l == pNode)
  111. pNode->l = tree->nil;
  112.  
  113. break;
  114. }
  115.  
  116. /* Set up the links in the list */
  117. if (sNode->prev != tree->nil)
  118. sNode->prev->next = sNode->next;
  119. sNode->next->prev = sNode->prev;
  120.  
  121. /* Delete & rebalance */
  122. avlDeleteNode(tree, sNode);
  123. avlDeleteFixUp(tree, pNode);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement