Advertisement
Guest User

Drzewo Czerwono-Czarne 1.0

a guest
Nov 20th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.74 KB | None | 0 0
  1. //ALGO2 IS1 211A LAB03
  2. //Jakub Ogryzek
  3. //oj44443@zut.edu.pl
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <ctime>
  7. #include <random>
  8.  
  9. using namespace std;
  10.  
  11. template <typename T>
  12. class Node {
  13. public:
  14.     T data;
  15.     Node *left;
  16.     Node *right;
  17.     Node *parent;
  18.     //0 to czerwony, 1 to czarny;
  19.     bool color;
  20.     Node()
  21.     {
  22.         //left = nullptr;
  23.         //right = nullptr;
  24.         //parent = nullptr;
  25.         //color = 1;
  26.     }
  27. };
  28.  
  29. template <typename T>
  30. class Tree {
  31. public:
  32.     Node<T> *root;
  33.     int counter;
  34.     Tree()
  35.     {
  36.         counter = 0;
  37.         root = nullptr;
  38.     }
  39.     bool Insert(const T&data)
  40.     {
  41.         if (root == nullptr)
  42.         {
  43.             root = new Node<T>;
  44.             root->data = data;
  45.             counter += 1;
  46.         }
  47.         else
  48.         {
  49.             Node<T> *current;
  50.             current = root;
  51.             while (current != nullptr)
  52.             {
  53.                 if (data < current->data)
  54.                 {
  55.                     //w lewo
  56.                     if (current->left == nullptr)
  57.                     {
  58.                         current->left = new Node<T>;
  59.                         current->left->data = data;
  60.                         current->left->parent = current;
  61.                         counter += 1;
  62.                         current = current->left;
  63.                         return 1;
  64.                     }
  65.                     else
  66.                     {
  67.                         current = current->left;
  68.                     }
  69.                 }
  70.                 if (data > current->data)
  71.                 {
  72.                     //w prawo
  73.                     if (current->right == nullptr)
  74.                     {
  75.                         current->right = new Node<T>;
  76.                         current->right->data = data;
  77.                         current->right->parent = current;
  78.                         counter += 1;
  79.                         current = current->right;
  80.                         return 1;
  81.                     }
  82.                     else
  83.                     {
  84.                         current = current->right;
  85.                     }
  86.                 }
  87.                 if (data == current->data)
  88.                 {
  89.                     //element sie powtarza
  90.                     return 0;
  91.                 }
  92.             }
  93.         }
  94.     }
  95.     bool AddNewElement(const T&data)
  96.     {
  97.         //0 to czerwony, 1 to czarny;
  98.         int flag = Insert(data);
  99.         if (flag == 0)
  100.             return 0;
  101.         Node<T>*x = ReturnFunction(data);
  102.         x->color = 0;
  103.         while (x != root && x->parent->color == 0)
  104.         {
  105.             if (x->parent == x->parent->parent->left)
  106.             {
  107.                 Node<T>*y = x->parent->parent->right;
  108.                 if (y != nullptr && y->color == 0)
  109.                 {
  110.                     x->parent->color = 1;
  111.                     y->color = 1;
  112.                     x->parent->parent->color = 0;
  113.                     x = x->parent->parent;
  114.                 }
  115.                 else
  116.                 {
  117.                     if (x == x->parent->right)
  118.                     {
  119.                         RotateLeft(x);
  120.                         //x = x->parent;
  121.                     }
  122.                     x->color = 1; //x->parent->color = 1;
  123.                     x->parent->color = 0; //x->parent->parent->color = 0;
  124.                     RotateRight(x); //RotateRight(x->parent);
  125.                 }
  126.             }
  127.             else
  128.             {
  129.                 Node<T>*y = x->parent->parent->left;
  130.                 if (y!=nullptr && y->color == 0)
  131.                 {
  132.                     x->parent->color = 1;
  133.                     y->color = 1;
  134.                     x->parent->parent->color = 0;
  135.                     x = x->parent->parent;
  136.                 }
  137.                 else
  138.                 {
  139.                     if (x == x->parent->left)
  140.                     {
  141.                         RotateRight(x);
  142.                         //x = x->parent;
  143.                     }
  144.                     x->color = 1; //x->parent->color = 1;
  145.                     x->parent->color = 0; // x->parent->parent->color = 0;
  146.                     RotateLeft(x); //RotateLeft(x->parent);
  147.                 }
  148.             }
  149.         }
  150.         root->color = 1;
  151.         return 1;
  152.     }
  153.     Node<T> *ReturnFunction(const T&key)
  154.     {
  155.         Node<T> *current;
  156.         current = root;
  157.         if (current->data == key)
  158.         {
  159.             return current;
  160.         }
  161.         else
  162.         {
  163.             while (current != nullptr)
  164.             {
  165.                 if (current->data > key)
  166.                 {
  167.                     //w lewo
  168.                     //current = current->left;
  169.                     if (current->left != nullptr)
  170.                     {
  171.                         if (current->left->data == key)
  172.                         {
  173.                             return current->left;
  174.                         }
  175.                     }
  176.                 }
  177.                 if (current->data > key)
  178.                     current = current->left;
  179.                 if (current->data < key)
  180.                 {
  181.                     //w prawo
  182.                     //current = current->right;
  183.                     if (current->right != nullptr)
  184.                     {
  185.                         if (current->right->data == key)
  186.                         {
  187.                             return current->right;
  188.                         }
  189.                     }
  190.                 }
  191.                 if (current->data < key)
  192.                     current = current->right;
  193.             }
  194.         }
  195.         return NULL;
  196.     }
  197.     Node<T> *SearchElement(const T&key)
  198.     {
  199.         Node<T> *current;
  200.         current = root;
  201.         if (current->data == key)
  202.         {
  203.             cout << "Znaleziono wezel o podanym kluczu (" << current->data << ")" << endl;
  204.             return current;
  205.         }
  206.         else
  207.         {
  208.             while (current != nullptr)
  209.             {
  210.                 if (current->data > key)
  211.                 {
  212.                     //w lewo
  213.                     //current = current->left;
  214.                     if (current->left != nullptr)
  215.                     {
  216.                         if (current->left->data == key)
  217.                         {
  218.                             cout << "Znaleziono wezel o podanym kluczu (" << current->left->data << ")" << endl;
  219.                             return current->left;
  220.                         }
  221.                     }
  222.                 }
  223.                 if (current->data > key)
  224.                     current = current->left;
  225.                 if (current->data < key)
  226.                 {
  227.                     //w prawo
  228.                     //current = current->right;
  229.                     if (current->right != nullptr)
  230.                     {
  231.                         if (current->right->data == key)
  232.                         {
  233.                             cout << "Znaleziono wezel o podanym kluczu (" << current->right->data << ")" << endl;
  234.                             return current->right;
  235.                         }
  236.                     }
  237.                 }
  238.                 if (current->data < key)
  239.                     current = current->right;
  240.             }
  241.         }
  242.         cout << "Nie znaleziono wezla o podanym kluczu (" << key << ")" << endl;
  243.         return NULL;
  244.     }
  245.     void PreOrderRek(Node<T> *current)
  246.     {
  247.         cout << current->data << ", ";
  248.         if (current->left != nullptr)
  249.             PreOrderRek(current->left);
  250.         if (current->right != nullptr)
  251.             PreOrderRek(current->right);
  252.     }
  253.     void PreOrder()
  254.     {
  255.         Node<T> *current;
  256.         current = root;
  257.         cout << "Przejscie Pre-Order:" << endl;
  258.         PreOrderRek(current);
  259.         cout << endl;
  260.     }
  261.     void InOrderRek(Node<T> *current)
  262.     {
  263.         if (current->left != nullptr)
  264.             InOrderRek(current->left);
  265.         cout << current->data << ", ";
  266.         if (current->right != nullptr)
  267.             InOrderRek(current->right);
  268.     }
  269.     void InOrder()
  270.     {
  271.         Node<T> *current;
  272.         current = root;
  273.         cout << "Przejscie In-Order:" << endl;
  274.         InOrderRek(current);
  275.         cout << endl;
  276.     }
  277.     void ToStringRek(Node<T> *current)
  278.     {
  279.         ToStringFunction(current);
  280.         if (current->left != nullptr)
  281.             ToStringRek(current->left);
  282.         if (current->right != nullptr)
  283.             ToStringRek(current->right);
  284.     }
  285.     void ToStringFunction(Node<T> *current)
  286.     {
  287.         cout << "(" << current->data << ") [";
  288.         if (current->color == 1)
  289.             cout << "black";
  290.         if (current->color == 0)
  291.             cout << "red";
  292.         if (current->parent == nullptr)
  293.             cout << ", parent: NULL, left: ";
  294.         else
  295.             cout << ", parent: " << current->parent->data << ", left: ";
  296.         if (current->left == nullptr)
  297.             cout << "NULL, right: ";
  298.         else
  299.             cout << current->left->data << ", right: ";
  300.         if (current->right == nullptr)
  301.             cout << "NULL]";
  302.         else
  303.             cout << current->right->data << "]";
  304.         cout << endl;
  305.     }
  306.     int HeightRek(Node <T> *current)
  307.     {
  308.         if (current == nullptr)
  309.             return 0;
  310.         int height_left = HeightRek(current->left);
  311.         int height_right = HeightRek(current->right);
  312.         if (height_left < height_right)
  313.             return height_right + 1;
  314.         else
  315.             return height_left + 1;
  316.     }
  317.     void Height()
  318.     {
  319.         Node<T> *current;
  320.         current = root;
  321.         int tree_height = HeightRek(current);
  322.         cout << "Wysokosc drzewa wynosi: " << tree_height << endl;
  323.     }
  324.     void ToString()
  325.     {
  326.         cout << "red black tree:" << endl;
  327.         cout << "size: " << counter << endl;
  328.         Node<T> *current;
  329.         current = root;
  330.         ToStringRek(current);
  331.     }
  332.     void DeleteTree(Node <T> *root)
  333.     {
  334.         if (root->left != nullptr)
  335.             DeleteTree(root->left);
  336.         if (root->right != nullptr)
  337.             DeleteTree(root->right);
  338.         delete root;
  339.     }
  340.     void RotateLeft(Node<T> *Y)
  341.     {
  342.         Node<T> *X = Y->parent;
  343.         if (X->right == Y)
  344.         {
  345.             X->right = Y->left;
  346.             if (Y->left != nullptr)
  347.                 Y->left->parent = X;
  348.             if (X->parent != nullptr) {
  349.                 Y->parent = X->parent;
  350.                 if (X == X->parent->right)
  351.                     X->parent->right = Y;
  352.                 else
  353.                     X->parent->left = Y;
  354.             }
  355.             else
  356.                 Y->parent = nullptr;
  357.             Y->left = X;
  358.             X->parent = Y;
  359.             if (X == root)
  360.                 root = Y;
  361.         }
  362.         else
  363.             cout << "Wezel Y nie jest prawym dzieckiem wezla X" << endl;
  364.     }
  365.     void RotateRight(Node<T>*X)
  366.     {
  367.         Node<T> *Y = X->parent;
  368.         if (Y->left == X)
  369.         {
  370.             Y->left = X->right;
  371.             if (X->right != nullptr)
  372.                 X->right->parent = Y;
  373.             if (Y->parent != nullptr) {
  374.                 X->parent = Y->parent;
  375.                 if (Y == Y->parent->left)
  376.                     Y->parent->left = X;
  377.                 else
  378.                     Y->parent->right = X;
  379.             }
  380.             else
  381.                 X->parent = nullptr;
  382.             X->right = Y;
  383.             Y->parent = X;
  384.             if (Y == root)
  385.                 root = X;
  386.         }
  387.         else
  388.             cout << "Wezel X nie jest lewym dzieckiem wezla Y" << endl;
  389.     }
  390. };
  391.  
  392. struct Struktura
  393. {
  394.     int pole1;
  395.     char pole2;
  396.     Struktura()
  397.     {
  398.         const int MAX_ORDER = 8;
  399.         const int n = pow(10, MAX_ORDER);
  400.         std::random_device rd;
  401.         std::default_random_engine generator(rd());
  402.         std::uniform_int_distribution<long long> zakres(0, n);
  403.         pole1 = zakres(generator);
  404.         pole2 = rand() % ('z' - 'a' + 1) + 'a';
  405.     }
  406.     Struktura(int a, char b)
  407.     {
  408.         pole1 = a;
  409.         pole2 = b;
  410.     }
  411.     bool operator ==(const Struktura&n) const
  412.     {
  413.         return pole1 == n.pole1;
  414.     }
  415.     bool operator >(const Struktura&n) const
  416.     {
  417.         return pole1 > n.pole1;
  418.     }
  419.     bool operator <(const Struktura&n) const
  420.     {
  421.         return pole1 < n.pole1;
  422.     }
  423. };
  424.  
  425. ostream& operator <<(ostream&stream, const Struktura&m)
  426. {
  427.     return (stream << m.pole1 << m.pole2);
  428. }
  429.  
  430. int main()
  431. {
  432.     srand(time(NULL));
  433.     const int MAX_ORDER = 7;
  434.     Tree<Struktura>*rbt = new Tree<Struktura>();
  435.     //for (int o = 1; o <= MAX_ORDER; o++)
  436.     //{
  437.     const int n = pow(10, 1);
  438.     //Dodawanie do drzewa
  439.     clock_t t1 = clock();
  440.     for (int i = 0; i < n; i++)
  441.     {
  442.         int flag = 0;
  443.         while(flag==0)
  444.         flag=rbt->AddNewElement(Struktura{});
  445.     }
  446.     clock_t t2 = clock();
  447.     rbt->ToString();
  448.     cout << "Czas wykonania operacji dodawania " << n << " elementow to: " << (double)(t2 - t1) / CLOCKS_PER_SEC << " sekundy" << endl;
  449.     rbt->Height();
  450.     rbt->InOrder();
  451.     //Usuwanie drzewa
  452.     rbt->DeleteTree(rbt->root);
  453.     system("pause");
  454.     //}
  455.  
  456.     /*Tree<int> *tree = new Tree<int>();
  457.     tree->AddNewElement(55);
  458.     tree->AddNewElement(69);
  459.     tree->AddNewElement(62);
  460.     tree->AddNewElement(57);
  461.     tree->AddNewElement(35);
  462.     tree->AddNewElement(83);
  463.     tree->AddNewElement(72);
  464.     tree->AddNewElement(74);
  465.     tree->ToString();
  466.     tree->Height();
  467.     tree->InOrder();
  468.     tree->SearchElement(55);
  469.     tree->SearchElement(100);
  470.     system("pause");*/
  471. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement