Advertisement
Ser1ousSAM

shit

Mar 28th, 2022
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.38 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 ll negativeINF = -0xFFFFFFFFF;
  8. const ll INF = 0xFFFFFFFFF;
  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 == NULL) 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<ll>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.                 if (previous == 0)
  145.                     metka2 = 1;
  146.                 ans = previous;
  147.             }
  148.  
  149.             else
  150.                 ans = 0;
  151.         }
  152.         else if (t == NULL) {
  153.             ans = 0;
  154.         }
  155.         else {
  156.             if (t->data > x) {
  157.                 if (t->data > previous) {
  158.                     next(ans, t->left, x, metka, metka2, previous);
  159.                 }
  160.                 else
  161.                     next(ans, t->left, x, metka, metka2, t->data);
  162.  
  163.             }
  164.             else {
  165.                 if (previous > t->data && metka && t->data > x) {
  166.                     metka = false;
  167.                     next(ans, t->right, x, metka, metka2, t->data);
  168.                 }
  169.                 else
  170.                     next(ans, t->right, x, metka, metka2, previous);
  171.  
  172.             }
  173.         }
  174.     }
  175.  
  176.     void prev(ll &ans, node* t, ll x, bool metka,bool& metka2, ll previous = negativeINF) {
  177.         if (t == NULL && t != root) {
  178.             if (x > previous && previous != negativeINF) {
  179.                 if (previous == 0)
  180.                     metka2 = 1;
  181.                 ans = previous;
  182.             }
  183.             else
  184.                 ans = 0;
  185.         }
  186.         else if (t == NULL) {
  187.             ans = 0;
  188.         }
  189.         else {
  190.             if (t->data < x) {
  191.                 if (previous < t->data) {
  192.                     prev(ans, t->right, x, metka, metka2, t->data);
  193.                 }
  194.                 else
  195.                     prev(ans, t->right, x, metka, metka2, previous);
  196.             }
  197.             else {
  198.                 if (previous > t->data) {
  199.                     prev(ans, t->left, x, metka, metka2, t->data);
  200.                 }
  201.                 else
  202.                     prev(ans, t->left, x, metka, metka2, previous);
  203.  
  204.             }
  205.         }
  206.     }
  207.  
  208.     BST Balance_Tree(vector<ll>& new_arr) {
  209.         BST t;
  210.         for (ll i : new_arr) {
  211.             t.insert(i);
  212.         }
  213.         return t;
  214.     }
  215.  
  216.     node* find(node* t, ll x) {
  217.         if (t == NULL)
  218.             return NULL;
  219.         else if (x < t->data)
  220.             return find(t->left, x);
  221.         else if (x > t->data)
  222.             return find(t->right, x);
  223.         else
  224.             return t;
  225.     }
  226.  
  227.     void search(ll x) {
  228.         root = find(root, x);
  229.     }
  230. };
  231.  
  232. int main() {
  233.     BST t;
  234.     string q = "";
  235.     ll x;
  236.     bool is_edit = 0;
  237.     while (cin >> q >> x) {
  238.         if (q == "insert") {
  239.             t.insert(x);
  240.             is_edit = 1;
  241.         }
  242.         else if (q == "exists") {
  243.             if (t.find(t.root, x) == NULL) {
  244.                 cout << "false\n";
  245.             }
  246.             else {
  247.                 cout << "true\n";
  248.             }
  249.         }
  250.         else if (q == "delete") {
  251.             t.remove(x);
  252.             is_edit = 1;
  253.         }
  254.         else if (q == "next") {
  255.             if (is_edit) {
  256.                 vector<ll>new_tree;
  257.                 t.preOrder(t.balanceBST(t.root), new_tree);
  258.                 t = t.Balance_Tree(new_tree);
  259.             }
  260.             bool is_null = 0;
  261.             is_edit = 0;
  262.             ll ans = 0;
  263.             t.next(ans, t.root, x, true, is_null);
  264.             if (is_null) {
  265.                 cout << 0 << "\n";
  266.             }
  267.             else if(ans != 0) {
  268.                 cout << ans << '\n';
  269.             }
  270.             else {
  271.                 cout << "none" << '\n';
  272.             }
  273.         }
  274.         else if (q == "prev") {
  275.             if (is_edit) {
  276.                 vector<ll>new_tree;
  277.                 t.preOrder(t.root, new_tree);
  278.                 t = t.Balance_Tree(new_tree);
  279.             }
  280.             bool is_null = 0;
  281.             is_edit = 0;
  282.             ll ans = 0;
  283.             t.prev(ans, t.root, x, true, is_null);
  284.             if (is_null) {
  285.                 cout << 0 << "\n";
  286.             }
  287.             else if (ans != 0) {
  288.                 cout << ans << '\n';
  289.             }
  290.             else {
  291.                 cout << "none" << '\n';
  292.             }
  293.         }
  294.     }
  295.  
  296.     return 0;
  297. }
  298.  
  299.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement