Advertisement
Mbxvim

Untitled

Jun 2nd, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5. struct bin_tree
  6. {
  7.     int weight;
  8.     int id;
  9.     struct bin_tree* left;
  10.     struct bin_tree* right;
  11. };
  12.  
  13. bin_tree* head = NULL;
  14.  
  15. void push(bin_tree*& head, int x, int w)
  16. {
  17.     if (!head)
  18.     {
  19.         bin_tree* t = new bin_tree;
  20.         t->weight = w;
  21.         t->id = x;
  22.         t->left = NULL;
  23.         t->right = NULL;
  24.         head = t;
  25.     }
  26.     else
  27.     {
  28.         if (x < head->id)
  29.             push(head->left, x, w);
  30.         else
  31.             push(head->right, x, w);
  32.     }
  33. }
  34.  
  35. void print(bin_tree*& head, int n)
  36. {
  37.     if (head != NULL)
  38.     {
  39.         print(head->left, n + 1);
  40.         cout << setw(n * 3) << head->id << '(' << head->weight << ')' << endl;
  41.         print(head->right, n + 1);
  42.     }
  43. }
  44.  
  45. bin_tree* del(bin_tree*& head, int x)
  46. {
  47.     bin_tree* P, * v;
  48.     if (!head) cout << "Запрашиваемого элемента нет в дереве" << endl;
  49.     else if (x < head->id) head->left = del(head->left, x);
  50.     else if (x > head->id) head->right = del(head->right, x);
  51.     else {
  52.         P = head;
  53.         if (!head->right) head = head->left;
  54.         else if (!head->left) head = head->right;
  55.         else {
  56.             v = head->left;
  57.             if (v->right)
  58.             {
  59.                 while (v->right->right)
  60.                     v = v->right;
  61.                 head->id = v->right->id;
  62.                 head->weight = v->right->weight;
  63.                 P = v->right; v->right = v->right->left;
  64.             }
  65.             else
  66.             {
  67.                 head->id = v->id;
  68.                 head->weight = v->weight;
  69.                 P = v;
  70.                 head->left = head->left->left;
  71.             }
  72.         }
  73.     }
  74.     return head;
  75. }
  76.  
  77. int individual(bin_tree*& head, int s)
  78. {
  79. }
  80.  
  81. void menu() {
  82.     std::cout << "1 - Добавить" << std::endl;
  83.     std::cout << "2 - Удалить" << std::endl;
  84.     std::cout << "3 - Напечатать" << std::endl;
  85.     std::cout << "4 - Вывести вершины заданного уровня и заданного веса" << std::endl;
  86.     std::cout << "0 - Выйти" << std::endl;
  87. }
  88.  
  89. int main()
  90. {
  91.     setlocale(LC_ALL, "Russian");
  92.     push(head, 21, 1);
  93.     push(head, 42, 0);
  94.     push(head, 76, 1);
  95.     push(head, 53, -1);
  96.     push(head, 34, 1);
  97.     push(head, 55, -1);
  98.     push(head, 60, -1);
  99.     int action, w, x, s; s = 0;
  100.     while (true) {
  101.         menu();
  102.         int action;
  103.         std::cin >> action;
  104.         switch (action) {
  105.         case(1):
  106.             std::cout << "Добавить:" << std::endl;
  107.             cin >> x;
  108.             cin >> w;
  109.             push(head, x, w);
  110.             break;
  111.         case(2):
  112.             std::cout << "Удалить:" << std::endl;
  113.             cin >> x;
  114.             del(head, x);
  115.             break;
  116.         case(3):
  117.             print(head, 1);
  118.             break;
  119.         case(4):
  120.             cout << individual(head, s);
  121.             std::cout << "\n" << std::endl;
  122.             break;
  123.         case(0):
  124.             return 0;
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement