Advertisement
cacodemon665

Лаба 16 Вариант 1

May 16th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. using namespace std;
  6.  
  7. struct ttree
  8. {
  9.     int inf;
  10.     ttree* left;
  11.     ttree* right;
  12. } *proot;
  13.  
  14.  
  15. ttree* add(ttree* proot, int inf)
  16. {
  17.     ttree* nl, * pr = NULL, * ps;
  18.     bool b;
  19.     nl = new ttree;
  20.     nl->inf = inf;
  21.     nl->left = NULL;
  22.     nl->right = NULL;
  23.     if (proot == NULL) return nl;
  24.     ps = proot;
  25.     while (ps != NULL)
  26.     {
  27.         pr = ps;
  28.         b = (inf < ps->inf);
  29.         if (b) ps = ps->left;
  30.         else ps = ps->right;
  31.     }
  32.     if (b) pr->left = nl;
  33.     else pr->right = nl;
  34.     return proot;
  35. }
  36.  
  37. void poisktree(ttree* p, int key, bool& b, int& inf)
  38. {
  39.     if ((p != NULL) && (b != true))
  40.     {
  41.         if (p->inf != key)
  42.         {
  43.             poisktree(p->left, key, b, inf);
  44.             poisktree(p->right, key, b, inf);
  45.         }
  46.         else
  47.         {
  48.             b = true;
  49.             inf = p->inf;
  50.         }
  51.     }
  52. }
  53.  
  54.  
  55. ttree* deltree(ttree* p)
  56. {
  57.     if (p == NULL) return NULL;
  58.     deltree(p->left);
  59.     deltree(p->right);
  60.     delete(p);
  61.     p = NULL;
  62.     return NULL;
  63. }
  64.  
  65.  
  66. ttree* del(ttree* proot, int inf)
  67. {
  68.     ttree* ps = proot, * pr = proot, * w = NULL, * v = NULL;
  69.     while ((ps != NULL) && (ps->inf != inf))
  70.     {
  71.         pr = ps;
  72.         if (inf < ps->inf) ps = ps->left;
  73.         else ps = ps->right;
  74.     }
  75.  
  76.     if (ps == NULL) return proot;
  77.  
  78.     if ((ps->left == NULL) && (ps->right == NULL))
  79.     {
  80.         if (ps == pr)
  81.         {
  82.             delete(ps);
  83.             return NULL;
  84.         }
  85.         if (pr->left == ps) pr->left = NULL;
  86.         else pr->right = NULL;
  87.         delete(ps);
  88.         return proot;
  89.     }
  90.  
  91.     if (ps->left == NULL)
  92.     {
  93.         if (ps == pr)
  94.         {
  95.             ps = ps->right;
  96.             delete(pr);
  97.             return ps;
  98.         }
  99.  
  100.         if (pr->left == ps) pr->left = ps->right;
  101.         else pr->right = ps->right;
  102.         delete(ps);
  103.         return proot;
  104.     }
  105.  
  106.     if (ps->right == NULL)
  107.     {
  108.         if (ps == pr)
  109.         {
  110.             ps = ps->left;
  111.             delete(pr);
  112.             return ps;
  113.         }
  114.  
  115.         if (pr->left == ps) pr->left = ps->left;
  116.         else pr->right = ps->left;
  117.         delete(ps);
  118.         return proot;
  119.     }
  120.  
  121.  
  122.     w = ps->left;
  123.     if (w->right == NULL) w->right = ps->right;
  124.     else
  125.     {
  126.         while (w->right != NULL)
  127.         {
  128.             v = w;
  129.             w = w->right;
  130.         }
  131.         v->right = w->left;
  132.         w->left = ps->left;
  133.         w->right = ps->right;
  134.     }
  135.  
  136.     if (ps == pr)
  137.     {
  138.         delete(ps);
  139.         return w;
  140.     }
  141.  
  142.     if (pr->left == ps) pr->left = w;
  143.     else pr->right = w;
  144.     delete(ps);
  145.     return proot;
  146. }
  147.  
  148. void wrtree_pr(ttree* p)
  149. {
  150.     if (p == NULL) return;
  151.     cout << p->inf << "  ";
  152.     wrtree_pr(p->left);
  153.     wrtree_pr(p->right);
  154. }
  155.  
  156. void wrtree_obr(ttree* p)
  157. {
  158.     if (p == NULL) return;
  159.     wrtree_obr(p->left);
  160.     wrtree_obr(p->right);
  161.     cout << p->inf << "  ";
  162. }
  163.  
  164. void wrtree_sim(ttree* p)
  165. {
  166.     if (p == NULL) return;
  167.     wrtree_sim(p->left);
  168.     cout << p->inf << "  ";
  169.     wrtree_sim(p->right);
  170. }
  171.  
  172. void zadanie()
  173. {
  174.     ttree* min, * max;
  175.     min = max = proot;
  176.  
  177.     while (min->left) min = min->left;
  178.     while (max->right) max = max->right;
  179.  
  180.     int t = min->inf;
  181.     min->inf = max->inf;
  182.     max->inf = t;
  183. }
  184.  
  185.  
  186.  
  187. int main()
  188. {
  189.     int v, n;
  190.     cout << "vvedite kolichesvo elementov ";
  191.     cin >> n;
  192.     for (int i = 0; i < n; i++)
  193.     {
  194.         cout << "element " << i + 1 << " :";
  195.         cin >> v;
  196.         proot = add(proot, v);
  197.     }
  198.     while (true)
  199.     {
  200.         cout << "1. Dobavit" << endl;
  201.         cout << "2. Udalit" << endl;
  202.         cout << "3. Poisk" << endl;
  203.         cout << "4. Print" << endl;
  204.         cout << "5. Zadanie" << endl;
  205.         cout << "6. Exit" << endl;
  206.         int k, x;
  207.         cin >> k;
  208.         switch (k)
  209.         {
  210.         case 1:
  211.             cout << "vvedi x ";
  212.             cin >> x;
  213.             proot = add(proot, x);
  214.             break;
  215.         case 2:
  216.             cout << "vvedi x ";
  217.             cin >> x;
  218.             proot = del(proot, x);
  219.             break;
  220.         case 3:
  221.         {
  222.             int inf;
  223.             bool b = false;
  224.             cout << "vvedi x ";
  225.             cin >> x;
  226.             poisktree(proot, x, b, inf);
  227.             if (b) cout << "x=" << x << " naiden" << endl;
  228.             else cout << "x=" << x << " ne naiden" << endl;
  229.         }
  230.         break;
  231.         case 4:
  232.             wrtree_pr(proot);
  233.             cout << endl;
  234.             wrtree_obr(proot);
  235.             cout << endl;
  236.             wrtree_sim(proot);
  237.             cout << endl;
  238.             break;
  239.         case 5:
  240.             zadanie();
  241.             break;
  242.         default:
  243.             proot = deltree(proot);
  244.             return 0;
  245.         }
  246.         _getch();
  247.         system("cls");
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement