-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreap.cpp
More file actions
164 lines (159 loc) · 4.02 KB
/
treap.cpp
File metadata and controls
164 lines (159 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <utility>
#include <memory.h>
#include <ctime>
using namespace std;
template<typename T>
struct treap {
struct _Node {
_Node()
{ this->lch = this->rch = NULL;
this->rank = rand(); this->size = 1; }
void maintain()
{
printf("maintain: maintaining tree of val:");
cout << this->val << " ";
this-> size = 1;
if (this->rch != NULL)
this-> size += this->rch->size;
if (this->lch != NULL)
this-> size += this->lch->size;
cout << this->size << endl;}
void purge()
{ if (this->rch != NULL) this->rch->purge();
if (this->lch != NULL) this->lch->purge();
delete this->rch; delete this->lch; }
unsigned long size;
_Node * lch;
_Node * rch;
T val;
int rank;
};
_Node* root;
treap()
{ srand(time(NULL));
this->root = NULL; }
~treap()
{ if (this->root!=NULL)
this->root->purge();
if (this->root!=NULL)
delete this->root; }
void rotate_left(_Node*& o)
{ if (o->rch == NULL) return;
_Node* k = o->rch;
o->rch = k->lch;
k->lch = o;
o = k;
o->maintain();
k->maintain(); }
void rotate_right(_Node*& o)
{ if (o->lch == NULL) return;
_Node* k = o->lch;
o->lch = k->rch;
k->rch = o;
o = k;
o->maintain();
k->maintain(); }
void insert(_Node*& o, T& x)
{ if (o==NULL) {
o = new _Node();
o->val = x;
o->rank = rand();
} else {
if (x < o-> val) {
insert(o->lch, x);
if (o->lch->rank > o->rank)
rotate_right(o);
} else if (x > o-> val) {
insert(o->rch, x);
if (o->rch->rank > o->rank)
rotate_left(o);
} else return; // already exists
}}
void insert(T x)
{ this->insert(root, x); }
void remove(_Node*&o, T& x)
{ if (o==NULL)return;
if (x == o->val) {
if (o->lch == NULL) o = o->rch;
else if (o->rch == NULL) o = o->lch;
else {
if (o->lch->rank > o->rch->rank) {
rotate_right(o);
remove(o->rch, x);
} else {
rotate_left(o);
remove(o->lch, x);
}
}
o->maintain();
} else if (x < o->val) {
remove(o->lch, x);
o->maintain();
} else {
remove(o->rch, x);
o->maintain();}}
void remove(T x)
{ this->remove(root, x); }
_Node* find(_Node* o, T& x)
{ if(o==NULL)return NULL;
if (o->val == x) return o;
else if (x < o->val) {
if (o->lch == NULL) return NULL;
else return find(o->lch, x);
} else {
if (o->rch == NULL) return NULL;
else return find(o->rch, x);}}
_Node* find(T x)
{ return this->find(root, x); }
_Node* kth(_Node* o, unsigned long k) // Find o s.t. o->s+1=k
{ if(o==NULL)return NULL;
unsigned long order = o->rch == NULL ? 0 : o->rch->size + 1;
if (o->size < k) return NULL;
else if (k <= order) {
if (o->rch != NULL) return kth(o->rch, k);
else return NULL;
} else {
if (o->lch != NULL) return kth(o->lch, k-order-1);
else return NULL;
}
}
_Node* kth(unsigned long k)
{ return this->kth(root, k); }
unsigned long rank(_Node* o, T& x)
{ if (o==NULL)return 0;
if (o->val == x) {
unsigned long r = o->lch == NULL ? 0 : o->lch->size;
return (unsigned long) r;
} else if (o->val > x) {
if (o->lch == NULL) {
return 0;
} else if (o->lch->val < x) { // like case above
return o->lch->size;
} else // >=, leave for recursion to handle
return rank(o->lch, x);
} else // <, return size of entire tree
return o->size;
}
unsigned long rank(T x)
{ return this->rank(root, x); }
unsigned long size()
{ return this->root->size; }
};
int main()
{
treap<long> T;
T.insert(long(1L));
T.insert(long(2L));
T.insert(long(3L));
T.insert(5L); T.insert(9L);
cout << "size of tree:"<< T.size() << endl;
cout << T.find(2L)->val << endl;
cout << T.find(9L)->val << endl;
cout << T.kth(3)->val << endl;
return 0;
}