Guest User

Untitled

a guest
Jan 10th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. int result = -1;
  5.  
  6. class node
  7. {
  8. public:
  9.     int key, height; // Key and height of the node
  10.  
  11.     // Every node has only one left and right child (if they exist of course)
  12.     node *left_child;
  13.     node *right_child;
  14.  
  15.     // Constructor
  16.     node () {}
  17.  
  18.     node(int key, int height)
  19.     {
  20.         this -> key = key;
  21.         this -> height = height;
  22.     }
  23.  
  24.     // Make new node with given value
  25.     node* new_node(int val)
  26.     {
  27.         // Allocate memory for new node
  28.         node* x = (node*)malloc(sizeof(struct node));
  29.  
  30.         x->key = val;
  31.         x->left_child = NULL;
  32.         x->right_child = NULL;
  33.         x->height = 1; // Currently a leaf
  34.  
  35.         // Return the new node
  36.         return x;
  37.     }
  38. };
  39.  
  40. // Helper function to return the height at which the node x is
  41. int get_height(node *x)
  42. {
  43.     if (x == NULL) { // Empty
  44.         return 0;
  45.     }
  46.     return x->height;
  47. }
  48.  
  49. // Return the predecessor of the current node
  50. node* get_predecessor(node *current)
  51. {
  52.     node* x = current;
  53.     while (x->right_child != NULL)
  54.     {
  55.         x = x->right_child;
  56.     }
  57.     return x;
  58. }
  59.  
  60. // Return the successor of the current node
  61. node* get_successor(node *current)
  62. {
  63.     node* x = current;
  64.     while (x->left_child != NULL)
  65.     {
  66.         x = x->left_child;
  67.     }
  68.     return x;
  69. }
  70.  
  71. node* right_rotate(node *current)
  72. {
  73.     node *child = current->left_child;
  74.     node *subtree = child->right_child;
  75.  
  76.     // Rotate (i.e. change places)
  77.     child->right_child = current;
  78.     current->left_child = subtree;
  79.  
  80.     // Update the height values
  81.     current->height = max(get_height(current->left_child), get_height(current->right_child)) + 1;
  82.     child->height = max(get_height(child->left_child), get_height(child->right_child)) + 1;
  83.  
  84.     return child;
  85. }
  86.  
  87. node* left_rotate(node *current)
  88. {
  89.     node *child = current->right_child;
  90.     node *subtree = child->left_child;
  91.  
  92.     // Rotate
  93.     child->left_child = current;
  94.     current->right_child = subtree;
  95.  
  96.     // Update height values
  97.     current->height = max(get_height(current->left_child), get_height(current->right_child)) + 1;
  98.     child->height = max(get_height(child->left_child), get_height(child->right_child)) + 1;
  99.  
  100.     return child;
  101. }
  102.  
  103. // Delete the node in the BST with value = key
  104. node* delete_node(node* parent, int key)
  105. {
  106.     if (parent == NULL) {
  107.         return parent;
  108.     }
  109.  
  110.     // Continue on right subtree
  111.     if (key > parent->key) {
  112.         delete_node(parent->right_child, key);
  113.     }
  114.     // Continue on left subtree
  115.     else if (key < parent->key) {
  116.         delete_node(parent->left_child, key);
  117.     }
  118.     // If the key is the same as the value of the current node, then this is the wanted node
  119.     else {
  120.         // Only one or no child
  121.         if (parent->left_child == NULL or parent->right_child == NULL)
  122.         {
  123.             node* child;
  124.             if (parent->right_child != NULL) {
  125.                 child = parent->right_child;
  126.             } else {
  127.                 child = parent->left_child;
  128.             }
  129.  
  130.             // No child
  131.             if (child == NULL)
  132.             {
  133.                 child = parent;
  134.                 parent = NULL;
  135.             }
  136.             else // One child: copy the contents (key, height etc)
  137.             {
  138.                 *parent = *child;
  139.             }
  140.         }
  141.         // Two children
  142.         else
  143.         {
  144.             node* new_parent = get_predecessor(parent->left_child);
  145.             parent->key = new_parent->key; // Copy only the new key
  146.  
  147.             // Delete the predecessor since it's been replaced
  148.             parent->left_child = delete_node(parent->left_child, new_parent->key);
  149.         }
  150.     }
  151.  
  152.     // Get height of the node
  153.     parent->height = max(get_height(parent->left_child), get_height(parent->right_child)) + 1;
  154.  
  155.     int state;
  156.     if (parent == NULL) {
  157.         state = 0;
  158.     }
  159.     else {
  160.         state = get_height(parent->left_child) - get_height(parent->right_child);
  161.     }
  162.  
  163.     // 1.1. Left child of parent is left-weighted
  164.     if (state > 1 and key < parent->left_child->key)
  165.     {
  166.         return right_rotate(parent);
  167.     }
  168.     // 1.2 Left child of parent is right-weighted
  169.     if (state > 1 and key > parent->left_child->key)
  170.     {
  171.         parent->left_child = left_rotate(parent->left_child);
  172.         return right_rotate(parent);
  173.     }
  174.  
  175.     // 2.1. Right child of parent is right-weighted
  176.     if (state < -1 and key > parent->right_child->key)
  177.     {
  178.         return left_rotate(parent);
  179.     }
  180.     // 2.2. Right child of parent is left-weighted
  181.     if (state < -1 and key < parent->right_child->key)
  182.     {
  183.         parent->right_child = right_rotate(parent->right_child);
  184.         return left_rotate(parent);
  185.     }
  186.     return parent;
  187. }
  188.  
  189. // parent is the previous node and key is the value of the element that should be inserted
  190. node* insert_node(node* parent, int key)
  191. {
  192.     if (parent == NULL) {
  193.         return node().new_node(key);
  194.     }
  195.  
  196.     // Decide where to continue with recursion
  197.     if (key < parent->key) {
  198.         parent->left_child = insert_node(parent->left_child, key);
  199.     }
  200.     else if (key > parent->key) {
  201.         parent->right_child = insert_node(parent->right_child, key);
  202.     }
  203.     else {
  204.         return parent;
  205.     }
  206.  
  207.     // Height of a parent is the bigger height of the two children plus 1
  208.     parent->height = max(get_height(parent->left_child), get_height(parent->right_child)) + 1;
  209.  
  210.     int state;
  211.     if (parent == NULL) {
  212.         state = 0;
  213.     }
  214.     else {
  215.         state = get_height(parent->left_child) - get_height(parent->right_child);
  216.     }
  217.  
  218.     // 1.1. Left child of parent is left-weighted
  219.     if (state > 1 and key < parent->left_child->key)
  220.     {
  221.         return right_rotate(parent);
  222.     }
  223.     // 1.2 Left child of parent is right-weighted
  224.     if (state > 1 and key > parent->left_child->key)
  225.     {
  226.         parent->left_child = left_rotate(parent->left_child);
  227.         return right_rotate(parent);
  228.     }
  229.  
  230.     // 2.1. Right child of parent is right-weighted
  231.     if (state < -1 and key > parent->right_child->key)
  232.     {
  233.         return left_rotate(parent);
  234.     }
  235.     // 2.2. Right child of parent is left-weighted
  236.     if (state < -1 and key < parent->right_child->key)
  237.     {
  238.         parent->right_child = right_rotate(parent->right_child);
  239.         return left_rotate(parent);
  240.     }
  241.     return parent;
  242. }
  243.  
  244. node* query(node* parent, int key)
  245. {
  246.     if (parent == NULL) {
  247.         return parent;
  248.     }
  249.     // The node is in the left subtree
  250.     if (key < parent->key)
  251.     {
  252.         query(parent->left_child, key);
  253.     }
  254.     // The node is in the right subtree
  255.     else if (key > parent->key)
  256.     {
  257.         query(parent->right_child, key);
  258.     }
  259.     else // The needed node is this
  260.     {
  261.         // Return it's predecessor
  262.         node *predecessor = get_predecessor(parent->left_child);
  263.         result = predecessor->key;
  264.         return predecessor;
  265.     }
  266.     return parent;
  267. }
  268.  
  269. int main(void)
  270. {
  271.     ios::sync_with_stdio(false);
  272.     cin.tie(0); cout.tie(0);
  273.  
  274.     int tasks;
  275.     node *root = NULL; // The root of the AVL tree
  276.  
  277.     cin >> tasks;
  278.     while (tasks--)
  279.     {
  280.         int type, number;
  281.         cin >> type >> number;
  282.         if (type == 0) { // Print the predecessor of number
  283.             query(root, number);
  284.             cout << result << endl;
  285.             result = -1;
  286.         }
  287.         if (type == 1) { // Insert the current number in the BST
  288.             root = insert_node(root, number);
  289.         }
  290.         if (type == 2) { // Delete the current number from the BST
  291.             root = delete_node(root, number);
  292.         }
  293.     }
  294.     return 0;
  295. }
Advertisement
Add Comment
Please, Sign In to add comment