Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. // returns true is Node's color is true or NULL
  2.  
  3. bool Node::isBlack() {
  4. return (this == NULL || this->black == true);
  5. }
  6.  
  7. Node* Node::succ(Node* x){
  8. if (x->right != NULL) return Min(x->right);
  9. Node* pt = x->parent;
  10. while (pt != NULL && x == pt->right){
  11. x = pt;
  12. pt = pt->parent;
  13. }
  14. return pt;
  15. }
  16.  
  17. // removal method
  18. Node* Node::RBRemove(Node* root, string key) {
  19. Node* node = search(root, key);
  20. Node* y = NULL;
  21. Node* x = NULL;
  22. Node* z = NULL;
  23. if (node->left != NULL && node->right != NULL) {
  24. y = succ(node);
  25. }
  26. else{
  27. y = node;
  28. }
  29.  
  30. if (y->getLeft() != NULL) {
  31. x = y->getLeft();
  32. }
  33. else {
  34. x = y->getRight();
  35. }
  36. if(x) x->parent = y->parent;
  37. if (y->getParent() == NULL) {
  38. root = x;
  39. }
  40. else {
  41. if (y == y->getParent()->getLeft())
  42. y->getParent()->left = x;
  43. else
  44. y->getParent()->right = x;
  45. }
  46. if (y != node) {
  47. node->key = y->getKey();
  48. }
  49. if (y->isBlack()) {
  50. delProcess(root, x, y->parent);
  51. }
  52. return root;
  53. }
  54.  
  55. // rotation method (just a single rotation)
  56.  
  57. void Node::Rotate(Node* &root, bool recolor) {
  58. Node* x = this;
  59. Node* parent = x->parent;
  60. Node* gp = getGP(x);
  61. x->parent = gp; // x's parent points to grandparent
  62. if (gp != NULL) {
  63. if (r(gp) == parent) { // if p is right child of gp
  64. gp->right = x; // gp right points to x
  65. }
  66. else {
  67. gp->left = x; // gp left points to x
  68. }
  69. }
  70. else {
  71. root = x;
  72. }
  73.  
  74.  
  75. if (x == r(parent)) {
  76. Node* left = x->left;
  77. x->left = parent;
  78. parent->parent = x;
  79. parent->right = left;
  80. if (left != NULL) {
  81. left->parent = parent;
  82. }
  83. }
  84. else if (x == l(parent)){
  85. Node* right = x->right;
  86. x->right = parent;
  87. parent->parent = x;
  88. parent->left = right;
  89. if (right != NULL) {
  90. right->parent = parent;
  91. }
  92. }
  93.  
  94. if (recolor) {
  95. bool xc = x->black;
  96. bool pc = NULL;
  97. if(parent->black) pc = parent->black;
  98. x->black = pc;
  99. parent->black = xc;
  100. }
  101. }
  102.  
  103.  
  104. // method where I fix up the tree after removal
  105.  
  106. bool isBlack(Node* n) {
  107. if (n == NULL || n->isBlack() == true) return true;
  108. else return false;
  109. }
  110.  
  111. void delProcess(Node* &root, Node* x, Node* p) {
  112. if (!isBlack(x)) {
  113. x->setColor(true);
  114. return;
  115. }
  116. if (p == NULL) {
  117. return;
  118. }
  119. else {
  120. Node* w = NULL;
  121. if (x == p->getRight()) {
  122. w = p->getLeft();
  123. }
  124. else {
  125. w = p->getRight();
  126. }
  127. if (!isBlack(w)) { // handles the case where sibling node is red
  128. bool temp = isBlack(w);
  129. w->Rotate(root, true);
  130. delProcess(root, x, p, z);
  131. }
  132. else if (isBlack(w->getLeft()) && isBlack(w->getRight())) { // handles case where siblings left and right children are black (or NULL)
  133. w->setColor(false);
  134. x = p;
  135. p = p->getParent();
  136. delProcess(root, x, p, z);
  137. }
  138.  
  139. else if (!isBlack(w->getDirect(w))) { // handles case where siblings direct child is red
  140. Node* direct = w->getDirect(w);
  141. direct->setColor(true);
  142. w->Rotate(root, true);
  143. }
  144. else { // the case where there is a zig-zag child is present and double rotation is required
  145. Node* zz = x == w->getRight() ? zz = w->getLeft() : zz = w->getRight();
  146. zz->Rotate(root, true);
  147. zz->Rotate(root, true);
  148. w->setColor(true);
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement