Advertisement
he_obviously

Untitled

Feb 2nd, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. typedef long long ll;
  5.  
  6. using namespace std;
  7.  
  8. struct node {
  9.     ll x; int h = 1;
  10.     node* l = 0, * r = 0;
  11.     node() {}
  12.     node(ll _x) {
  13.         this->x = _x;
  14.     }
  15. };
  16.  
  17. int get_h(node* v) {
  18.     return (v ? v->h : 0);
  19. }
  20.  
  21. int dif(node* v) {
  22.     return (v ? get_h(v->r) - get_h(v->l) : 0);
  23. }
  24.  
  25. void update(node* v) {
  26.     int h1 = get_h(v->l),
  27.         h2 = get_h(v->r);
  28.     v->h = max(h1, h2) + 1;
  29. }
  30.  
  31. node* right_rotate(node* v) {
  32.     if (!v) return v;
  33.     node* c = v->l;
  34.     v->l = c->r;
  35.     c->r = v;
  36.     update(v);
  37.     update(c);
  38.     return c;
  39. }
  40.  
  41. node* left_rotate(node* v) {
  42.     if (!v) return v;
  43.     node* c = v->r;
  44.     v->r = c->l;
  45.     c->l = v;
  46.     update(v);
  47.     update(c);
  48.     return c;
  49. }
  50.  
  51. node* balance(node* v) {
  52.     if (!v) return v;
  53.     update(v);
  54.     if (dif(v) == 2) {
  55.         if (dif(v->r) < 0) {
  56.             v->r = right_rotate(v->r);
  57.         }
  58.         return left_rotate(v);
  59.     }
  60.     if (dif(v) == -2) {
  61.         if (dif(v->l) > 0) {
  62.             v->l = left_rotate(v->l);
  63.         }
  64.         return right_rotate(v);
  65.     }
  66.     return v;
  67. }
  68.  
  69. node* insert(node* v, ll k) {
  70.     if (!v) {
  71.         return new node(k);
  72.     }
  73.     else if (k < v->x) {
  74.         v->l = insert(v->l, k);
  75.     }
  76.     else {
  77.         v->r = insert(v->r, k);
  78.     }
  79.     return balance(v);
  80. }
  81.  
  82. bool find(node* v, ll k) {
  83.     if (!v) {
  84.         return false;
  85.     }
  86.     else if (v->x == k) {
  87.         return true;
  88.     }
  89.     else if (k < v->x) {
  90.         return find(v->l, k);
  91.     }
  92.     else {
  93.         return find(v->r, k);
  94.     }
  95. }
  96.  
  97. void print(node* v) {
  98.     if (!v) {
  99.         return;
  100.     }
  101.     print(v->l);
  102.     cout << v->x << "\n";
  103.     print(v->r);
  104. }
  105.  
  106. int main() {
  107.  
  108.     ios_base::sync_with_stdio(0);
  109.     cin.tie(0); cout.tie(0);
  110.  
  111.     node* root = nullptr;
  112.  
  113.     ll cur;
  114.  
  115.     while (true) {
  116.         cin >> cur;
  117.         if (!cur) {
  118.             break;
  119.         }
  120.         else if (find(root, cur)) {
  121.             continue;
  122.         }
  123.         else {
  124.             root = insert(root, cur);
  125.         }
  126.     }
  127.  
  128.     print(root);
  129.  
  130.     return 0;
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement