46 void Iterate(
double,
double,
const std::function<
void (
const key &)> &);
68 nodes[n].randValue = random();
69 assert(nodes[n].right == nullTreapNode);
70 assert(nodes[n].left == nullTreapNode);
71 if (head == nullTreapNode)
74 nodes[n].parent = rootOfTree;
84 while (nodes[n].parent != rootOfTree)
87 if (nodes[nodes[n].parent].randValue >= nodes[n].randValue)
90 if (nodes[nodes[n].parent].left == n)
92 RotateLeftChildUp(nodes[n].parent);
95 assert(nodes[nodes[n].parent].right == n);
96 RotateRightChildUp(nodes[n].parent);
112 if (nodes[n].parent != rootOfTree)
114 if (nodes[nodes[n].parent].right == n)
115 nodes[nodes[n].parent].right = l;
117 assert(nodes[nodes[n].parent].left == n);
118 nodes[nodes[n].parent].left = l;
126 nodes[l].parent = nodes[n].parent;
128 if (lr != nullTreapNode)
129 nodes[lr].parent = n;
145 if (nodes[n].parent != rootOfTree)
147 if (nodes[nodes[n].parent].right == n)
148 nodes[nodes[n].parent].right = r;
150 assert(nodes[nodes[n].parent].left == n);
151 nodes[nodes[n].parent].left = r;
159 nodes[r].parent = nodes[n].parent;
161 if (rl != nullTreapNode)
162 nodes[rl].parent = n;
170 if (nodes[n].value < nodes[where].value)
172 if (nodes[where].left == nullTreapNode)
174 nodes[where].left = n;
175 nodes[n].parent = where;
180 Insert(n, nodes[where].left);
184 if (nodes[where].right == nullTreapNode)
186 nodes[where].right = n;
187 nodes[n].parent = where;
192 Insert(n, nodes[where].right);
208 nodes.resize(nodes.size()+1);
210 nodes[t].right = nodes[t].left = nullTreapNode;
218 if (n == nodes.size()-1)
220 nodes.resize(nodes.size()-1);
222 head = nullTreapNode;
226 nodes[n] = nodes[nodes.size()-1];
227 nodes.resize(nodes.size()-1);
228 if (nodes[n].parent != rootOfTree)
230 if (nodes[nodes[n].parent].right == nodes.size())
231 nodes[nodes[n].parent].right = n;
233 assert(nodes[nodes[n].parent].left == nodes.size());
234 nodes[nodes[n].parent].left = n;
241 if (nodes[n].left != nullTreapNode)
242 nodes[nodes[n].left].parent = n;
244 if (nodes[n].right != nullTreapNode)
245 nodes[nodes[n].right].parent = n;
257 while (n != nullTreapNode)
259 if (k == nodes[n].value)
265 if (k < nodes[n].value)
280 assert(head != nullTreapNode);
282 while (nodes[n].left != nullTreapNode)
284 key k = nodes[n].value;
292 IterateHelper(head, low, high, f);
298 if (n == nullTreapNode)
300 if (nodes[n].value > low && nodes[n].value <= high)
304 if (nodes[n].value <= high)
305 IterateHelper(nodes[n].right, low, high, f);
306 if (nodes[n].value > low)
307 IterateHelper(nodes[n].left, low, high, f);
316 assert(head != nullTreapNode);
318 while (nodes[n].left != nullTreapNode)
320 return nodes[n].value;
327 if (nodes[n].left == nullTreapNode && nodes[n].right == nullTreapNode)
329 if (nodes[n].parent == rootOfTree)
331 head = nullTreapNode;
335 if (nodes[nodes[n].parent].left == n)
337 nodes[nodes[n].parent].left = nullTreapNode;
340 assert(nodes[nodes[n].parent].right == n);
341 nodes[nodes[n].parent].right = nullTreapNode;
346 else if (nodes[n].left == nullTreapNode)
348 if (nodes[n].parent == rootOfTree)
350 head = nodes[n].right;
351 nodes[nodes[n].right].parent = rootOfTree;
353 else if (nodes[nodes[n].parent].left == n)
355 nodes[nodes[n].parent].left = nodes[n].right;
356 nodes[nodes[n].right].parent = nodes[n].parent;
359 assert(nodes[nodes[n].parent].right == n);
360 nodes[nodes[n].parent].right = nodes[n].right;
361 nodes[nodes[n].right].parent = nodes[n].parent;
365 else if (nodes[n].right == nullTreapNode)
367 if (nodes[n].parent == rootOfTree)
369 head = nodes[n].left;
370 nodes[nodes[n].left].parent = rootOfTree;
372 else if (nodes[nodes[n].parent].left == n)
374 nodes[nodes[n].parent].left = nodes[n].left;
375 nodes[nodes[n].left].parent = nodes[n].parent;
378 assert(nodes[nodes[n].parent].right == n);
379 nodes[nodes[n].parent].right = nodes[n].left;
380 nodes[nodes[n].left].parent = nodes[n].parent;
393 while (nodes[next].left != nullTreapNode)
394 next = nodes[next].left;
395 nodes[n].value = nodes[next].value;
405 PrintHelper(head, 0);
412 if (n == nullTreapNode)
414 if (nodes[n].right != nullTreapNode)
415 PrintHelper(nodes[n].right, depth+1);
416 for (
int x = 0; x < 4*depth; x++)
418 std::cout << nodes[n].value <<
" (d" << depth <<
", " << nodes[n].randValue <<
")\n";
419 if (nodes[n].left != nullTreapNode)
420 PrintHelper(nodes[n].left, depth+1);
427 assert(nodes[head].parent == rootOfTree);
434 if (nodes[n].left != nullTreapNode)
436 assert(nodes[nodes[n].left].parent == n);
437 assert(nodes[n].randValue >= nodes[nodes[n].left].randValue);
438 VerifyHelper(nodes[n].left);
440 if (nodes[n].right != nullTreapNode)
442 assert(nodes[nodes[n].right].parent == n);
443 assert(nodes[n].randValue >= nodes[nodes[n].right].randValue);
444 VerifyHelper(nodes[n].right);
457 return nodes[which].value;
463 return GetHeightHelper(head);
470 if (n == nullTreapNode)
472 if (nodes[n].right != nullTreapNode)
473 h =
std::max(h, 1+GetHeightHelper(nodes[n].right));
474 if (nodes[n].left != nullTreapNode)
475 h =
std::max(h, 1+GetHeightHelper(nodes[n].left));