NHumme

6C

Dec 10th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <string>
  7. #include <set>
  8. #include <stack>
  9. #include <queue>
  10. #include <deque>
  11. using namespace std;
  12.  
  13. #define TASK "bstsimple"
  14.  
  15. struct node {
  16.     int t;
  17.     node *left, *right;
  18.     node(int tt) {
  19.         t = tt;
  20.         left = NULL;
  21.         right = NULL;
  22.     }
  23. };
  24.  
  25. node *root, *last;
  26.  
  27. node *find(node *x, int t) {
  28.     if (x == NULL || x->t == t) {
  29.         return x;
  30.     }
  31.     if (t < x->t) {
  32.         return find(x->left, t);
  33.     }
  34.     return find(x->right, t);
  35. }
  36.  
  37. node *min(node *x) {
  38.     if (x->left == NULL) {
  39.         return x;
  40.     }
  41.     return min(x->left);
  42. }
  43.  
  44. node *max(node *x) {
  45.     if (x->right == NULL) {
  46.         return x;
  47.     }
  48.     return max(x->right);
  49. }
  50.  
  51. node *prev(node *x, int t) {
  52.     if (x == NULL || x->t == t) {
  53.         if (x != NULL && x->left != NULL) {
  54.             return max(x->left);
  55.         }
  56.         return last;
  57.     }
  58.     if (t < x->t) {
  59.         return prev(x->left, t);
  60.     }
  61.     last = x;
  62.     return prev(x->right, t);
  63. }
  64.  
  65. node *next(node *x, int t) {
  66.     if (x == NULL || x->t == t) {
  67.         if (x != NULL && x->right != NULL) {
  68.             return min(x->right);
  69.         }
  70.         return last;
  71.     }
  72.     if (t < x->t) {
  73.     last = x;
  74.         return next(x->left, t);
  75.     }
  76.     return next(x->right, t);
  77. }
  78.  
  79. node *ins(node *x, int t) {
  80.     if (x == NULL) {
  81.         return new node(t);
  82.     }
  83.     if (t < x->t) {
  84.         x->left = ins(x->left, t);
  85.     }
  86.     else if (t > x->t) {
  87.         x->right = ins(x->right, t);
  88.     }
  89.     return x;
  90. }
  91.  
  92. node *del(node *x, int t) {
  93.     if (x == NULL) {
  94.         return x;
  95.     }
  96.     if (t < x->t) {
  97.         x->left = del(x->left, t);
  98.     }
  99.     else if (t > x->t) {
  100.         x->right = del(x->right, t);
  101.     }
  102.     else if (x->left != NULL && x->right != NULL) {
  103.         x->t = min(x->right)->t;
  104.         x->right = del(x->right, x->t);
  105.     }
  106.     else if (x->left != NULL) {
  107.         x = x->left;
  108.     }
  109.     else {
  110.         x = x->right;
  111.     }
  112.     return x;
  113. }
  114.  
  115. int main() {
  116.  
  117. #ifdef _DEBUG
  118.     freopen("debug.in", "r", stdin);
  119.     freopen("debug.out", "w", stdout);
  120. #else
  121.     freopen(TASK".in", "r", stdin);
  122.     freopen(TASK".out", "w", stdout);
  123. #endif // _DEBUG
  124.  
  125.     ios_base::sync_with_stdio(0);
  126.     cin.tie(0);
  127.     cout.tie(0);
  128.  
  129.     string s;
  130.     int x;
  131.     node *t;
  132.     while (cin >> s) {
  133.         cin >> x;
  134.         switch (s[0]) {
  135.         case 'i': root = ins(root, x); break;
  136.         case 'd': root = del(root, x); break;
  137.         case 'e': cout << (find(root, x) == NULL ? "false\n" : "true\n"); break;
  138.         case 'n': last = NULL; t = next(root, x); if (t == NULL) cout << "none"; else cout << t->t; cout << "\n"; break;
  139.         case 'p': last = NULL; t = prev(root, x); if (t == NULL) cout << "none"; else cout << t->t; cout << "\n"; break;
  140.         }
  141.     }
  142.  
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment