Advertisement
Guest User

Untitled

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