Advertisement
radmickey

Untitled

Dec 13th, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. struct Node {
  6.     int value;
  7.     int height = 1;
  8.     Node* left = nullptr;
  9.     Node* right = nullptr;
  10.  
  11.     explicit Node(int v) {
  12.         value = v;
  13.     }
  14.  
  15.     Node() {}
  16. };
  17.  
  18. struct AVL {
  19.  
  20.     Node* head = nullptr;
  21.     int Height(Node* x){
  22.         if(x == nullptr){
  23.             return 0;
  24.         }
  25.         return x->height;
  26.     }
  27.  
  28.     void fixHeight(Node* x){
  29.         if(x == nullptr){
  30.             return;
  31.         }
  32.         x->height = std::max(Height(x->left), Height(x->right)) + 1;
  33.     }
  34.  
  35.     int CheckBalance(Node* x){
  36.         if(x == nullptr){
  37.             return 0;
  38.         }
  39.         return Height(x->right) - Height(x->left);
  40.     }
  41.  
  42.     Node* RotateRight(Node* x){
  43.         Node* y = x->left;
  44.         x->left = y->right;
  45.         y->right = x;
  46.         fixHeight(x);
  47.         fixHeight(y);
  48.         return y;
  49.     }
  50.  
  51.     Node* RotateLeft(Node* x){
  52.         Node* y = x->right;
  53.         x->right = y->left;
  54.         y->left = x;
  55.         fixHeight(x);
  56.         fixHeight(y);
  57.         return y;
  58.     }
  59.  
  60.     Node* BalanceTree(Node* x){
  61.         fixHeight(x);
  62.         if(CheckBalance(x) == 2){
  63.             if(CheckBalance(x->right) < 0){
  64.                 x->right = RotateRight(x->right);
  65.             }
  66.             return RotateLeft(x);
  67.         }
  68.         if(CheckBalance(x) == -2){
  69.             if(CheckBalance(x->left) > 0){
  70.                 x->left = RotateLeft(x->left);
  71.             }
  72.             return RotateRight(x);
  73.         }
  74.         return x;
  75.     }
  76.  
  77.     Node* Insert(Node* x, int value){
  78.         if(x == nullptr){
  79.             return new Node(value);
  80.         }
  81.         if(x->value > value){
  82.             x->left = Insert(x->left, value);
  83.         }
  84.         else {
  85.             x->right = Insert(x->right, value);
  86.         }
  87.         return BalanceTree(x);
  88.     }
  89.  
  90.     bool Exists(int value){
  91.         if(head == nullptr){
  92.             return false;
  93.         }
  94.         Node* head_copy = head;
  95.         while(head_copy != nullptr){
  96.             if(value == head_copy->value){
  97.                 return true;
  98.             }
  99.             else if(head_copy->value > value){
  100.                 head_copy = head_copy->left;
  101.             }
  102.             else{
  103.                 head_copy = head_copy->right;
  104.             }
  105.         }
  106.         return false;
  107.     }
  108.  
  109.  
  110.     Node* remove(int value, Node* head) {
  111.         if (head == nullptr) {
  112.             return nullptr;
  113.         }
  114.        
  115.        if (head->value == value) {
  116.             if (head->left == nullptr && head->right == nullptr) {
  117.                 delete head;
  118.                 return nullptr;
  119.             }
  120.  
  121.             if (head->left == nullptr && head->right != nullptr) {
  122.                 Node* temp = head->right;
  123.                 delete head;
  124.                 return temp;
  125.             }
  126.  
  127.             if (head->left != nullptr && head->right == nullptr) {
  128.                 Node* temp = head->left;
  129.                 delete head;
  130.                 return temp;
  131.             }
  132.  
  133.             // Если осталось два ребенка
  134.  
  135.             Node* max_deleted = head->left;
  136.  
  137.             head->height--;
  138.  
  139.             if (head->left->right == nullptr) {
  140.                 head->left->height--;
  141.             }
  142.  
  143.             while (max_deleted->right != nullptr) {
  144.                 max_deleted->height--;
  145.                 max_deleted = max_deleted;
  146.             }
  147.  
  148.             head->value = max_deleted->value;
  149.             head->left = remove(max_deleted->value, head->left);
  150.  
  151.         } else if (head->value > value) {
  152.            head->left = remove(value, head->left);
  153.        } else if (head->value < value) {
  154.            head->right = remove(value, head->right);
  155.        }
  156.  
  157.        return BalanceTree(head);
  158.     }
  159. };
  160.  
  161.  
  162. int main() {
  163.     srand(time(0));
  164.     std::ios::sync_with_stdio(false);
  165.     std::cin.tie(nullptr);
  166.     std::cout.tie(nullptr);
  167.  
  168.     int n;
  169.     std::cin >> n;
  170.     AVL avl_tree;
  171.     char c;
  172.     int i;
  173.  
  174.     for (int t = 0; t < n; t++) {
  175.         std::cin >> c >> i;
  176.  
  177.         if(c == 'A'){
  178.             if (avl_tree.Exists(i)) {
  179.                 std::cout << avl_tree.CheckBalance(avl_tree.head) << '\n';
  180.                 continue;
  181.             }
  182.  
  183.             avl_tree.head = avl_tree.Insert(avl_tree.head, i);
  184.             std::cout << avl_tree.CheckBalance(avl_tree.head) << '\n';
  185.         } else if(c == 'D') {
  186.             if (!avl_tree.Exists(i)) {
  187.                 std::cout << avl_tree.CheckBalance(avl_tree.head) << '\n';
  188.                 continue;
  189.             }
  190.  
  191.             avl_tree.head = avl_tree.remove(i, avl_tree.head);
  192.             std::cout << avl_tree.CheckBalance(avl_tree.head) << '\n';
  193.         } else {
  194.             if (avl_tree.Exists(i)) {
  195.                 std::cout << "Y\n";
  196.             } else {
  197.                 std::cout << "N\n";
  198.             }
  199.         }
  200.     }
  201.  
  202.     return 0;
  203. }
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement