Advertisement
Guest User

tata

a guest
Nov 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string>
  4. const std::string Traverses[3] = {"INFIX","PREFIX","POSTFIX"};
  5.  
  6. struct node {
  7. node* left = nullptr;
  8. node* parent = nullptr;
  9. node* right = nullptr;
  10. int info;
  11. bool IsRed = 1;
  12. };
  13.  
  14. void checkEasy(node*&);
  15.  
  16. void showTree (node* &r, const int mode){
  17.     if (mode == 0)
  18.     {
  19.         if (r->left != nullptr)
  20.             showTree(r -> left,mode);      
  21.         std::cout << "[" << r -> info << "] ";
  22.         if (r->right != nullptr)
  23.             showTree(r -> right,mode);
  24.     }
  25.     if (mode == 1)
  26.     {
  27.         std::cout << "[" << r -> info << "] ";
  28.         if (r->left != nullptr)
  29.             showTree(r -> left,mode);          
  30.         if (r->right != nullptr)
  31.             showTree(r -> right,mode);
  32.     }
  33.     if (mode == 2)
  34.     {
  35.         if (r->left != nullptr)
  36.             showTree(r -> left,mode);      
  37.         if (r->right != nullptr)
  38.             showTree(r -> right,mode);
  39.         std::cout << "[" << r -> info << "] ";
  40.     }
  41. }
  42.  
  43. node* findGran(node *&n){
  44.     if ((n != nullptr) && (n -> parent != nullptr))
  45.         return n -> parent -> parent;
  46.     else
  47.         return nullptr;
  48. }
  49.  
  50. node* findUncle(node *&n){
  51.     node* g = findGran(n);
  52.     if (g == nullptr)
  53.         return nullptr;
  54.     if (n -> parent == g -> left)
  55.         return g -> right;
  56.     else
  57.         return g -> left;
  58. }
  59.  
  60. void rotRight(node* &n)
  61. {
  62.     node* p = n -> left;
  63.     p -> parent = n -> parent;
  64.     if (n -> parent != nullptr)
  65.     {
  66.         if (n -> parent -> left == n)
  67.             n -> parent -> left = p;
  68.         else
  69.             n -> parent -> right = p;
  70.     }  
  71.     n -> left = p -> right;
  72.     if (p -> right != nullptr)
  73.         p -> right -> parent = n;
  74.     n -> parent = p;
  75.     p -> right = n;
  76. }
  77. void rotLeft(node* &n)
  78. {
  79.     node* p = n -> right;
  80.     p -> parent = n -> parent;
  81.     if (n -> parent != nullptr) {
  82.         if (n -> parent -> left == n)
  83.             n -> parent -> left = p;
  84.         else
  85.             n -> parent -> right = p;
  86.     }      
  87.     n->right = p->left;
  88.     if (p -> left != nullptr)
  89.         p -> left -> parent = n;
  90.     n -> parent = p;
  91.     p -> left = n;
  92. }
  93.  
  94. void checkTree(node* &n){
  95.     node *u = findUncle(n);
  96.     node *g = findGran(n);
  97.     if ((u != nullptr) && (u -> IsRed == 1))
  98.     {
  99.         n -> parent -> IsRed = 0;
  100.         u -> IsRed = 0;
  101.         g -> IsRed = 1;
  102.         checkEasy(g);
  103.     }
  104.     else
  105.     {
  106.         if ((n == n -> parent -> right) && (n -> parent == g -> left))
  107.         {
  108.         rotLeft(n->parent);
  109.         n = n->left;
  110.         }
  111.         else
  112.             if ((n == n->parent->left) && (n->parent == g->right))
  113.             {
  114.                 rotRight(n->parent);
  115.                 n = n->right;
  116.             }
  117.         g = findGran(n);
  118.         n->parent->IsRed = 0;
  119.         g->IsRed = 1;
  120.         if ((n == n->parent->left) && (n->parent == g->left))
  121.             rotRight(g);
  122.         if ((n == n->parent->right) && (n->parent == g->right))
  123.             rotLeft(g);    
  124.     }
  125. }
  126.  
  127. void checkEasy(node* &n){
  128.     if (n -> parent == NULL)
  129.         n -> IsRed = 0;
  130.     else
  131.     {
  132.         if (n -> parent -> IsRed == 0)
  133.             return;
  134.         else
  135.             checkTree(n);
  136.     }
  137. }
  138.  
  139.  
  140.  
  141. int readInt(int &h){   
  142.     std::string str = "";
  143.     char ch = 0;
  144.     while   (1)
  145.     {
  146.         ch = getch ();
  147.         if ((ch == '-') and (str.length() == 0))
  148.         {
  149.             putch(ch);
  150.             str += ch;
  151.         }
  152.         if ((ch >= '0') and (ch <= '9'))
  153.         {
  154.             putch(ch);
  155.             str += ch;
  156.         }
  157.         if ((ch == 8) and (str.length() != 0))
  158.         {
  159.             putch(ch);
  160.             putch(0);
  161.             putch(ch);
  162.             str.pop_back();
  163.         }
  164.         if (ch == 13)
  165.         {
  166.             h=atoi(str.c_str());
  167.             return 0;
  168.         }
  169.         if (ch == 'q')
  170.             return 1;          
  171.     }
  172. }
  173.  
  174. void addValue(node* p, node* &r, int &num){
  175.     if (r == nullptr)
  176.     {
  177.         r = new node;
  178.         r -> info = num;
  179.         r -> parent = p;
  180.         checkEasy(r);
  181.         return;
  182.     }  
  183.     if (num > r-> info)
  184.         addValue(r, r -> right, num);
  185.     if (num < r-> info)
  186.         addValue(r, r -> left, num);
  187. }
  188.  
  189. bool findValue(node* &r, int &num){
  190.     if (r == nullptr)
  191.         return 0;
  192.     if (num == r -> info)
  193.         return 1;  
  194.     if (num > r -> info)
  195.         if (findValue(r -> right, num))
  196.             return 1;
  197.     if (num < r -> info)
  198.         if (findValue(r -> left, num))
  199.             return 1;
  200.     return 0;
  201. }
  202.  
  203. void changeTraverse(int &s){
  204.     char c = ' ';
  205.     while (c!=13)
  206.     {
  207.         system("cls");
  208.         std::cout << "\n\n  [Choose the traverse you need by arrows & 'Enter']\n\n\n";
  209.         for (int i=0;i<3;i++)
  210.         {
  211.             if (i==s)
  212.                 std::cout<<"-> ";
  213.             else
  214.                 std::cout<<"   ";
  215.             std::cout << Traverses[i]<<std::endl;
  216.         }
  217.         c=getch();
  218.         if (c==80)
  219.             s=(s+1)%3;
  220.         if (c==72)
  221.             s=(s+2)%3;
  222.     }
  223. }
  224.  
  225. node* createTree(){
  226.     node* x=new node;
  227.     int h;
  228.     system("cls");
  229.     std::cout << "\n\n  [Enter first value, or press 'Q' to exit]\n>";
  230.     if (readInt(h))
  231.         exit(0);
  232.     x -> info = h;
  233.     x -> IsRed = 0;
  234.     system("cls");
  235.     std::cout << "\n\n  |Current Tree|\n\n  ";
  236.     showTree (x,0);
  237.     while (1)
  238.     {
  239.         std::cout << "\n\n  [Enter another value, to complete, press 'Q']\n>";
  240.         if (readInt(h))
  241.             break;
  242.         addValue(nullptr, x, h);
  243.         showTree(x,0);
  244.         system("pause");
  245.         system("cls");
  246.         std::cout << "\n\n  |Current Tree|\n\n  ";
  247.         showTree (x,0);
  248.     }  
  249.     return x;
  250. }
  251.  
  252. int maxValue(node* r){
  253.     if (r -> right == nullptr)
  254.         return r -> info;
  255.     return maxValue(r->right);
  256. }
  257.  
  258. int minValue(node* r){
  259.     if (r -> left == nullptr)
  260.         return r -> info;
  261.     return minValue(r->left);
  262. }
  263.  
  264. void deleteTree(node* r){
  265.     if (r -> left != nullptr)
  266.         deleteTree(r -> left);     
  267.     if (r -> right != nullptr)
  268.         deleteTree(r -> right);
  269.     delete r;
  270. }
  271.  
  272. node* removeValue(node *r, int n){          
  273.     if (r == nullptr)
  274.         return r;
  275.     if (n < r -> info)
  276.         r->left = removeValue(r->left, n);     
  277.     else if (n > r -> info)
  278.         r->right = removeValue(r->right, n);   
  279.     else
  280.     {
  281.         if ((r->left != nullptr) and (r->right != nullptr))
  282.         {
  283.             r->info = minValue(r->right);
  284.             r->right = removeValue(r->right, r->info);
  285.         }
  286.         else
  287.         {
  288.             if (r->left != nullptr)
  289.             {
  290.                 node* t=r;
  291.                 r = r->left;
  292.                 delete t;              
  293.             }
  294.             else if (r->right != nullptr)
  295.             {
  296.                 node* t=r;
  297.                 r = r->right;
  298.                 delete t;              
  299.             }
  300.             else
  301.             {
  302.                 delete r;
  303.                 return nullptr;
  304.             }
  305.         }          
  306.     }
  307.     return r;
  308. }
  309.  
  310. void SomeKindaUI(){
  311.     node* Root;
  312.     int num, s = 0;
  313.     Root=createTree();
  314.     system("cls");
  315.     while (1)
  316.     {
  317.         if (Root==nullptr)
  318.             Root=createTree();
  319.         std::cout << "\n\n  |Current Tree|\n\n  ";
  320.         showTree (Root,s);
  321.         std::cout << "\n\n\nTo delete value, press '1'.\nTo add value, press '2'.\nTo recreate a tree, press '3'.\nTo change traverse, press '4'.\nTo find a value, press '5'.\nTo find min value, press '6'.\nTo find max value, press '7'.\nPress 'q' to quit. \n ";
  322.         char ch = getch();
  323.         if (ch=='q')    
  324.             break;
  325.         if (ch=='1')
  326.         {
  327.             std::cout << "\n\n  [Enter value to delete]\n>";
  328.             readInt(num);
  329.             if (not findValue(Root, num))
  330.                 std::cout<< "There is no such an element";
  331.             else
  332.                 Root=removeValue(Root,num);            
  333.         }
  334.         if (ch=='2')
  335.         {
  336.             std::cout << "\n\n  [Enter another value]\n>";
  337.             readInt(num);
  338.             addValue(nullptr, Root, num);
  339.         }
  340.         if (ch=='3')
  341.         {
  342.             deleteTree(Root);
  343.             Root = createTree();   
  344.         }
  345.         if (ch=='4')
  346.             changeTraverse(s);
  347.         if (ch=='5')
  348.         {
  349.             std::cout << "\n\n  [Enter value to find]\n>";
  350.             readInt(num);
  351.             std::cout << std::endl;
  352.             if (findValue(Root, num))
  353.                 std::cout<< "True";
  354.             else
  355.                 std::cout<< "False";
  356.             std::cout << "\n\nPress any key to continue...";
  357.             getch();
  358.         }
  359.         if (ch=='6')
  360.         {
  361.             std::cout << "\n\nMin Value: "<< minValue(Root);
  362.             std::cout << "\n\nPress any key to continue...";
  363.             getch();           
  364.         }
  365.         if (ch=='7')
  366.         {
  367.             std::cout << "\n\nMax Value: "<< maxValue(Root);
  368.             std::cout << "\n\nPress any key to continue...";
  369.             getch();           
  370.         }
  371.         system("cls");
  372.     }
  373. }
  374.  
  375. int main() {
  376.     SomeKindaUI();
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement