Advertisement
LDG2875

Treap

May 9th, 2022
1,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Treap
  6. {
  7.     int key, p;
  8.     Treap *left, *right;
  9. };
  10.  
  11. Treap* New_node(int x)
  12. {
  13.     Treap* nod = new Treap;
  14.     nod->key = x;
  15.     nod->p = rand();
  16.     nod->left = nod->right = NULL;
  17.     return nod;
  18. }
  19.  
  20. Treap* rot_right(Treap* y)
  21. {
  22.     Treap *x = y->left;
  23.     y->left = x->right;
  24.     x->right = y;
  25.     return x;
  26. }
  27.  
  28. Treap* rot_left(Treap* y)
  29. {
  30.     Treap *x = y->right;
  31.     y->right = x->left;
  32.     x->left = y;
  33.     return x;
  34. }
  35.  
  36. void Insert(Treap* &node, int key)
  37. {
  38.     if(node == NULL)
  39.         node = New_node(key);
  40.     else
  41.     {
  42.         if(key < node->key)
  43.         {
  44.             Insert(node->left, key);
  45.             if(node->p < node->left->p)
  46.                 node = rot_right(node);
  47.         }
  48.         else
  49.         {
  50.             Insert(node->right, key);
  51.             if(node->p < node->right->p)
  52.                 node = rot_left(node);
  53.         }
  54.     }
  55. }
  56.  
  57. Treap* Search(Treap* node, int key)
  58. {
  59.     if(node->key == key)
  60.         return node;
  61.     if(key < node->key)
  62.         Search(node->left, key);
  63.     else
  64.         Search(node->right, key);
  65. }
  66.  
  67. Treap* Delete(Treap* node, int key)
  68. {
  69.     if(!node)
  70.         return node;
  71.     if(key < node->key)
  72.         node->left = Delete(node->left, key);
  73.     else if(key > node->key)
  74.         node->right = Delete(node->right, key);
  75.     else if(!node->left || !node->right)
  76.     {
  77.         Treap* aux = (node->left ? node->left : node->right);
  78.         delete node;
  79.         node = aux;
  80.     }
  81.     else if(node->left->p > node->right->p)
  82.     {
  83.         node = rot_right(node);
  84.         node->right = Delete(node->right, key);
  85.     }
  86.     else
  87.     {
  88.         node = rot_left(node);
  89.         node->left = Delete(node->left, key);
  90.     }
  91.     return node;
  92. }
  93.  
  94. void Inorder(Treap* node)
  95. {
  96.     if(node)
  97.     {
  98.         Inorder(node->left);
  99.         cout << "Nodul curent este: " << node->key << " " << node->p << " ";
  100.         if(node->left)
  101.             cout << "Fiul sau stang are valoarea: " << node->left->key << " " << node->left->p << " ";
  102.         if(node->right)
  103.             cout << "Fiul sau drept are valoarea: " << node->right->key << " " << node->right->p << " ";
  104.         cout << '\n';
  105.         Inorder(node->right);
  106.     }
  107. }
  108.  
  109. int main()
  110. {
  111.     cout << "Treap\n";
  112.     cout << "Alegeti una dintre opiunile urmatoare:\n";
  113.     cout << "1.Inserare nod.\n";
  114.     cout << "2.Cautare valoare.\n";
  115.     cout << "3.Stergere nod.\n";
  116.     cout << "4.Afisarea treapului.\n";
  117.     int T, c, key;
  118.     Treap* root = NULL;
  119.     while(1)
  120.     {
  121.         cin >> c;
  122.         if(c == 1)
  123.         {
  124.             cout << "Introduceti valoarea nodului de inserat: ";
  125.             cin >> key;
  126.             Insert(root, key);
  127.             cout << "Nod inserat!\n";
  128.         }
  129.         else if(c == 2)
  130.         {
  131.             cout << "Introduceti valoarea nodului de cautat: ";
  132.             cin >> key;
  133.             Treap* nod = NULL;
  134.             nod = Search(root, key);
  135.             if(!nod)
  136.                 cout << key << "Nu exista in treap acum!\n";
  137.             else
  138.                 cout << nod->key << " " << nod->p << '\n';
  139.         }
  140.         else if(c == 3)
  141.         {
  142.             cout << "Introduceti valoarea nodului de sters: ";
  143.             cin >> key;
  144.             root = Delete(root, key);
  145.         }
  146.         else if(c == 4)
  147.         {
  148.             //cout << "Ta daaa\n";
  149.             Inorder(root);
  150.             cout << '\n';
  151.         }
  152.     }
  153.     return 0;
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement