Advertisement
Guest User

Untitled

a guest
May 27th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.96 KB | None | 0 0
  1. // RBTree.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <string>
  6. #include <iostream>
  7. #include <fstream>
  8. #include <cstdlib>
  9. #include <cstdio>
  10. #include <conio.h>
  11.  
  12.  
  13. using namespace std;
  14.  
  15.  
  16. struct RBNode {
  17.     RBNode() : left(NULL), right(NULL), up(NULL), color('R'), key(0) {}
  18.     RBNode(int k) : left(NULL), right(NULL), up(NULL), color('R') {
  19.         key = k;
  20.     }
  21.     RBNode *left;
  22.     RBNode *right;
  23.     RBNode *up;
  24.     char color;
  25.     int key;
  26. };
  27.  
  28. void print(RBNode *root, RBNode *sentinel, ofstream& output) {
  29.     if (root->left != sentinel) {
  30.         print(root->left, sentinel, output);
  31.     }
  32.     output << root->key << " ";
  33.     if (root->right != sentinel) {
  34.         print(root->right, sentinel, output);
  35.     }
  36. }
  37. void printBT(string sp, string sn, RBNode * v, RBNode *sentinel, ofstream &output) {
  38.     string s, cr = "  ", cl = "  ", cp = "  ";
  39.     cr[0] = 'r'; cr[1] = '-';
  40.     cl[0] = 'L'; cl[1] = '-';
  41.     cp[0] = 'I';
  42.     if (v) {
  43.         s = sp;
  44.         if (sn == cr) s[s.length() - 2] = ' ';
  45.         if (v->right != sentinel) printBT(s + cp, cr, v->right, sentinel, output);
  46.  
  47.         s = s.substr(0, sp.length() - 2);
  48.         output << s << sn << v->key << ":" << v->color << endl;
  49.  
  50.         s = sp;
  51.         if (sn == cl) s[s.length() - 2] = ' ';
  52.         if (v->left != sentinel)printBT(s + cp, cl, v->left, sentinel, output);
  53.     }
  54. }
  55. void rotateLeft(RBNode **root, RBNode *x) { //rotację wykonujemy na x
  56.     RBNode *y = x->right;
  57.     x->right = y->left;
  58.     if (y->left != (*root)->up) {
  59.         y->left->up = x;
  60.     }
  61.     y->up = x->up;
  62.     if (x->up == (*root)->up) {
  63.         *root = y;
  64.     } else if (x == x->up->left) {
  65.         x->up->left = y;
  66.     } else {
  67.         x->up->right = y;
  68.     }
  69.     y->left = x;
  70.     x->up = y;
  71. }
  72. void rotateRight(RBNode **root, RBNode *x) { //rotację wykonujemy na x
  73.     RBNode *y = x->left;
  74.     x->left = y->right;
  75.     if (y->right != (*root)->up) {
  76.         y->right->up = x;
  77.     }
  78.     y->up = x->up;
  79.     if (x->up == (*root)->up) {
  80.         *root = y;
  81.     } else if (x == x->up->right) {
  82.         x->up->right = y;
  83.     } else {
  84.         x->up->left = y;
  85.     }
  86.     y->right = x;
  87.     x->up = y;
  88. }
  89. void RBInsertFix(RBNode *&root, RBNode *z) { //z to node dodany w RBInsert
  90.     while (z->up->color == 'R') {
  91.         if (z->up == z->up->up->left) {
  92.             RBNode *y = z->up->up->right;
  93.             if (y->color == 'R') {
  94.                 z->up->color = 'B';
  95.                 y->color = 'B';
  96.                 z->up->up->color = 'R';
  97.                 z = z->up->up;
  98.             } else if (z == z->up->right) {
  99.                 z = z->up;
  100.                 rotateLeft(&root, z);
  101.             } else {
  102.                 z->up->color = 'B';
  103.                 z->up->up->color = 'R';
  104.                 rotateRight(&root, z->up->up);
  105.  
  106.             }
  107.         } else if (z->up == z->up->up->right) {
  108.             RBNode *y = z->up->up->left;
  109.             if (y->color == 'R') {
  110.                 z->up->color = 'B';
  111.                 y->color = 'B';
  112.                 z->up->up->color = 'R';
  113.                 z = z->up->up;
  114.             } else if (z == z->up->left) {
  115.                 z = z->up;
  116.                 rotateRight(&root, z);
  117.             } else {
  118.                 z->up->color = 'B';
  119.                 z->up->up->color = 'R';
  120.                 rotateLeft(&root, z->up->up);
  121.             }
  122.         }
  123.     }
  124.     root->color = 'B';
  125. }
  126. void RBInsert(RBNode *&root, int value) {
  127.     RBNode *newNode = new RBNode(value);
  128.     if (root == NULL) {
  129.         newNode->color = 'B';
  130.         root = newNode;
  131.         root->up = new RBNode();
  132.         root->up->color = 'B';
  133.         root->up->key = 1337;
  134.         root->left = root->up;
  135.         root->right = root->up;
  136.     } else {
  137.         RBNode *y = root->up;
  138.         RBNode *x = root;
  139.         while (x != root->up) {
  140.             y = x;
  141.             if (newNode->key < x->key) {
  142.                 x = x->left;
  143.             } else {
  144.                 x = x->right;
  145.             }
  146.         }
  147.         newNode->up = y;
  148.         if (y == root->up) {
  149.             root = newNode;
  150.         } else if (newNode->key < y->key) {
  151.             y->left = newNode;
  152.         } else {
  153.             y->right = newNode;
  154.         }
  155.         newNode->left = root->up;
  156.         newNode->right = root->up;
  157.     }
  158.     RBInsertFix(root, newNode);
  159. }
  160. void RBTransplant(RBNode *&root, RBNode *u, RBNode *v) {
  161.     if (u->up == root->up) {
  162.         root = v;
  163.     } else if (u == u->up->left) {
  164.         u->up->left = v;
  165.     } else {
  166.         u->up->right = v;
  167.     }
  168.     v->up = u->up;
  169. }
  170. RBNode *findMin(RBNode *root, RBNode *sentinel) {
  171.     while (root->left != sentinel) {
  172.         root = root->left;
  173.     }
  174.     return root;
  175. }
  176. RBNode *findValue(RBNode *root, int value) {
  177.     RBNode *sentinel = root->up;
  178.     while (true) {
  179.         if (value == root->key) {
  180.             return root;
  181.         } else if (root->right != sentinel && value > root->key) {
  182.             root = root->right;
  183.         } else if (root->left != sentinel && value < root->key) {
  184.             root = root->left;
  185.         } else return NULL;
  186.     }
  187. }
  188. void RBDeleteFix(RBNode *&root, RBNode *x) {
  189.     RBNode *w = new RBNode();
  190.     while (x != root && x->color == 'B') {
  191.         if (x == x->up->left) {
  192.             w = x->up->right;
  193.             if (w->color == 'R') {
  194.                 w->color = 'B';
  195.                 x->up->color = 'R';
  196.                 rotateLeft(&root, x->up);
  197.                 w = x->up->right;
  198.             }
  199.             if (w->left->color == 'B' && w->right->color == 'B') {
  200.                 w->color = 'R';
  201.                 x = x->up;
  202.             } else if (w->right->color == 'B') {
  203.                 w->left->color = 'B';
  204.                 w->color = 'R';
  205.                 rotateRight(&root, w);
  206.                 w = x->up->right;
  207.             } else {
  208.                 w->color = x->up->color;
  209.                 x->up->color = 'B';
  210.                 w->right->color = 'R';
  211.                 rotateLeft(&root, x->up);
  212.                 x = root;
  213.             }
  214.         } else {
  215.             w = x->up->left;
  216.             if (w->color == 'R') {
  217.                 w->color = 'B';
  218.                 x->up->color = 'R';
  219.                 rotateRight(&root, x->up);
  220.                 w = x->up->left;
  221.             }
  222.             if (w->right->color == 'B' && w->left->color == 'B') {
  223.                 w->color = 'R';
  224.                 x = x->up;
  225.             } else if (w->left->color == 'B') {
  226.                 w->right->color = 'B';
  227.                 w->color = 'R';
  228.                 rotateLeft(&root, w);
  229.                 w = x->up->left;
  230.             } else {
  231.                 w->color = x->up->color;
  232.                 x->up->color = 'B';
  233.                 w->left->color = 'R';
  234.                 rotateRight(&root, x->up);
  235.                 x = root;
  236.             }
  237.         }
  238.     }
  239.     x->color = 'B';
  240. }
  241. void RBDelete(RBNode *&root, int value) {
  242.     RBNode *sentinel = root->up;
  243.     RBNode *z = findValue(root, value);
  244.     RBNode *y = z;
  245.     RBNode *x = new RBNode();
  246.     char yOrginalColor = y->color;
  247.     if (z->left == sentinel) {
  248.         x = z->right;
  249.         RBTransplant(root, z, z->right);
  250.     } else if (z->right == sentinel) {
  251.         x = z->left;
  252.         cout << '2' << endl;
  253.         RBTransplant(root, z, z->left);
  254.     } else {
  255.         y = findMin(z->right, sentinel);
  256.         yOrginalColor = y->color;
  257.         x = y->right;
  258.         if (y->up == z) {
  259.             x->up = y;
  260.         } else {
  261.             cout << 5 << endl;
  262.             RBTransplant(root, y, y->right);
  263.             y->right = z->right;
  264.             y->right->up = y;
  265.         }
  266.         RBTransplant(root, z, y);
  267.         y->left = z->left;
  268.         y->left->up = y;
  269.         y->color = z->color;
  270.     }
  271.     if (yOrginalColor == 'B') {
  272.         RBDeleteFix(root, x);
  273.     }
  274. }
  275. int myrandom(int i) { return std::rand() % i; }
  276.  
  277. int main(int argc, char* argv[]) {
  278.     ofstream output("drzewo.txt");
  279.     RBNode *tree = NULL;
  280.     ifstream input("dane.txt");
  281.     while (!input.eof()) {
  282.         int dana;
  283.         input >> dana;
  284.         if(dana>=0)RBInsert(tree, dana);
  285.     }
  286.     if (argc > 1) {
  287.         string c = argv[1];
  288.         if (c == "help") {
  289.             cout << "MENU:" << endl;
  290.             cout << "1.Utworz nowe drzewo." << endl;
  291.             cout << "2.Dodaj wartosci do drzewa." << endl;
  292.             cout << "3.Usun wartosci z drzewa." << endl;
  293.             _getch();
  294.         } else if (c == "1") {
  295.             ofstream clear("dane.txt");
  296.             ofstream clear1("drzewo.txt");
  297.         } else if (c == "2") {
  298.             ofstream dane("dane.txt", ios::app);
  299.             for (int i = 2; i < argc; i++) {
  300.                 RBInsert(tree, atoi(argv[i]));
  301.                 dane << atoi(argv[i]) << " ";
  302.                 ofstream output("drzewo.txt");
  303.                 printBT("", "", tree, tree->up, output);
  304.             }
  305.         } else if (c == "3") {
  306.             for (int i = 2; i < argc; i++) {
  307.                 RBDelete(tree, atoi(argv[i]));
  308.                 ofstream dane("dane.txt");
  309.                 print(tree, tree->up, dane);
  310.                 ofstream output("drzewo.txt");
  311.                 printBT("", "", tree, tree->up, output);
  312.             }
  313.         }
  314.     } else {
  315.         int a = 0;
  316.         while (a != -1) {
  317.             system("cls");
  318.             ofstream output("drzewo.txt");
  319.             if (tree != NULL) {
  320.                 printBT("", "", tree, tree->up, output);
  321.             }
  322.             cout << "Menu: " << endl;
  323.             cout << "1. Dodaj element do drzewa." << endl;
  324.             cout << "2. Usun element z drzewa." << endl;
  325.             cin >> a;
  326.             switch (a) {
  327.             case 1:
  328.                 cout << "Podaj wartosc dodawanego elementu: ";
  329.                 cin >> a;
  330.                 RBInsert(tree, a);
  331.                 break;
  332.             case 2:
  333.                 cout << "Podaj wartosc usuwanego elementu: ";
  334.                 cin >> a;
  335.                 RBDelete(tree, a);
  336.                 break;
  337.             }
  338.  
  339.         }
  340.     }
  341.  
  342.     return 0;
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement