Guest User

Untitled

a guest
Jan 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.76 KB | None | 0 0
  1. // lab3.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. struct Node
  11. {
  12.     int value;
  13.     Node *left, *right;
  14. } root;
  15.  
  16. Node *Create(int value);
  17. void Add(int value, Node *root);
  18. void Scan_preorder(Node *root);
  19. void Scan_inorder(Node *root);
  20. void Scan_postorder(Node *root);
  21. void Print(Node *root);
  22. Node *Find(int value, Node *root);
  23. Node *Min(Node *root);
  24. Node *Parent(Node *root, Node *son);
  25. void Remove(int value, Node *&root);
  26. void Free(Node *&root);
  27. Node *DSW_1(Node *&root);
  28. Node *DSW_2(Node *&root);
  29. void rot_left(Node *&root, Node *to_rot);
  30. void rot_right(Node *&root, Node *to_rot);
  31. int Log2(int x);
  32. unsigned int Depth(Node *root);
  33. void Queue(int * queue, Node * root, int index);
  34. void Draw(Node * root, int block_width);
  35.  
  36. int main()
  37. {
  38.     int x = 1, value_to_delete, value_to_add, value_to_find;
  39.     Node *root = 0, *value_found;
  40.  
  41.     while (x)
  42.     {
  43.         cout << "--------========= MENU =========----------" << endl;
  44.         cout << "Wcisnij:" << endl;
  45.         cout << "\t1 - aby stworzyc drzewo" << endl;
  46.         cout << "\t2 - aby dodac wezel do drzewa" << endl;
  47.         cout << "\t3 - aby usunac wezel z drzewa" << endl;
  48.         cout << "\t4 - aby wypisac drzewo" << endl;
  49.         cout << "\t5 - aby znalezc element drzewa" << endl;
  50.         cout << "\t6 - aby zwolnic cala pamiec" << endl;
  51.         cout << "\t7 - aby zrownowazyc drzewo algorytmen DSW - faza 1" << endl;
  52.         cout << "\t8 - aby zrownowazyc drzewo algorytmen DSW - faza 2" << endl;
  53.         cout << "\t0 - aby wyjsc" << endl << endl;
  54.         cin >> x;
  55.         cout << endl << endl;
  56.  
  57.         switch (x)
  58.         {
  59.         case 1:
  60.         {
  61.             cout << "Podaj wartosc korzenia drzewa:" << endl;
  62.             cin >> value_to_add;
  63.  
  64.             root = Create(value_to_add);
  65.             break;
  66.         }
  67.         case 2:
  68.         {
  69.             cout << "Podaj wartosc do dodania:" << endl;
  70.             cin >> value_to_add;
  71.             Add(value_to_add, root);
  72.             break;
  73.         }
  74.         case 3:
  75.         {
  76.             cout << "Podaj wartosc do usuniecia:" << endl;
  77.             cin >> value_to_delete;
  78.  
  79.             Remove(value_to_delete, root);
  80.             break;
  81.         }
  82.         case 4:
  83.         {
  84.             Print(root);
  85.             break;
  86.         }
  87.         case 5:
  88.         {
  89.             cout << "Podaj wartosc do znalezienia:" << endl;
  90.             cin >> value_to_find;
  91.  
  92.             value_found = Find(value_to_find, root);
  93.  
  94.             if (!value_found)
  95.                 cout << "Nie ma wezla o podanej wartosci" << endl;
  96.             else
  97.                 cout << "Znaleziono wezel o wartsci " << value_found->value << endl;
  98.             break;
  99.         }
  100.         case 6:
  101.         {
  102.             Free(root);
  103.             break;
  104.         }
  105.         case 7:
  106.         {
  107.             root = DSW_1(root);
  108.             break;
  109.         }
  110.         case 8:
  111.         {
  112.             root = DSW_2(root);
  113.             break;
  114.         }
  115.         case 0:
  116.         default:
  117.             break;
  118.         }
  119.     }
  120.  
  121.     return 0;
  122. }
  123.  
  124. Node *Create(int value)
  125. {
  126.     Node *new_node = new Node;
  127.  
  128.     new_node->value = value;
  129.     new_node->left = NULL;
  130.     new_node->right = NULL;
  131.  
  132.     return new_node;
  133. }
  134.  
  135. void Add(int value, Node *root)
  136. {
  137.     Node *p = root;
  138.  
  139.     if (p->value == value)
  140.     {
  141.         cout << "Wezel o podanej wartosci juz istnieje" << endl;
  142.         return;
  143.     }
  144.  
  145.     if (!p)
  146.         Create(value);
  147.     else if (value < p->value)
  148.     {
  149.         if (p->left != NULL)
  150.             Add(value, p->left);
  151.         else
  152.             p->left = Create(value);
  153.     }
  154.     else
  155.     {
  156.         if (p->right != NULL)
  157.             Add(value, p->right);
  158.         else
  159.             p->right = Create(value);
  160.     }
  161. }
  162.  
  163. void Scan_preorder(Node *root)
  164. {
  165.     if (root != NULL)
  166.     {
  167.         cout << root->value << " ";
  168.         Scan_preorder(root->left);
  169.         Scan_preorder(root->right);
  170.     }
  171. }
  172.  
  173. void Scan_inorder(Node *root)
  174. {
  175.     if (root != NULL)
  176.     {
  177.         Scan_inorder(root->left);
  178.         cout << root->value << " ";
  179.         Scan_inorder(root->right);
  180.     }
  181. }
  182.  
  183. void Scan_postorder(Node *root)
  184. {
  185.     if (root != NULL)
  186.     {
  187.         Scan_postorder(root->left);
  188.         Scan_postorder(root->right);
  189.         cout << root->value << " ";
  190.     }
  191. }
  192.  
  193. void Print(Node *root)
  194. {
  195.     int x = 1;
  196.  
  197.     if (!root)
  198.     {
  199.         cout << "Drzewo jest puste" << endl;
  200.         return;
  201.     }
  202.  
  203.     while (x)
  204.     {
  205.         cout << endl << "W jakiej kolejnosci wyswietlic drzewo?" << endl;
  206.         cout << "1 - preorder" << endl;
  207.         cout << "2 - inorder" << endl;
  208.         cout << "3 - postorder" << endl;
  209.         cout << "4 - rysuj" << endl;
  210.         cout << "0 - wyjscie" << endl;
  211.         cin >> x;
  212.  
  213.         switch (x)
  214.         {
  215.         case 1:
  216.         {
  217.             Scan_preorder(root);
  218.             cout << endl;
  219.             break;
  220.         }
  221.         case 2:
  222.         {
  223.             Scan_inorder(root);
  224.             cout << endl;
  225.             break;
  226.         }
  227.         case 3:
  228.         {
  229.             Scan_postorder(root);
  230.             cout << endl;
  231.             break;
  232.         }
  233.         case 4:
  234.         {
  235.             Draw(root, 3);
  236.             break;
  237.         }
  238.         case 0:
  239.         default:
  240.             break;
  241.         }
  242.     }
  243. }
  244.  
  245. Node *Find(int value, Node *root)
  246. {
  247.     if (!root || root->value == value)
  248.         return root;
  249.     if (value < root->value)
  250.         return Find(value, root->left);
  251.     else if (value > root->value)
  252.         return Find(value, root->right);
  253. }
  254.  
  255. Node *Min(Node *root)
  256. {
  257.     while (root->left)
  258.         root = root->left;
  259.     return root;
  260. }
  261.  
  262. Node *Parent(Node *root, Node *son)
  263. {
  264.     Node *p = NULL;
  265.  
  266.     while (root != son)
  267.     {
  268.         p = root;
  269.         if (son->value < root->value)
  270.             root = root->left;
  271.         else
  272.             root = root->right;
  273.     }
  274.     return p;
  275. }
  276.  
  277. void Remove(int value, Node *&root)
  278. {
  279.     Node *p = root, *parent, *to_delete, *pred, *temp = 0;
  280.     bool check = false;
  281.  
  282.     to_delete = Find(value, root);
  283.  
  284.     if (to_delete)
  285.     {
  286.         parent = Parent(p, to_delete);
  287.         if ((to_delete->left == NULL && to_delete->right != NULL) || (to_delete->left != NULL && to_delete->right == NULL)) //jedno dziecko - lewe lub prawe
  288.         {
  289.             if (to_delete->left == NULL && to_delete->right) //prawe dziecko
  290.             {
  291.                 if (parent)
  292.                 {
  293.                     if (parent->left == to_delete)  //lewy syn rodzica
  294.                     {
  295.                         parent->left = to_delete->right;
  296.                         delete to_delete;
  297.                     }
  298.                     else    //prawy syn rodzica
  299.                     {
  300.                         parent->right = to_delete->right;
  301.                         delete to_delete;
  302.                     }
  303.                 }
  304.                 else // korzen z prawym synem
  305.                 {
  306.                     root = to_delete->right;
  307.                     delete to_delete;
  308.                 }
  309.             }
  310.             else //lewe dziecko
  311.             {
  312.                 if (parent)
  313.                 {
  314.  
  315.                     if (parent->left == to_delete)  //lewy syn rodzica
  316.                     {
  317.                         parent->left = to_delete->left;
  318.                         delete to_delete;
  319.                     }
  320.                     else    //prawy syn rodzica
  321.                     {
  322.                         parent->right = to_delete->left;
  323.                         delete to_delete;
  324.                     }
  325.                 }
  326.                 else // korzen z lewym synem
  327.                 {
  328.                     root = to_delete->left;
  329.                     delete to_delete;
  330.                 }
  331.             }
  332.             return;
  333.         }
  334.  
  335.         if (to_delete->left == NULL && to_delete->right == NULL) //lisc
  336.         {
  337.             parent = Parent(p, to_delete);
  338.  
  339.             if (!parent) //sam korzeń
  340.             {
  341.                 delete to_delete;
  342.                 root = NULL;
  343.                 return;
  344.             }
  345.  
  346.             if (parent->left == to_delete)
  347.                 parent->left = NULL;
  348.             else
  349.                 parent->right = NULL;
  350.  
  351.             delete to_delete;
  352.             return;
  353.         }
  354.  
  355.         if (to_delete->left && to_delete->right)
  356.         {
  357.             pred = to_delete->left;
  358.  
  359.             while (pred->right != NULL)
  360.             {
  361.                 check = true;
  362.                 temp = pred;
  363.                 pred = pred->right;
  364.             }
  365.  
  366.             to_delete->value = pred->value;
  367.  
  368.             if (check)
  369.                 temp->right = NULL;
  370.             else
  371.                 to_delete->left = pred->left;
  372.             delete pred;
  373.  
  374.             return;
  375.         }
  376.     }
  377.     else
  378.     {
  379.         cout << "Brak podanego elementu" << endl;
  380.         return;
  381.     }
  382.  
  383. }
  384.  
  385. void Free(Node *&root)
  386. {
  387.     if (root)
  388.     {
  389.         Free(root->left);
  390.         Free(root->right);
  391.  
  392.         delete root;
  393.  
  394.         root = NULL;
  395.     }
  396. }
  397.  
  398. int n;  // globalny licznik wierszy
  399.  
  400. Node *DSW_1(Node *&root)
  401. {
  402.     Node *p;
  403.  
  404.     n = 0;
  405.     p = root;
  406.  
  407.     while (p)
  408.     {
  409.         if (p->left)
  410.         {
  411.             rot_right(root, p);
  412.             p = Parent(root, p);
  413.         }
  414.         else
  415.         {
  416.             n++;
  417.             p = p->right;
  418.         }
  419.     }
  420.  
  421.     return root;
  422. }
  423.  
  424. Node *DSW_2(Node *&root)
  425. {
  426.     Node *p = root, *parent;
  427.     int licznik, i;
  428.  
  429.     licznik = n + 1 - Log2(n + 1); // liczba lisci na najnizszym poziomie
  430.  
  431.     for (i = 0; i < licznik; i++)
  432.     {
  433.         rot_left(root, p);
  434.         parent = Parent(root, p);
  435.         p = parent->right;
  436.     }
  437.  
  438.     n -= licznik;
  439.  
  440.     while (n > 1)
  441.     {
  442.         n >>= 1;
  443.         p = root;
  444.  
  445.         for (i = 0; i < n; i++)
  446.         {
  447.             rot_left(root, p);
  448.             parent = Parent(root, p);
  449.             p = parent->right;
  450.         }
  451.     }
  452.     return root;
  453. }
  454.  
  455. void rot_left(Node *&root, Node *to_rot)
  456. {
  457.     Node *p, *parent;
  458.  
  459.     p = to_rot->right;
  460.  
  461.     if (!p)
  462.         return;
  463.  
  464.     parent = Parent(root, to_rot);
  465.     to_rot->right = p->left;
  466.     p->left = to_rot;
  467.  
  468.     if (!parent)
  469.     {
  470.         root = p;
  471.         return;
  472.     }
  473.  
  474.     if (parent->left == to_rot)
  475.         parent->left = p;
  476.     else
  477.         parent->right = p;
  478. }
  479.  
  480. void rot_right(Node *&root, Node *to_rot)
  481. {
  482.     Node *p, *parent;
  483.  
  484.     p = to_rot->left;
  485.  
  486.     if (!p)
  487.         return;
  488.  
  489.     parent = Parent(root, to_rot);
  490.     to_rot->left = p->right;
  491.     p->right = to_rot;
  492.  
  493.     if (!parent)
  494.     {
  495.         root = p;
  496.         return;
  497.     }
  498.    
  499.     if (parent->left == to_rot)
  500.         parent->left = p;
  501.     else
  502.         parent->right = p;
  503. }
  504.  
  505. int Log2(int x)
  506. {
  507.     int y = 1;
  508.  
  509.     while ((x >>= 1) > 0)
  510.         y <<= 1;
  511.  
  512.     return y;
  513. }
  514.  
  515. unsigned int Depth(Node *root)
  516. {
  517.     if (!root)
  518.         return 0;
  519.  
  520.     unsigned int left_depth = Depth(root->left);
  521.     unsigned int right_depth = Depth(root->right);
  522.  
  523.     return (left_depth > right_depth) ? left_depth + 1 : right_depth + 1;
  524. }
  525.  
  526. void Queue(int * queue, Node * root, int index)
  527. {
  528.     if (!root)
  529.         return;
  530.  
  531.     if (index == 0)
  532.         queue[0] = root->value;
  533.  
  534.     if (root->left)
  535.     {
  536.         queue[2 * index + 1] = root->left->value;
  537.         Queue(queue, root->left, 2 * index + 1);
  538.     }
  539.     if (root->right)
  540.     {
  541.         queue[2 * index + 2] = root->right->value;
  542.         Queue(queue, root->right, 2 * index + 2);
  543.     }
  544. }
  545.  
  546. void Draw(Node * root, int block_width)
  547. {
  548.     int depth = Depth(root);
  549.     int max_blocks = pow(2, depth - 1);
  550.     int blocks_count = 0;
  551.     for (int i = 0; i < depth; ++i)
  552.     {
  553.         blocks_count += pow(2, i);
  554.     }
  555.  
  556.     int * queue = new int[blocks_count];
  557.     for (int i = 0; i < blocks_count; queue[i++] = -1);
  558.     Queue(queue, root, 0);
  559.  
  560.     int width = max_blocks * block_width;
  561.     int begin = 0;
  562.     for (int i = 0; i < depth; ++i)
  563.     {
  564.         int current_width = width / pow(2, i);
  565.         for (int j = begin; j < begin + pow(2, i); ++j)
  566.         {
  567.             if (j == begin)
  568.                 cout << setw(current_width / 2);
  569.             else
  570.                 cout << setw(current_width);
  571.  
  572.             if (queue[j] != -1)
  573.                 cout << queue[j];
  574.             else
  575.                 cout << " ";
  576.         }
  577.         begin += pow(2, i);
  578.         cout << "\n";
  579.     }
  580. }
Add Comment
Please, Sign In to add comment