Advertisement
Guest User

Red-black zalupos

a guest
Oct 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. //реализация функций для КЧ дерева
  2. //1 - red 2 - black
  3.  
  4. BinaryTree::RbNode::RbNode(int input, TreeNode* parent) {
  5.     node = input;
  6.     color = 1;
  7.     L = R = nullptr;
  8.     P = parent;
  9. }
  10.  
  11. BinaryTree::RbNode::RbNode() {
  12.     node = NULL;
  13.     color = 2;
  14.     L = R = P = nullptr;
  15. }
  16.  
  17. void BinaryTree::RbNode::print_color() {
  18.     if (color == 1) cout << "R";
  19.     else cout << "B";
  20. }
  21.  
  22. unsigned char BinaryTree::RbNode::get_color() {
  23.     return color;
  24. }
  25.  
  26. void BinaryTree::RbNode::change_color(unsigned char c) {
  27.     color = c;
  28. }
  29.  
  30. BinaryTree::TreeNode* BinaryTree::RbNode::find_grandpa() {
  31.     if (P != NULL) {
  32.         return P->P;
  33.     }
  34.     else {
  35.         return nullptr;
  36.     }
  37. }
  38.  
  39. BinaryTree::TreeNode* BinaryTree::RbNode::find_uncle() {
  40.     TreeNode* p = find_grandpa();
  41.     if (p == nullptr) {
  42.         return nullptr;
  43.     }
  44.     if (P == p->L) {
  45.         return p->R;
  46.     }
  47.     else {
  48.         return p->L;
  49.     }
  50. }
  51.  
  52. void BinaryTree::RbNode::rb_left_Rotate() {
  53.     TreeNode* pivot = R;
  54.     pivot->P = P;
  55.     if (P != nullptr) {
  56.         if (P->L == this) {
  57.             P->L = pivot;
  58.         }
  59.         else {
  60.             P->R = pivot;
  61.         }
  62.     }
  63.    
  64.     R = pivot->L;
  65.     if (pivot->L != nullptr) {
  66.         pivot->L->P = this;
  67.     }
  68.    
  69.     P = pivot;
  70.     pivot->L = this;
  71. }
  72.  
  73. void BinaryTree::RbNode::rb_right_Rotate() {
  74.     TreeNode* pivot = L;
  75.     pivot->P = P;
  76.     if (P != nullptr) {
  77.         if (P->L == this) {
  78.             P->L = pivot;
  79.         }
  80.         else {
  81.             P->R = pivot;
  82.         }
  83.     }
  84.    
  85.     L = pivot->R;
  86.     if (pivot->R != nullptr) {
  87.         pivot->R->P = this;
  88.     }
  89.    
  90.     P = pivot;
  91.     pivot->R = this;
  92. }
  93.  
  94. void BinaryTree::RbNode::insert_1(TreeNode* bl) {
  95.     if (P == nullptr) {
  96.         color = 2;
  97.     }
  98.     else {
  99.         insert_2(bl);
  100.     }
  101. }
  102.  
  103. void BinaryTree::RbNode::insert_2(TreeNode* bl) {
  104.     if (P->get_color() == 2) {
  105.         return;
  106.     }
  107.     else {
  108.         insert_3(bl);
  109.     }
  110. }
  111.  
  112. void BinaryTree::RbNode::insert_3(TreeNode* bl) {
  113.     TreeNode* u = find_uncle();
  114.     TreeNode* g;
  115.     if (u != nullptr && u->get_color() == 1) {
  116.         P->change_color(2);
  117.         u->change_color(2);
  118.         g = find_grandpa();
  119.         g->change_color(1);
  120.         g->insert_1(bl);
  121.     }
  122.     else {
  123.         insert_4(bl);
  124.     }
  125. }
  126.  
  127. void BinaryTree::RbNode::insert_4(TreeNode* bl) {
  128.     TreeNode* g = find_grandpa();
  129.     if (g && this == P->R && P == g->L) {
  130.         P->rb_left_Rotate();
  131.         bl = bl->L;
  132.     }
  133.     else if (g && this == P->L && P == g->R) {
  134.         P->rb_right_Rotate();
  135.         bl = bl->R;
  136.     }
  137.     insert_5(bl);
  138. }
  139.  
  140. void BinaryTree::RbNode::insert_5(TreeNode* bl) {
  141.     TreeNode* g = bl->find_grandpa();
  142.     bl->P->change_color(2);
  143.     if (g) g->change_color(1);
  144.     if (g && bl == bl->P->L && bl->P == g->L) {
  145.         g->rb_right_Rotate();
  146.     }
  147.     else if (g) {
  148.         g->rb_left_Rotate();
  149.     }
  150. }
  151.  
  152. BinaryTree::TreeNode* BinaryTree::RbNode::add(int input, TreeNode* bl) {
  153.     if (!bl) bl = new RbNode(input, nullptr);
  154.     TreeNode* ptr = bl;
  155.     TreeNode* parent = bl;
  156.     while (ptr != nullptr) {
  157.         parent = ptr;
  158.         if (input > ptr->node) {
  159.             ptr = ptr->R;
  160.         }
  161.         else if (input < ptr->node) {
  162.             ptr = ptr->L;
  163.         }
  164.     }
  165.     ptr = new RbNode(input, parent);
  166.     if (input > parent->node) {
  167.         parent->R = ptr;
  168.     }
  169.     else {
  170.         parent->L = ptr;
  171.     }
  172.     ptr->insert_1(ptr);
  173.     if (bl->P) return P;
  174.     else return bl;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement