Advertisement
CyberN00b

a_tree

Jan 8th, 2023
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Node{
  6.  
  7.     long long int bro;
  8.     long long int left_son;
  9.     long long int parent;
  10.     long long int val;
  11.     long long int size;
  12.  
  13.     Node(long long int val, long long int parent=-1) {
  14.         this->val = val;
  15.         this->parent = parent;
  16.         bro = -1;
  17.         left_son = -1;
  18.         size = 1;
  19.     }
  20.  
  21. };
  22. struct Tree{
  23.  
  24.     void insert(long long int val) {
  25.         if (nodes.empty())
  26.             insert(-1, -1, val);
  27.         else
  28.             insert(get_root(), -1, val);
  29.     }
  30.  
  31.     void delete_tree(Tree& b) {
  32.         for (auto& x : b.nodes)
  33.             delete_node(x->val);
  34.     }
  35.  
  36.     void delete_node(long long int val) {
  37.         if (nodes.empty())
  38.             return;
  39.         delete_n(find(get_root(), val));
  40.     }
  41.  
  42.     void symmetry_print() {
  43.         if (nodes.empty())
  44.             cout << "дерево пустое!";
  45.         else {
  46.             cout << "дерево: ";
  47.             symmetry_print(get_root());
  48.         }
  49.         cout << endl;
  50.     }
  51.  
  52.     void obr_print() {
  53.         if (nodes.empty())
  54.             cout << "дерево пустое!";
  55.         else {
  56.             cout << "дерево: ";
  57.             obr_print(get_root());
  58.         }
  59.         cout << endl;
  60.     }
  61.  
  62. private:
  63.  
  64.     void delete_n(long long int index) {
  65.         if (index == -1)
  66.             return;
  67.         long long int l = get_left_son(index), r = get_right_son(index);
  68.         if (l == -1 && r == -1) {
  69.             if (nodes[index]->parent != -1) {
  70.                 if (is_right(nodes[index]->parent, nodes[index]->val)) {
  71.                     if (nodes[nodes[index]->parent]->left_son != -1)
  72.                         nodes[nodes[nodes[index]->parent]->left_son]->bro = -1;
  73.                 } else
  74.                     nodes[nodes[index]->parent]->left_son = -1;
  75.             }
  76.             deleting_node(index);
  77.         } else if (r == -1) {
  78.             nodes[l]->parent = nodes[index]->parent;
  79.             nodes[l]->bro = nodes[index]->bro;
  80.             if (nodes[index]->parent != -1) {
  81.                 if (is_right(nodes[index]->parent, nodes[index]->val)) {
  82.                     if (nodes[nodes[index]->parent]->left_son != -1)
  83.                         nodes[nodes[nodes[index]->parent]->left_son]->bro = l;
  84.                 } else {
  85.                     nodes[nodes[index]->parent]->left_son = l;
  86.                 }
  87.             }
  88.             deleting_node(index);
  89.         } else if (l == -1) {
  90.             nodes[r]->parent = nodes[index]->parent;
  91.             nodes[r]->bro = nodes[index]->bro;
  92.             if (nodes[index]->parent != -1) {
  93.                 if (is_right(nodes[index]->parent, nodes[index]->val)) {
  94.                     if (nodes[nodes[index]->parent]->left_son != -1)
  95.                         nodes[nodes[nodes[index]->parent]->left_son]->bro = r;
  96.                 } else {
  97.                     nodes[nodes[index]->parent]->left_son = r;
  98.                 }
  99.             }
  100.             deleting_node(index);
  101.         } else {
  102.             nodes[index]->val = del_left(get_right_son(index));
  103.         }
  104.     }
  105.  
  106.     long long int del_left(long long index) {
  107.         while (nodes[index]->left_son != -1)
  108.             index = nodes[index]->left_son;
  109.         long long int tmp = nodes[index]->val, r = get_right_son(index);
  110.         if (r != -1) {
  111.             nodes[r]->parent = nodes[index]->parent;
  112.             if (!is_right(nodes[index]->parent, nodes[index]->val)) {
  113.                 nodes[nodes[index]->parent]->left_son = r;
  114.             } else {
  115.                 long long int l = get_left_son(nodes[index]->parent);
  116.                 if (l != -1)
  117.                     nodes[l]->bro = r;
  118.             }
  119.         } else {
  120.             if (!is_right(nodes[index]->parent, nodes[index]->val)) {
  121.                 nodes[nodes[index]->parent]->left_son = -1;
  122.             } else {
  123.                 long long int l = get_left_son(nodes[index]->parent);
  124.                 if (l != -1)
  125.                     nodes[l]->bro = -1;
  126.             }
  127.         }
  128.         deleting_node(index);
  129.         return tmp;
  130.     }
  131.  
  132.     void deleting_node(long long int index) {
  133.         nodes.erase(nodes.begin() + index);
  134.         for (auto& x : nodes) {
  135.             if (x->parent > index)
  136.                 --x->parent;
  137.             if (x->left_son > index)
  138.                 --x->left_son;
  139.             if (x->bro > index)
  140.                 --x->bro;
  141.         }
  142.     }
  143.  
  144.     void symmetry_print(long long int index) {
  145.         if (index == -1)
  146.             return;
  147.         symmetry_print(get_left_son(index));
  148.         cout << nodes[index]->val << ' ';
  149.         symmetry_print(get_right_son(index));
  150.     }
  151.  
  152.     void obr_print(long long int index) {
  153.         if (index == -1)
  154.             return;
  155.         obr_print(get_left_son(index));
  156.         obr_print(get_right_son(index));
  157.         cout << nodes[index]->val << ' ';
  158.     }
  159.  
  160.     void rotate_right(long long int index)
  161.     {
  162.         long long int l = get_left_son(index);
  163.         if (l == -1)
  164.             return;
  165.         set_as_left_son(index, get_right_son(l));
  166.         nodes[l]->bro = nodes[index]->bro;
  167.         nodes[index]->bro = -1;
  168.         nodes[l]->parent = nodes[index]->parent;
  169.         if (nodes[l]->parent != -1 && !is_right(nodes[l]->parent, nodes[index]->val))
  170.             nodes[nodes[l]->parent]->left_son = l;
  171.         set_as_right_son(l, index);
  172.         nodes[l]->size = nodes[index]->size;
  173.         fix_size(index);
  174.  
  175.     }
  176.  
  177.     void rotate_left(long long int index)
  178.     {
  179.         long long int r = get_right_son(index);
  180.         if (r == -1)
  181.             return;
  182.         set_as_right_son(index, get_left_son(r));
  183.         nodes[r]->bro = nodes[index]->bro;
  184.         if (nodes[r]->left_son != -1)
  185.             nodes[nodes[r]->left_son]->bro = -1;
  186.         nodes[r]->parent = nodes[index]->parent;
  187.         if (nodes[r]->parent != -1 && !is_right(nodes[r]->parent, nodes[index]->val))
  188.             nodes[nodes[r]->parent]->left_son = r;
  189.         set_as_left_son(r, index);
  190.         nodes[r]->size = nodes[index]->size;
  191.         fix_size(index);
  192.     }
  193.  
  194.     void fix_size(long long int index) {
  195.         long long int l = get_left_son(index), r = get_right_son(index);
  196.         nodes[index]->size = 1 + (l == -1 ? 0 : nodes[l]->size) + (r == -1 ? 0 : nodes[r]->size);
  197.     }
  198.  
  199.     void insert(long long int index, long long int parent, long long int val) {
  200.         if (index == -1) {
  201.             nodes.push_back(new Node(val, parent));
  202.             check_node(nodes.size() - 1);
  203.             return;
  204.         }
  205.         if (rand() % (nodes[index]->size + 1) == 0) {
  206.             insert_root(index, parent, val);
  207.         } else {
  208.             if (val > nodes[index]->val) {
  209.                 insert(get_right_son(index), index, val);
  210.             } else {
  211.                 insert(get_left_son(index), index, val);
  212.             }
  213.         }
  214.     }
  215.  
  216.     void insert_root(long long int index, long long int parent, long long int val) {
  217.         if (index == -1) {
  218.             nodes.push_back(new Node(val, parent));
  219.             check_node(nodes.size() - 1);
  220.             return;
  221.         }
  222.         if (val < nodes[index]->val)
  223.         {
  224.             insert_root(get_left_son(index), index, val);
  225.             rotate_right(index);
  226.         }
  227.         else
  228.         {
  229.             insert_root(get_right_son(index), index, val);
  230.             rotate_left(index);
  231.         }
  232.     }
  233.    
  234.     long long int find(long long int index, long long int val) {
  235.         if (index == -1)
  236.             return -1;
  237.         if (val == nodes[index]->val)
  238.             return index;
  239.         if (nodes[index]->val < val) {
  240.             return find(get_right_son(index), val);
  241.         }
  242.         return find(get_left_son(index), val);
  243.     }
  244.  
  245.     void check_node(long long int index) {
  246.         long long int parent = nodes[index]->parent;
  247.         if (parent != -1)
  248.             if (is_right(parent, nodes[index]->val)) {
  249.                 if (nodes[parent]->left_son != -1)
  250.                     nodes[nodes[parent]->left_son]->bro = index;
  251.             } else {
  252.                 nodes[parent]->left_son = index;
  253.                 for (long long int i = 0; i < nodes.size(); ++i) {
  254.                     if (i == index)
  255.                         continue;
  256.                     if (nodes[i]->parent == parent && is_right(parent, nodes[i]->val))
  257.                         nodes[index]->bro = i;
  258.                 }
  259.             }
  260.     }
  261.  
  262.     long long int get_root() {
  263.         long long int index = 0;
  264.         while (nodes[index]->parent != -1)
  265.             index = nodes[index]->parent;
  266.         return index;
  267.     }
  268.  
  269.     long long int get_left_son( long long int index) {
  270.         return nodes[index]->left_son;
  271.     }
  272.  
  273.     long long int get_right_son(long long int index) {
  274.         for (long long int i = 0; i < nodes.size(); ++i) {
  275.             if (nodes[i]->parent == index && is_right(index, nodes[i]->val))
  276.                 return i;
  277.         }
  278.         return -1;
  279.     }
  280.  
  281.     void set_as_left_son(long long int parent, long long int index) {
  282.         nodes[parent]->left_son = index;
  283.         if (index != -1) {
  284.             nodes[index]->parent = parent;
  285.             nodes[index]->bro = get_right_son(parent);
  286.         }
  287.     }
  288.  
  289.     void set_as_right_son(long long int parent, long long int index) {
  290.         if (nodes[parent]->left_son != -1)
  291.             nodes[nodes[parent]->left_son]->bro = index;
  292.         if (index != -1)
  293.             nodes[index]->parent = parent;
  294.     }
  295.  
  296.     bool is_right(long long int parent_index, long long int val) {
  297.         return nodes[parent_index]->val < val;
  298.     }
  299.     vector<Node*> nodes;
  300. };
  301.  
  302. int main() {
  303.     int a;
  304.     Tree A, B;
  305.     cout << "Введите кол-во элементов дерева А: ";
  306.     cin >> a;
  307.     for (int i = 0; i < a; ++i) {
  308.         int t;
  309.         cout << "Введите элемент " << i + 1 << ": ";
  310.         cin >> t;
  311.         A.insert(t);
  312.     }
  313.     A.obr_print();
  314.     cout << "Введите кол-во элементов дерева B: ";
  315.     cin >> a;
  316.     for (int i = 0; i < a; ++i) {
  317.         int t;
  318.         cout << "Введите элемент " << i + 1 << ": ";
  319.         cin >> t;
  320.         B.insert(t);
  321.     }
  322.     B.symmetry_print();
  323.     A.delete_tree(B);
  324.     cout << "Дерево А после операции:\n";
  325.     A.obr_print();
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement