-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreap.cpp
More file actions
117 lines (109 loc) · 2.41 KB
/
Copy pathtreap.cpp
File metadata and controls
117 lines (109 loc) · 2.41 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
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
template <class T>
class Treap {
struct Node {
T key;
int priority;
Node *left, *right;
Node(T k, int p) : key(k), priority(p), left(nullptr), right(nullptr) {}
};
// Node *n will be a new treap
void merge(Node*& n, Node* left, Node* right) {
if (left == nullptr || right == nullptr) {
n = left ? left : right;
} else {
if (left->priority > right->priority) {
n = left;
merge(n->right, left->right, right);
} else {
n = right;
merge(n->left, left, right->left);
}
}
}
void split(Node* root, T key, Node*& l, Node*& r) {
if (!root)
l = r = NULL;
else if (root->key > key) {
r = root;
split(root->left, key, l, root->left);
} else {
l = root;
split(root->right, key, root->right, r);
}
}
Node* insert(Node* root, Node* node) {
if (!root)
return node;
if (node->priority > root->priority) {
split(root, node->key, node->left, node->right);
return node;
} else {
if (node->key < root->key) {
root->left = insert(root->left, node);
} else {
root->right = insert(root->right, node);
}
return root;
}
}
void erase(Node*& n, T value) {
if (n == nullptr) {
return;
}
if (n->key == value) {
merge(n, n->left, n->right);
} else {
if (n->key > value) {
erase(n->left, value);
} else {
erase(n->right, value);
}
}
}
Node* head;
public:
Treap() {
srand(time(NULL));
head = 0;
}
void add(T value) {
Node* n = new Node(value, rand() % 1000);
head = insert(head, n);
}
void erase(T value) { erase(head, value); }
void print() {
queue<Node*> qn;
qn.push(head);
while (!qn.empty()) {
cout << "(" << qn.front()->key << ", " << qn.front()->priority << ") ";
if (qn.front()->left != nullptr) {
qn.push(qn.front()->left);
}
if (qn.front()->right != nullptr) {
qn.push(qn.front()->right);
}
qn.pop();
}
cout << endl;
}
};
int main() {
Treap<int> treap;
cout << "Adding 5 numbers" << endl;
treap.add(5);
treap.add(8);
treap.add(10);
treap.add(11);
treap.add(15);
treap.print();
cout << endl;
cout << "Erasing 10" << endl;
treap.erase(10);
treap.print();
cout << endl;
return 0;
}