Advertisement
dipBRUR

Treap

Oct 3rd, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.14 KB | None | 0 0
  1. Entry example
  2. 9
  3. 15 39
  4. 6 33
  5. 17 21
  6. 5 19
  7. 13 29
  8. 8 17
  9. 14 15
  10. 11 9
  11. 9 51
  12. // We consider a max-Heap
  13. struct Node
  14. {
  15.     int key, priority, sze;            // key and priority of a node
  16.     Node *left, *right;                // left and right subtree
  17.     Node(int k, int p): key(k), priority(p), left(NULL), right(NULL) {} // for test
  18.     Node(int k): key(k), left(NULL), right(NULL), priority(rand()) {}
  19. };
  20.  
  21. int sz(Node *root)
  22. {
  23.     return root == NULL ? 0 : root->sze;
  24. }
  25.  
  26. void update_sze(Node *root)
  27. {
  28.     if (root != NULL)
  29.         root->sze = sz(root->left) + 1 + sz(root->right);
  30. }
  31.  
  32. // Search for a node by its key, complies with BST properties
  33. Node *Search(Node *root, int key)
  34. {
  35.     if (root == NULL)
  36.         return NULL;
  37.     if (key < root->key)
  38.         return Search(root->left, key);
  39.     if (key > root->key)
  40.         return Search(root->right, key);
  41.     return root;
  42. }
  43.  
  44. // Split: Enter the root of the tree, the key of the node to be Inserted and the left and
  45. //        right subtrees
  46. void Split(Node *root, int key, Node *&L, Node *&R)
  47. {
  48.     if (root == NULL)
  49.     {
  50.         L = R = NULL;
  51.         return;
  52.     }
  53.     else if(root->key < key)
  54.     {
  55.         split (root->right, key, root->right, R);
  56.         L = root;
  57.     }
  58.     else
  59.     {
  60.         split(root->left, key, L, root->left);
  61.         R = root;
  62.     }
  63.     update_sze(root);
  64. }
  65.  
  66. // Merge: I receive a Left Tree and a Right Tree
  67. // All the keys of L must be less than R
  68. Node *Merge(Node *L, Node *R)
  69. {
  70.     Node *root; // This will be our new tree
  71.    
  72.  
  73.     if (L == NULL || R == NULL)
  74.     {
  75.         root = (L! = NULL ? L: R);
  76.     }
  77.     else if (L->priority < R->priority)
  78.     {
  79.         R->left = Merge(L, R->left);
  80.         root = R;
  81.     }
  82.     else
  83.     {
  84.         L->right = Merge(L->right, R);
  85.         root = L;
  86.     }
  87.     update_sze(root);
  88.     return root;
  89. }
  90.  
  91. // Insertion: I receive a tree and the node to Insert
  92. Node *Insert(Node *root, Node *newNode)
  93. {
  94.     if (root == NULL)
  95.         return newNode;
  96.     if (root->priority < newNode->priority)
  97.     {
  98.         // if it does not comply with the heap property in the current location
  99.         split (root, newNode->key, newNode->left, newNode->right); //Insert node through split
  100.         update_sze(newNode);
  101.         return newNode; // return new node with its respective left and right subtrees
  102.     }
  103.     // Search for the element by the key.
  104.     if (root->key < newNode->key)
  105.     {
  106.         root->right = Insert(root->right, newNode);
  107.     }
  108.     else
  109.     {
  110.         root->left = Insert(root->left, newNode);
  111.     }
  112.     update_sze(root);
  113.     return root;
  114. }
  115.  
  116. // Elimination: I receive a tree and the key of the node to be deleted
  117. Node *Erase(Node *root, int key)
  118. {
  119.     if (root == NULL)
  120.         return NULL;
  121.     if (root->key == key)
  122.     {
  123.         // if I found it, I mix its sub-trees
  124.         Node *aux = Merge(root->left, root->right);
  125.         delete root; // delete current node
  126.         update_sze(aux);
  127.         return aux; // return new tree
  128.     }
  129.     // Node search by key
  130.     if (root->key < key)
  131.     {
  132.         root->right = Erase(root->right, key);
  133.     }
  134.     else
  135.     {
  136.         root->left = Erase(root->left, key);
  137.     }
  138.     update_sze(root);
  139.     return root;
  140. }
  141.  
  142. Node *kth(Node *root, int k)
  143. {
  144.     if (sz(root) < k)
  145.         return NULL;
  146.     int inleft = sz(root->left) + 1;
  147.     if (ans == k)
  148.         return root;
  149.     else
  150.         return kth(root->right, k-inleft);
  151.     else
  152.         return kth(root->left, k);
  153. }
  154.  
  155. int Count(Node *root, int key) // Count total number less than key
  156. {
  157.     if (root == NULL)
  158.         return 0;
  159.     if (root->key == key)
  160.         return sz(root->left);
  161.     else if (root->key > key)
  162.         return Count(root->left, key);
  163.     else if (root->key < key)
  164.         return sz(root->left) + 1 + Count(root->right, key);
  165. }
  166.        
  167. // We clean the test tree
  168. Node *Clear(Node *root)
  169. {
  170.     if (root != NULL)
  171.     {
  172.         Clear(root->left);
  173.         Clear(root->right);
  174.         delete root;
  175.     }
  176. }
  177.  
  178. // Pre-order Tour of the Treap
  179. void Traverse(Node *root)
  180. {
  181.     if (root == NULL)
  182.         return;
  183.  
  184.     printf("[%d,%d]->", root->key, root->priority);
  185.  
  186.     Traverse (root -> left);
  187.     Traverse (root -> right);
  188. }
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195. int main()
  196. {
  197.     Node *treap = NULL;
  198.     int operations;
  199.     scanf("%d", &operations);
  200.     char s[2];
  201.     int item;
  202.     for (int i = 0; i < operations; i++)
  203.     {
  204.         scanf("%s", s);
  205.         scanf("%d", &item);
  206.         if (s[0] == 'I')
  207.         {
  208.             Node *ss = Search(treap, item);
  209.             if (ss == NULL)
  210.                 treap = Insert(treap, new Node(item));
  211.         }
  212.         else if (s[0] == 'D')
  213.         {
  214.             treap = Erase(treap, item);
  215.         }
  216.         else if (s[0] == 'C')
  217.         {
  218.             int ans = Count(treap, item);
  219.             printf("%d\n", ans);
  220.         }
  221.         else if (s[0] == 'K')
  222.         {
  223.             Node *k = kth(treap, item);
  224.             if (k == NULL)
  225.                 puts("invalid");
  226.             else
  227.                 printf("%d\n", k->key);
  228.         }
  229.     }
  230.     //Traverse(treap);
  231.     Clear(treap);
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement