Advertisement
CyberN00b

d_tree

Jan 8th, 2023 (edited)
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.62 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.  
  12.     Node(long long int val, long long int parent=-1) {
  13.         this->val = val;
  14.         this->parent = parent;
  15.         bro = -1;
  16.         left_son = -1;
  17.     }
  18.  
  19. };
  20.  
  21. struct Tree{
  22.  
  23.     void insert(long long int val) {
  24.         if (nodes.empty())
  25.             insert(-1, -1, val);
  26.         else
  27.             insert(0, -1, val);
  28.     }
  29.  
  30.     void eq_tree(Tree& b) {
  31.         for (long long int i = 0; i < nodes.size(); ++i) {
  32.             if (!b.contains(nodes[i]->val))
  33.                 delete_n(i);
  34.         }
  35.     }
  36.  
  37.     bool contains(long long int val) {
  38.         if (nodes.empty())
  39.             return false;
  40.         return contains(0, val);
  41.     }
  42.  
  43.     void symmetry_print() {
  44.         if (nodes.empty())
  45.             cout << "Tree is empty!";
  46.         else {
  47.             cout << "Tree: ";
  48.             symmetry_print(0);
  49.         }
  50.         cout << endl;
  51.     }
  52.  
  53.     void straight_print() {
  54.         if (nodes.empty())
  55.             cout << "Tree is empty!";
  56.         else {
  57.             cout << "Tree: ";
  58.             straight_print(0);
  59.         }
  60.         cout << endl;
  61.     }
  62.  
  63. private:
  64.  
  65.     void delete_n(long long int index) {
  66.         if (index == -1)
  67.             return;
  68.         long long int l = get_left_son(index), r = get_right_son(index);
  69.         if (l == -1 && r == -1) {
  70.             if (nodes[index]->parent != -1) {
  71.                 if (is_right(nodes[index]->parent, nodes[index]->val)) {
  72.                     if (nodes[nodes[index]->parent]->left_son != -1)
  73.                         nodes[nodes[nodes[index]->parent]->left_son]->bro = -1;
  74.                 } else
  75.                     nodes[nodes[index]->parent]->left_son = -1;
  76.             }
  77.             deleting_node(index);
  78.         } else if (r == -1) {
  79.             nodes[l]->parent = nodes[index]->parent;
  80.             nodes[l]->bro = nodes[index]->bro;
  81.             if (nodes[index]->parent != -1) {
  82.                 if (is_right(nodes[index]->parent, nodes[index]->val)) {
  83.                     if (nodes[nodes[index]->parent]->left_son != -1)
  84.                         nodes[nodes[nodes[index]->parent]->left_son]->bro = l;
  85.                 } else {
  86.                     nodes[nodes[index]->parent]->left_son = l;
  87.                 }
  88.             }
  89.             deleting_node(index);
  90.         } else if (l == -1) {
  91.             nodes[r]->parent = nodes[index]->parent;
  92.             nodes[r]->bro = nodes[index]->bro;
  93.             if (nodes[index]->parent != -1) {
  94.                 if (is_right(nodes[index]->parent, nodes[index]->val)) {
  95.                     if (nodes[nodes[index]->parent]->left_son != -1)
  96.                         nodes[nodes[nodes[index]->parent]->left_son]->bro = r;
  97.                 } else {
  98.                     nodes[nodes[index]->parent]->left_son = r;
  99.                 }
  100.             }
  101.             deleting_node(index);
  102.         } else {
  103.             nodes[index]->val = del_left(get_right_son(index));
  104.         }
  105.     }
  106.  
  107.     long long int del_left(long long index) {
  108.         while (nodes[index]->left_son != -1)
  109.             index = nodes[index]->left_son;
  110.         long long int tmp = nodes[index]->val, r = get_right_son(index);
  111.         if (r != -1) {
  112.             nodes[r]->parent = nodes[index]->parent;
  113.             if (!is_right(nodes[index]->parent, nodes[index]->val)) {
  114.                 nodes[nodes[index]->parent]->left_son = r;
  115.             } else {
  116.                 long long int l = get_left_son(nodes[index]->parent);
  117.                 if (l != -1)
  118.                     nodes[l]->bro = r;
  119.             }
  120.         } else {
  121.             if (!is_right(nodes[index]->parent, nodes[index]->val)) {
  122.                 nodes[nodes[index]->parent]->left_son = -1;
  123.             } else {
  124.                 long long int l = get_left_son(nodes[index]->parent);
  125.                 if (l != -1)
  126.                     nodes[l]->bro = -1;
  127.             }
  128.         }
  129.         deleting_node(index);
  130.         return tmp;
  131.     }
  132.  
  133.     void deleting_node(long long int index) {
  134.         nodes.erase(nodes.begin() + index);
  135.         for (auto& x : nodes) {
  136.             if (x->parent > index)
  137.                 --x->parent;
  138.             if (x->left_son > index)
  139.                 --x->left_son;
  140.             if (x->bro > index)
  141.                 --x->bro;
  142.         }
  143.     }
  144.  
  145.     void symmetry_print(long long int index) {
  146.         if (index == -1)
  147.             return;
  148.         symmetry_print(get_left_son(index));
  149.         cout << nodes[index]->val << ' ';
  150.         symmetry_print(get_right_son(index));
  151.     }
  152.  
  153.     void straight_print(long long int index) {
  154.         if (index == -1)
  155.             return;
  156.         cout << nodes[index]->val << ' ';
  157.         straight_print(get_left_son(index));
  158.         straight_print(get_right_son(index));
  159.     }
  160.  
  161.     void insert(long long int index, long long int parent, long long int val) {
  162.         if (index == -1) {
  163.             nodes.push_back(new Node(val, parent));
  164.             check_node(nodes.size() - 1);
  165.             return;
  166.         }
  167.         if (val > nodes[index]->val) {
  168.             insert(get_right_son(index), index, val);
  169.         } else {
  170.             insert(get_left_son(index), index, val);
  171.         }
  172.     }
  173.  
  174.     bool contains(long long int index, long long int val) {
  175.         if (index == -1)
  176.             return false;
  177.         if (val == nodes[index]->val)
  178.             return true;
  179.         if (nodes[index]->val < val) {
  180.             return contains(get_right_son(index), val);
  181.         }
  182.         return contains(get_left_son(index), val);
  183.     }
  184.  
  185.     long long int find(long long int index, long long int val) {
  186.         if (index == -1)
  187.             return -1;
  188.         if (val == nodes[index]->val)
  189.             return index;
  190.         if (nodes[index]->val < val) {
  191.             return find(get_right_son(index), val);
  192.         }
  193.         return find(get_left_son(index), val);
  194.     }
  195.  
  196.     void check_node(long long int index) {
  197.         long long int parent = nodes[index]->parent;
  198.         if (parent != -1)
  199.             if (is_right(parent, nodes[index]->val)) {
  200.                 if (nodes[parent]->left_son != -1)
  201.                     nodes[nodes[parent]->left_son]->bro = index;
  202.             } else {
  203.                 nodes[parent]->left_son = index;
  204.                 for (long long int i = parent + 1; i < nodes.size(); ++i) {
  205.                     if (i == index)
  206.                         continue;
  207.                     if (nodes[i]->parent == parent) {
  208.                         nodes[index]->bro = i;
  209.                         break;
  210.                     }
  211.                 }
  212.             }
  213.     }
  214.  
  215.     long long int get_left_son( long long int index) {
  216.         return nodes[index]->left_son;
  217.     }
  218.  
  219.     bool is_right(long long int parent_index, long long int val) {
  220.         return nodes[parent_index]->val < val;
  221.     }
  222.  
  223.     long long int get_right_son( long long int index) {
  224.         if (nodes[index]->left_son != -1)
  225.             return nodes[nodes[index]->left_son]->bro;
  226.         for (long long int i = index + 1; i < nodes.size(); ++i) {
  227.             if (nodes[i]->parent == index)
  228.                 return i;
  229.         }
  230.         return -1;
  231.     }
  232.  
  233.     vector<Node*> nodes;
  234. };
  235.  
  236. int main() {
  237.     int a;
  238.     Tree A, B;
  239.     cin >> a;
  240.     for (int i = 0; i < a; ++i) {
  241.         int t;
  242.         cin >> t;
  243.         A.insert(t);
  244.     }
  245.     A.straight_print();
  246.     cin >> a;
  247.     for (int i = 0; i < a; ++i) {
  248.         int t;
  249.         cin >> t;
  250.         B.insert(t);
  251.     }
  252.     B.symmetry_print();
  253.     A.eq_tree(B);
  254.     cout << "Operation result:\n";
  255.     A.straight_print();
  256.     cout << "end";
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement