Advertisement
shek_shek

Randomize tree

Apr 27th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4. #include <string>
  5. #include <algorithm>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. struct node
  12. {
  13.     int key;  
  14.     int size;
  15.     node* left;
  16.     node* right;
  17.     node(int k) { key = k; left = right = 0; size = 1; }
  18.     node() {key = -1; left = right = 0; size = 0; }
  19. };
  20.  
  21. int getsize(node* p)
  22. {
  23.     if(!p) return 0;
  24.     return p->size;
  25. }
  26.  
  27.  
  28. void split (node *t, int k, node* l, node* r) {
  29.     if (getsize(t) == 0) {     
  30.         l = new node();
  31.         r = new node();
  32.     }
  33.     else
  34.         if (k < t->key) {
  35.             r = t;
  36.             split (t->left, k, l, r->left);
  37.         }
  38.         else {
  39.             l = t;
  40.             split (t->right, k, l->right, r);
  41.         }
  42. }
  43.  
  44.  
  45. node* ffind(node* p, int k)
  46. {
  47.     if( !p ) return 0;
  48.     if( k == p->key )
  49.         return p;
  50.     if( k < p->key )
  51.         return ffind(p->left,k);
  52.     else
  53.         return ffind(p->right,k);
  54. }
  55.  
  56. bool check(node* p, int k) {
  57.     if (ffind(p, k) == p)
  58.         return true;
  59.     return false;
  60. }
  61.  
  62. node* insert_at_root (node* t, int k) {
  63.     node* l = new node();
  64.     node* r = new node();
  65.     split(t, k, l, r);
  66.     t = new node(k);
  67.     t->key = k;
  68.     t->left = l;
  69.     t->left = r;
  70.     return t;
  71. }
  72.  
  73. node* insert (node* t, int k) {
  74.     int r = rand() % (t->size + 1);
  75.     if (r == t->size)
  76.         return t = insert_at_root(t, k);
  77.     if (k < t->key)
  78.         t = insert(t->left, k);
  79.     else
  80.         t = insert(t->right, k);
  81.     return t;
  82. }
  83.  
  84.  
  85. node* join(node* l, node* r) {
  86.     int m = l->size;
  87.     int n = r->size;
  88.     int total = m + n;
  89.     if (total == 0) {
  90.         node* t = new node();
  91.         return t;
  92.     }
  93.     int rr = rand() % (total + 1);
  94.     if (rr == 0) r++;
  95.     if (rr < m) {
  96.         // с вероятностью m / (m + n)
  97.         l->right = join(l->right, r);
  98.         return l;
  99.     }
  100.     if (rr < m) {
  101.         // с вероятностью m / (m + n)
  102.         r->left = join(l, r->left);
  103.         return r;
  104.     }
  105. }
  106.  
  107. node* remove(node* t, int k) {
  108.     if (getsize(t) == 0) {
  109.         t = new node(k);
  110.         return t;
  111.     }
  112.     if (k < t->key)
  113.         t->left = remove(t->left, k);
  114.     else
  115.         if (k > t->key)
  116.             t->right = remove(t->right, k);
  117.         else {
  118.             node* q = join(t->left, t->right);
  119.             t = q;
  120.         }
  121.         return t;
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. int main() {
  130.     freopen("input.txt","r",stdin);
  131.     freopen("output.txt","w",stdout);
  132.     node* t = new node();
  133.     int begin = 0;
  134.     int end = 100;
  135.     for (int i = begin; i <= end; i++) {
  136.         insert(t, i);
  137.     }
  138.     cout << "Время работы insert от " << begin << " до " << end << ": " << double(clock()) << " миллисекунд" << endl;
  139.  
  140.     for (int i = begin; i <= end; i++) {
  141.         if (check(t, i))
  142.             cout << 1 << endl;
  143.         else
  144.             cout << 0 << endl;
  145.     }
  146.     cout << endl;
  147.     cout << "Время работы check от " << begin << " до " << end << ": " << double(clock()) << " миллисекунд" << endl;
  148.     cout << check(t, 10000) << endl;
  149.  
  150.  
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement