gavra

RedBlackTree.cpp

May 7th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.36 KB | None | 0 0
  1. #include "RedBlackTree.h"
  2. #include <limits.h>
  3.  
  4. RedBlackTree::RedBlackTree()
  5. {
  6.     this->nil = new RBNode(INT_MIN, BLACK);
  7.     this->nil->left = this->nil->right = this->nil->parent = this->nil;
  8.     this->root = this->nil;
  9.     this->root->left = this->root->right = this->root->parent = this->nil;
  10. }
  11.  
  12. void RedBlackTree::Insert(RBNode* z)
  13. {
  14.     RBNode* y = this->nil;
  15.     RBNode* x = this->root;
  16.  
  17.     while (x != this->nil)
  18.     {
  19.         y = x;
  20.         if (z->key < x->key)
  21.             x = x->left;
  22.         else
  23.             x = x->right;
  24.     }
  25.  
  26.     z->parent = y;
  27.     if (y == this->nil)
  28.         this->root = z;
  29.     else if (z->key < y->key)
  30.     {
  31.         y->left = z;
  32.     }
  33.     else
  34.     {
  35.         y->right = z;
  36.     }
  37.     z->left = this->nil;
  38.     z->right = this->nil;
  39.     z->color = RED;
  40.  
  41.     InsertFixup(z);
  42. }
  43.  
  44. void RedBlackTree::InsertFixup(RBNode* z)
  45. {
  46.     RBNode* y;
  47.    
  48.     while (z->parent->color == RED)
  49.     {
  50.         if (z->parent == z->parent->parent->left)
  51.         {
  52.             y = z->parent->parent->right;
  53.             if (y->color == RED)
  54.             {
  55.                 z->parent->color = BLACK;
  56.                 y->color = BLACK;
  57.                 z->parent->parent->color = RED;
  58.                 z = z->parent->parent;
  59.             }
  60.             else
  61.             {
  62.                 if (z == z->parent->right)
  63.                 {
  64.                     z = z->parent;
  65.                     this->LeftRotate(z);
  66.                 }
  67.                 z->parent->color = BLACK;
  68.                 z->parent->parent->color = RED;
  69.                 RightRotate(z->parent->parent);
  70.             }
  71.         }
  72.         else
  73.         {
  74.             y = z->parent->parent->left;
  75.             if (y->color == RED)
  76.             {
  77.                 z->parent->color = BLACK;
  78.                 y->color = BLACK;
  79.                 z->parent->parent->color = RED;
  80.                 z = z->parent->parent;
  81.             }
  82.             else
  83.             {
  84.                 if (z == z->parent->left)
  85.                 {
  86.                     z= z->parent;
  87.                     this->RightRotate(z);
  88.                 }
  89.                 z->parent->color = BLACK;
  90.                 z->parent->parent->color = RED;
  91.                 LeftRotate(z->parent->parent);
  92.             }
  93.         }
  94.     }
  95.     this->root->color = BLACK;
  96. }
  97.  
  98. RBNode* RedBlackTree::Delete(RBNode* z)
  99. {
  100.     RBNode *y, *x;
  101.  
  102.     if (z->left == this->nil || z->right == this->nil)
  103.         y = z;
  104.     else
  105.         y = TreeSuccessor(z);
  106.     if (y->left != this->nil)
  107.         x = y->left;
  108.     else
  109.         x = y->right;
  110.     x->parent = y->parent;
  111.     if (y->parent == this->nil)
  112.     {
  113.         this->root = x;
  114.     }
  115.     else if (y == y->parent->left)
  116.     {
  117.         y->parent->left = x;
  118.     }
  119.     else
  120.         y->parent->right = x;
  121.     if (y != z)
  122.     {
  123.         z->key = y->key;
  124.         // COPY ?????
  125.     }
  126.     if (y->color == BLACK)
  127.         this->DeleteFixup(x);
  128.     return y;
  129. }
  130.  
  131. void RedBlackTree::DeleteFixup(RBNode* x)
  132. {
  133.     RBNode* w;
  134.  
  135.     while (x != this->root && x->color == BLACK)
  136.     {
  137.         if (x = x->parent->left)
  138.         {
  139.             w = x->parent->right;
  140.             if (w->color == RED)
  141.             {
  142.                 w->color = BLACK;
  143.                 x->parent->color = RED;
  144.                 this->LeftRotate(x->parent);
  145.                 w = x->parent->right;
  146.             }
  147.             if (w->left->color == BLACK && w->right->color == BLACK)
  148.             {
  149.                 w->color = RED;
  150.                 x = x->parent;
  151.             }
  152.             else
  153.             {
  154.                 if (w->right->color == BLACK)
  155.                 {
  156.                     w->left->color = BLACK;
  157.                     w->color = RED;
  158.                     this->RightRotate(w);
  159.                     w = x->parent->right;
  160.                 }
  161.                 w->color = x->parent->color;
  162.                 x->parent->color = BLACK;
  163.                 w->right->color = BLACK;
  164.                 this->LeftRotate(x->parent);
  165.                 x = this->root;
  166.             }
  167.         }
  168.         else
  169.         {
  170.             w = x->parent->left;
  171.             if (w->color == RED)
  172.             {
  173.                 w->color = BLACK;
  174.                 x->parent->color = RED;
  175.                 this->RightRotate(x->parent);
  176.                 w = x->parent->left;
  177.             }
  178.             if (w->right->color == BLACK && w->left->color == BLACK)
  179.             {
  180.                 w->color = RED;
  181.                 x = x->parent;
  182.             }
  183.             else
  184.             {
  185.                 if (w->left->color == BLACK)
  186.                 {
  187.                     w->right->color = BLACK;
  188.                     w->color = RED;
  189.                     this->LeftRotate(w);
  190.                     w = x->parent->left;
  191.                 }
  192.                 w->color = x->parent->color;
  193.                 x->parent->color = BLACK;
  194.                 w->left->color = BLACK;
  195.                 this->RightRotate(x->parent);
  196.                 x = this->root;
  197.             }
  198.         }
  199.     }
  200.     x->color = BLACK;
  201. }
  202.  
  203. void RedBlackTree::LeftRotate(RBNode* x)
  204. {
  205.     RBNode* y = x->right; // set y
  206.     x->right = y->left; // turn y's left subtree into x's right subtree
  207.     if (y->left != this->nil)
  208.         y->left->parent = x;
  209.     y->parent = x->parent; // link x parent to y
  210.    
  211.     if (x->parent == this->nil)
  212.     {
  213.         this->root = y;
  214.     }
  215.     else if (x == x->parent->left)
  216.     {
  217.         x->parent->left = y;
  218.     }
  219.     else
  220.     {
  221.         x->parent->right = y;
  222.     }
  223.     y->left = x; // put x on y's left
  224.     x->parent = y;
  225. }
  226.  
  227. void RedBlackTree::RightRotate(RBNode* x)
  228. {
  229.     RBNode* y = x->left; // set y
  230.     x->left = y->right; // turn y's right subtree into x's left subtree
  231.     if (y->right != this->nil)
  232.         y->right->parent = x;
  233.     y->parent = x->parent; // link x parent to y
  234.    
  235.     if (x->parent == this->nil)
  236.     {
  237.         this->root = y;
  238.     }
  239.     else if (x == x->parent->right)
  240.     {
  241.         x->parent->right = y;
  242.     }
  243.     else
  244.     {
  245.         x->parent->left = y;
  246.     }
  247.     y->right = x; // put x on y's right
  248.     x->parent = y;
  249. }
  250.  
  251. RBNode* RedBlackTree::TreeSuccessor(RBNode* x)
  252. {
  253.     if (x->right != this->nil)
  254.         return this->TreeMinimum(x->right);
  255.     RBNode* y = x->parent;
  256.     while (y != this->nil && x == y->right)
  257.     {
  258.         x = y;
  259.         y = y->parent;
  260.     }
  261.     return y;
  262. }
  263.  
  264. RBNode* RedBlackTree::TreeMinimum(RBNode* x)
  265. {
  266.     while (x->left != this->nil)
  267.         x = x->left;
  268.     return x;
  269. }
  270.  
  271. RBNode* RedBlackTree::TreeSearch(RBNode* x, int key)
  272. {
  273.     while (x != this->nil && key != x->key)
  274.     {
  275.         if (key < x->key)
  276.             x = x->left;
  277.         else x = x->right;
  278.     }
  279.     return x;
  280. }
  281.  
  282. void RedBlackTree::InorderTreeWalk(RBNode* x)
  283. {
  284.     if (x != this->nil)
  285.     {
  286.         InorderTreeWalk(x->left);
  287.         cout << x->key << " " << ((x->color == RED) ? "RED"  : "BLACK") << ", ";
  288.         InorderTreeWalk(x->right);
  289.     }
  290. }
  291.  
  292. void RedBlackTree::Print()
  293. {
  294.     InorderTreeWalk(this->root);
  295. }
Advertisement
Add Comment
Please, Sign In to add comment