Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <math.h>
- #include <string>
- #include <algorithm>
- #include <ctime>
- using namespace std;
- struct node
- {
- int key;
- int size;
- node* left;
- node* right;
- node(int k) { key = k; left = right = 0; size = 1; }
- node() {key = -1; left = right = 0; size = 0; }
- };
- int getsize(node* p)
- {
- if(!p) return 0;
- return p->size;
- }
- void split (node *t, int k, node* l, node* r) {
- if (getsize(t) == 0) {
- l = new node();
- r = new node();
- }
- else
- if (k < t->key) {
- r = t;
- split (t->left, k, l, r->left);
- }
- else {
- l = t;
- split (t->right, k, l->right, r);
- }
- }
- node* ffind(node* p, int k)
- {
- if( !p ) return 0;
- if( k == p->key )
- return p;
- if( k < p->key )
- return ffind(p->left,k);
- else
- return ffind(p->right,k);
- }
- bool check(node* p, int k) {
- if (ffind(p, k) == p)
- return true;
- return false;
- }
- node* insert_at_root (node* t, int k) {
- node* l = new node();
- node* r = new node();
- split(t, k, l, r);
- t = new node(k);
- t->key = k;
- t->left = l;
- t->left = r;
- return t;
- }
- node* insert (node* t, int k) {
- int r = rand() % (t->size + 1);
- if (r == t->size)
- return t = insert_at_root(t, k);
- if (k < t->key)
- t = insert(t->left, k);
- else
- t = insert(t->right, k);
- return t;
- }
- node* join(node* l, node* r) {
- int m = l->size;
- int n = r->size;
- int total = m + n;
- if (total == 0) {
- node* t = new node();
- return t;
- }
- int rr = rand() % (total + 1);
- if (rr == 0) r++;
- if (rr < m) {
- // с вероятностью m / (m + n)
- l->right = join(l->right, r);
- return l;
- }
- if (rr < m) {
- // с вероятностью m / (m + n)
- r->left = join(l, r->left);
- return r;
- }
- }
- node* remove(node* t, int k) {
- if (getsize(t) == 0) {
- t = new node(k);
- return t;
- }
- if (k < t->key)
- t->left = remove(t->left, k);
- else
- if (k > t->key)
- t->right = remove(t->right, k);
- else {
- node* q = join(t->left, t->right);
- t = q;
- }
- return t;
- }
- int main() {
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- node* t = new node();
- int begin = 0;
- int end = 100;
- for (int i = begin; i <= end; i++) {
- insert(t, i);
- }
- cout << "Время работы insert от " << begin << " до " << end << ": " << double(clock()) << " миллисекунд" << endl;
- for (int i = begin; i <= end; i++) {
- if (check(t, i))
- cout << 1 << endl;
- else
- cout << 0 << endl;
- }
- cout << endl;
- cout << "Время работы check от " << begin << " до " << end << ": " << double(clock()) << " миллисекунд" << endl;
- cout << check(t, 10000) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement