Advertisement
KShah

Untitled

Jan 24th, 2022
1,330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.42 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  AVLTree
  4. //
  5. //  resending
  6. //  Created by kshakh on 13.12.2021.
  7. //
  8.  
  9. #include <algorithm>
  10. #include <cassert>
  11. #include <climits>
  12. #include <iostream>
  13. #include <vector>
  14. #include <cstring>
  15.  
  16. using std::cin;
  17. using std::cout;
  18. using std::vector;
  19. using std::string;
  20. using std::max;
  21. using std::min;
  22.  
  23. struct Node {
  24.     int key;
  25.     Node* left;
  26.     Node* right;
  27.     int height;
  28.    
  29.     Node() : left(nullptr), right(nullptr) {}
  30.     Node(int val) : key(val), left(nullptr), right(nullptr), height(1) {}
  31. };
  32.  
  33. class AVLTree {
  34. public:
  35.     AVLTree() : root(nullptr) {}
  36.     ~AVLTree() {
  37.         deleteRoots(root);
  38.     }
  39.    
  40.     void Add(int);
  41.     void Delete(int);
  42.     bool Exists(int);
  43.     string Next(int);
  44.     string Prev(int);
  45. private:
  46.     Node* root;
  47.    
  48.     Node* insert(Node*, int);
  49.     Node* erase(Node*, int);
  50.     bool exists(Node*, int);
  51.     string next(Node*, int);
  52.     string prev(Node*, int);
  53.    
  54.     void deleteRoots(Node* p) {
  55.         if (p == nullptr) {
  56.             return;
  57.         }
  58.         deleteRoots(p->left);
  59.         deleteRoots(p->right);
  60.         delete p;
  61.     }
  62.    
  63.     int Height(Node* p) {
  64.         return (p != nullptr) ? p->height : 0;
  65.     }
  66.    
  67.     int Diff(Node* p) {
  68.         return Height(p->right) - Height(p->left);
  69.     }
  70.    
  71.     void Update(Node* p) {
  72.         p->height = max(Height(p->left), Height(p->right)) + 1;
  73.     }
  74.    
  75.     Node* RightRotate(Node* p) {
  76.         Node* q = p->left;
  77.         p->left = q->right;
  78.         q->right = p;
  79.        
  80.         Update(p);
  81.         Update(q);
  82.        
  83.         return q;
  84.     }
  85.    
  86.     Node* LeftRotate(Node* p) {
  87.         Node* q = p->right;
  88.         p->right = q->left;
  89.         q->left = p;
  90.        
  91.         Update(p);
  92.         Update(q);
  93.        
  94.         return q;
  95.     }
  96.    
  97.     Node* Balance(Node* p) {
  98.         Update(p);
  99.         if (Diff(p) == -2) {
  100.             if (Diff(p->left) > 0) {
  101.                 p->left = LeftRotate(p->left);
  102.             }
  103.             return RightRotate(p);
  104.         } else if (Diff(p) == 2) {
  105.             if (Diff(p->right) < 0) {
  106.                 p->right = RightRotate(p->right);
  107.             }
  108.             return LeftRotate(p);
  109.         }
  110.         return p;
  111.     }
  112.    
  113.     Node* findMin(Node* p) {
  114.         if (p->left == nullptr) {
  115.             return p;
  116.         } else {
  117.             return findMin(p->left);
  118.         }
  119.     }
  120.  
  121.     Node* eraseMin(Node* p) {
  122.         if (p->left == nullptr) {
  123.             return p->right;
  124.         }
  125.         p->left = eraseMin(p->left);
  126.         return Balance(p);
  127.     }
  128. };
  129.  
  130. void AVLTree::Add(int val) {
  131.     root = insert(root, val);
  132. }
  133.  
  134. Node* AVLTree::insert(Node* p, int val) {
  135.     if (p == nullptr || Height(p) == 0) {
  136.         return new Node(val);
  137.     }
  138.     if (val < p->key) {
  139.         p->left = insert(p->left, val);
  140.     } else if (val == p->key) {
  141.         return Balance(p);
  142.     } else {
  143.         p->right = insert(p->right, val);
  144.     }
  145.    
  146.     return Balance(p);
  147. }
  148.  
  149. void AVLTree::Delete(int val) {
  150.     root = erase(root, val);
  151. }
  152.  
  153. Node* AVLTree::erase(Node *p, int val) {
  154.     if (!exists(p, val) || p == nullptr) return p;
  155.     if (val < p->key) {
  156.         p->left = erase(p->left, val);
  157.     } else if (val > p->key) {
  158.         p->right = erase(p->right, val);
  159.     } else {
  160.         if (p->right == nullptr)
  161.             return p->left;
  162.         Node* sml = findMin(p->right);
  163.         sml->right = eraseMin(p->right);
  164.         sml->left = p->left;
  165.         return Balance(sml);
  166.     }
  167.     return Balance(p);
  168. }
  169.  
  170. bool AVLTree::Exists(int val) {
  171.     return exists(root, val);
  172. }
  173.  
  174. bool AVLTree::exists(Node *p, int val) {
  175.     if (p == nullptr) {
  176.         return false;
  177.     }
  178.     if (p->key == val) {
  179.         return true;
  180.     }
  181.     if (p->key > val) {
  182.         return exists(p->left, val);
  183.     } else {
  184.         return exists(p->right, val);
  185.     }
  186. }
  187.  
  188. string AVLTree::Next(int val) {
  189.     return next(root, val);
  190. }
  191.  
  192. string AVLTree::next(Node* p, int x) {
  193.     if (p == nullptr) return "none";
  194.     Node* curr = p;
  195.     Node* nex = nullptr;
  196.     while (curr != nullptr) {
  197.         if (curr->key > x) {
  198.             nex = curr;
  199.             curr = curr->left;
  200.         } else
  201.             curr = curr->right;
  202.     }
  203.     if (nex == nullptr) return "none";
  204.     return std::to_string(nex->key);
  205. }
  206.  
  207. string AVLTree::Prev(int val) {
  208.     return prev(root, val);
  209. }
  210.  
  211. string AVLTree::prev(Node* p, int x) {
  212.     if (p == nullptr) return "none";
  213.     Node* curr = p;
  214.     Node* prev = nullptr;
  215.     while (curr != nullptr) {
  216.         if (curr->key < x) {
  217.             prev = curr;
  218.             curr = curr->right;
  219.         } else
  220.             curr = curr->left;
  221.     }
  222.     if (prev == nullptr) return "none";
  223.     return std::to_string(prev->key);
  224. }
  225. int main() {
  226.     AVLTree tree;
  227.    
  228.     string String;
  229.     int numb;
  230.     while (cin >> String >> numb) {
  231.         if (String[0] == 'i') {
  232.             tree.Add(numb);
  233.         } else if (String[0] == 'd') {
  234.             tree.Delete(numb);
  235.         } else if (String[0] == 'e') {
  236.             bool fl = tree.Exists(numb);
  237.             if (fl == true) {
  238.                 std::cout << "true\n";
  239.             } else {
  240.                 std::cout << "false\n";
  241.             }
  242.         } else if (String[0] == 'n') {
  243.             cout << tree.Next(numb) << "\n";
  244.         } else if (String[0] == 'p') {
  245.             cout << tree.Prev(numb) << "\n";
  246.         }
  247.     }
  248.    
  249.     return 0;
  250. }
  251.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement