Advertisement
Ser1ousSAM

shit

Mar 28th, 2022
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.30 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<string>
  4. typedef long long ll;
  5. using namespace std;
  6.  
  7. const int negativeINF = -0xFFFFFFF;
  8. const int INF = 0xFFFFFFF;
  9.  
  10. class BST {
  11.     struct node {
  12.         ll data;
  13.         node* left;
  14.         node* right;
  15.         node(ll val) {
  16.             data = val;
  17.             left = right = NULL;
  18.         }
  19.     };
  20.  
  21.     node* insert(ll x, node* t)
  22.     {
  23.         if (t == NULL)
  24.         {
  25.             t = new node(0);
  26.             t->data = x;
  27.             t->left = t->right = NULL;
  28.         }
  29.         else if (x < t->data)
  30.             t->left = insert(x, t->left);
  31.         else if (x > t->data)
  32.             t->right = insert(x, t->right);
  33.         return t;
  34.     }
  35.  
  36.     node* findMin(node* t)
  37.     {
  38.         if (t == NULL)
  39.             return NULL;
  40.         else if (t->left == NULL)
  41.             return t;
  42.         else
  43.             return findMin(t->left);
  44.     }
  45.  
  46.     node* findMax(node* t) {
  47.         if (t == NULL)
  48.             return NULL;
  49.         else if (t->right == NULL)
  50.             return t;
  51.         else
  52.             return findMax(t->right);
  53.     }
  54.  
  55.     node* remove(ll x, node* t) {
  56.         node* temp;
  57.         if (t == NULL)
  58.             return NULL;
  59.         else if (x < t->data)
  60.             t->left = remove(x, t->left);
  61.         else if (x > t->data)
  62.             t->right = remove(x, t->right);
  63.         else if (t->left && t->right)
  64.         {
  65.             temp = findMin(t->right);
  66.             t->data = temp->data;
  67.             t->right = remove(t->data, t->right);
  68.         }
  69.         else
  70.         {
  71.             temp = t;
  72.             if (t->left == NULL)
  73.                 t = t->right;
  74.             else if (t->right == NULL)
  75.                 t = t->left;
  76.             delete temp;
  77.         }
  78.         return t;
  79.     }
  80. public:
  81.     node* root;
  82.     node* newNode(ll data);
  83.     vector <ll> arr;
  84.     void inorder(node* node) {
  85.         if (!node || node->data == 0) return;
  86.         inorder(node->left);
  87.         arr.push_back(node->data);
  88.         inorder(node->right);
  89.     }
  90.     node* construct(ll low, ll high) {
  91.         if (low > high) return NULL;
  92.         ll mid = low + (high - low) / 2;
  93.         node* root = new node(arr[mid]);
  94.         root->left = construct(low, mid - 1);
  95.         root->right = construct(mid + 1, high);
  96.         return root;
  97.     }
  98.     node* balanceBST(node* root) {
  99.         inorder(root);
  100.         return construct(0, (ll)arr.size() - 1);
  101.     }
  102.  
  103.     BST() {
  104.         root = NULL;
  105.     }
  106.  
  107.     void insert(ll x) {
  108.         root = insert(x, root);
  109.     }
  110.  
  111.     void remove(ll x) {
  112.         root = remove(x, root);
  113.     }
  114.  
  115.     node* sortedArrayToBST(vector<int>arr,
  116.         ll start, ll end)
  117.     {
  118.         if (start > end)
  119.             return NULL;
  120.  
  121.         ll mid = (start + end) / 2;
  122.         node* root = new node(arr[mid]);
  123.  
  124.         root->left = sortedArrayToBST(arr, start,
  125.             mid - 1);
  126.  
  127.         root->right = sortedArrayToBST(arr, mid + 1, end);
  128.  
  129.         return root;
  130.     }
  131.     void preOrder(node* node, vector<ll>& new_tree)
  132.     {
  133.         if (node == NULL)
  134.             return;
  135.         new_tree.push_back(node->data);
  136.         preOrder(node->left, new_tree);
  137.         preOrder(node->right, new_tree);
  138.     }
  139.  
  140.  
  141.     void next(ll& ans, node* t, ll x, bool metka, bool metka2, ll previous = INF) {
  142.         if (t == NULL && t != root) {
  143.             if (x < previous && previous != INF) {
  144.                 metka2 = 1;
  145.                 ans = previous;
  146.             }
  147.  
  148.             else
  149.                 ans = 0;
  150.         }
  151.         else if (t == NULL) {
  152.             ans = 0;
  153.         }
  154.         else {
  155.             if (t->data > x) {
  156.                 if (t->data > previous) {
  157.                     next(ans, t->left, x, metka, metka2, previous);
  158.                 }
  159.                 else
  160.                     next(ans, t->left, x, metka, metka2, t->data);
  161.  
  162.             }
  163.             else {
  164.                 if (previous > t->data && metka && t->data > x) {
  165.                     metka = false;
  166.                     next(ans, t->right, x, metka, metka2, t->data);
  167.                 }
  168.                 else
  169.                     next(ans, t->right, x, metka, metka2, previous);
  170.  
  171.             }
  172.         }
  173.     }
  174.  
  175.     void prev(ll &ans, node* t, ll x, bool metka,bool metka2, ll previous = negativeINF) {
  176.         if (t == NULL && t != root) {
  177.             if (x > previous && previous != negativeINF) {
  178.                 metka2 = 1;
  179.                 ans = previous;
  180.             }
  181.             else
  182.                 ans = 0;
  183.         }
  184.         else if (t == NULL) {
  185.             ans = 0;
  186.         }
  187.         else {
  188.             if (t->data < x) {
  189.                 if (previous < t->data) {
  190.                     prev(ans, t->right, x, metka, metka2, t->data);
  191.                 }
  192.                 else
  193.                     prev(ans, t->right, x, metka, metka2, previous);
  194.             }
  195.             else {
  196.                 if (previous > t->data) {
  197.                     prev(ans, t->left, x, metka, metka2, t->data);
  198.                 }
  199.                 else
  200.                     prev(ans, t->left, x, metka, metka2, previous);
  201.  
  202.             }
  203.         }
  204.     }
  205.  
  206.     BST Balance_Tree(vector<ll>& new_arr) {
  207.         BST t;
  208.         for (ll i : new_arr) {
  209.             t.insert(i);
  210.         }
  211.         return t;
  212.     }
  213.  
  214.     node* find(node* t, ll x) {
  215.         if (t == NULL)
  216.             return NULL;
  217.         else if (x < t->data)
  218.             return find(t->left, x);
  219.         else if (x > t->data)
  220.             return find(t->right, x);
  221.         else
  222.             return t;
  223.     }
  224.  
  225.     void search(ll x) {
  226.         root = find(root, x);
  227.     }
  228. };
  229.  
  230. int main() {
  231.     BST t;
  232.     string q = "";
  233.     ll x;
  234.     bool is_edit = 0;
  235.     while (cin >> q >> x) {
  236.         if (q == "insert") {
  237.             t.insert(x);
  238.             is_edit = 1;
  239.         }
  240.         else if (q == "exists") {
  241.             if (t.find(t.root, x) == NULL) {
  242.                 cout << "false\n";
  243.             }
  244.             else {
  245.                 cout << "true\n";
  246.             }
  247.         }
  248.         else if (q == "delete") {
  249.             t.remove(x);
  250.             is_edit = 1;
  251.         }
  252.         else if (q == "next") {
  253.             if (is_edit) {
  254.                 vector<ll>new_tree;
  255.                 t.preOrder(t.balanceBST(t.root), new_tree);
  256.                 t = t.Balance_Tree(new_tree);
  257.             }
  258.             bool is_null = 0;
  259.             is_edit = 0;
  260.             ll ans = 0;
  261.             t.next(ans, t.root, x, true, is_null);
  262.             if (is_null) {
  263.                 cout << 0 << "\n";
  264.             }
  265.             else if(ans != 0) {
  266.                 cout << ans << '\n';
  267.             }
  268.             else {
  269.                 cout << "none" << '\n';
  270.             }
  271.         }
  272.         else if (q == "prev") {
  273.             if (is_edit) {
  274.                 vector<ll>new_tree;
  275.                 t.preOrder(t.root, new_tree);
  276.                 t = t.Balance_Tree(new_tree);
  277.             }
  278.             bool is_null = 0;
  279.             is_edit = 0;
  280.             ll ans = 0;
  281.             t.prev(ans, t.root, x, true, is_null);
  282.             if (is_null) {
  283.                 cout << 0 << "\n";
  284.             }
  285.             else if (ans != 0) {
  286.                 cout << ans << '\n';
  287.             }
  288.             else {
  289.                 cout << "none" << '\n';
  290.             }
  291.         }
  292.     }
  293.  
  294.     return 0;
  295. }
  296.  
  297.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement