-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtreap.ts
More file actions
302 lines (241 loc) · 8.4 KB
/
treap.ts
File metadata and controls
302 lines (241 loc) · 8.4 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
TREAPS
If we insert a set of n items into a binary search tree, the resulting tree may be
horribly unbalanced, leading to long search times. As we saw in Section 12.4,
however, randomly built binary search trees tend to be balanced. Therefore, one
strategy that, on average, builds a balanced tree for a fixed set of items would be to
randomly permute the items and then insert them in that order into the tree.
What if we do not have all the items at once? If we receive the items one at a
time, can we still randomly build a binary search tree out of them?
We will examine a data structure that answers this question in the affirmative. A
treap is a binary search tree with a modified way of ordering the nodes. Figure 13.9
shows an example. As usual, each node x in the tree has a key value x:key. In
addition, we assign x:priority, which is a random number chosen independently
for each node. We assume that all priorities are distinct and also that all keys are
distinct. The nodes of the treap are ordered so that the keys obey the binary-search tree property and the priorities obey the min-heap order property:
1. If is a left child of u, then :key < u:key.
2. If is a right child of u, then :key > u:key.
3. If is a child of u, then :priority > u:priority.
(This combination of properties is why the tree is called a “treap”: it has features
of both a binary search tree and a heap.)
It helps to think of treaps in the following way. Suppose that we insert nodes
x1; x2;:::;xn, with associated keys, into a treap. Then the resulting treap is the
tree that would have been formed if the nodes had been inserted into a normal
binary search tree in the order given by their (randomly chosen) priorities, i.e.,
xi :priority < xj :priority means that we had inserted xi before xj .
*/
import Stack from "./Stack";
class TreapNode<T>{
public left: TreapNode<T>;
public right: TreapNode<T>;
public parent: TreapNode<T>;
public value: T;
public priority: number;
constructor() {
this.left = null;
this.right = null;
this.parent = null;
this.priority = Math.random();
}
}
export default class Treap<T>{
private root: TreapNode<T>;
constructor() {
this.root = null;
}
private insertNode(newNode: TreapNode<T>) {
let insertionPointer: TreapNode<T> = null;
let traversalPointer: TreapNode<T> = this.root;
while (traversalPointer !== null) {
insertionPointer = traversalPointer;
if (newNode.value < traversalPointer.value) {
traversalPointer = traversalPointer.left;
} else {
traversalPointer = traversalPointer.right;
}
}
newNode.parent = insertionPointer;
if (insertionPointer === null) {
this.root = newNode;
} else if (newNode.value < insertionPointer.value) {
insertionPointer.left = newNode;
} else {
insertionPointer.right = newNode;
}
newNode.left = null;
newNode.right = null;
this.insertion_fixup(newNode)
}
/**
* ROTATION ON THE TREE NODE
* Time Complexity: O(1)
*
*
* | |
* Y right-rotate X
* / \ ---------------->>>>> / \
* X a <<<<<---------------- b Y
* / \ left-rotate / \
* b c c a
*
*
*
*/
private rotateTree(direction: 'left' | 'right', node: TreapNode<T>) {
if (direction === 'left') {
this.leftRotate(node);
} else if (direction === 'right') {
this.rightRotate(node);
}
}
private leftRotate(x: TreapNode<T>) {
//Set y
const y = x.right;
//Turn y’s left subtree into x’s right subtree.
x.right = y.left;
if (y.left !== null) {
y.left.parent = x;
}
// Link x’s parent to y.
y.parent = x.parent;
if (x.parent === null) {
this.root = y;
} else if (x.parent.left === x) {
x.parent.left = y
} else {
x.parent.right = y
}
//Put x on y’s left
y.left = x;
x.parent = y;
}
private rightRotate(y: TreapNode<T>) {
//Set x
const x = y.left;
//Turn x’s right subtree into y’s left subtree.
y.left = x.right;
if (x.right !== null) {
x.right.parent = y;
}
// Link y’s parent to x.
x.parent = y.parent;
if (y.parent === null) {
this.root = x;
} else if (y.parent.left === y) {
y.parent.left = x;
} else {
y.parent.right = x
}
//Put y on x’s right
x.right = y;
y.parent = x;
}
private insertion_fixup(newNode: TreapNode<T>) {
while (newNode.parent !== null && newNode.priority > newNode.parent.priority) {
if (newNode === newNode.parent.left) {
this.rotateTree('right', newNode.parent);
} else {
this.rotateTree('left', newNode.parent);
}
}
}
public insert(value: T) {
const node = new TreapNode<T>();
node.value = value;
this.insertNode(node);
}
private findNode(value: T): TreapNode<T> {
let traverser = this.root;
while (traverser !== null && traverser.value !== value) {
if (value < traverser.value) {
traverser = traverser.left
} else {
traverser = traverser.right;
}
}
return traverser;
}
private deleteNode(node: TreapNode<T>) {
node.priority = Number.POSITIVE_INFINITY;
while (node.left || node.right) {
if (!node.right || (node.left && node.left.priority < node.right.priority)) {
this.rotateTree('right', node);
} else {
this.rotateTree('left', node);
}
}
if (node.parent === null) {
this.root = null;
}
else if (node === node.parent.left) {
node.parent.left = null;
} else {
node.parent.right = null
}
node.parent = null;
}
public delete(value: T) {
const node = this.findNode(value)
if (node === null) {
throw `no value with -(${value}) present in tree to delete`;
} else {
this.deleteNode(node);
}
}
// traversals
public inOrder() {
const stack = new Stack<TreapNode<T>>();
const tree: T[] = [];
let traverser = this.root;
while (traverser !== null || !stack.isEmpty()) {
if (traverser === null) {
const parent = stack.pop();
tree.push(parent.value);
traverser = parent.right;
continue;
}
stack.push(traverser);
traverser = traverser.left;
}
return tree;
}
public preOrder() {
const stack = new Stack<TreapNode<T>>();
const tree: T[] = [];
let traverser = this.root;
while (traverser !== null || !stack.isEmpty()) {
if (traverser === null) {
const parent = stack.pop();
traverser = parent.right;
continue;
}
tree.push(traverser.value);
stack.push(traverser);
traverser = traverser.left;
}
return tree;
}
public postOrder() {
const stack = new Stack<TreapNode<T>>();
const tree: T[] = [];
let traverser = this.root;
while (traverser !== null || !stack.isEmpty()) {
if (traverser === null) {
const node = stack.pop();
if (node.right === stack.pop()) {
traverser = node.right;
stack.push(node);
continue;
}
stack.revert();
tree.push(node.value);
continue;
}
if (traverser.right !== null) {
stack.push(traverser.right);
}
stack.push(traverser);
traverser = traverser.left;
}
}
}