-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathTreap.java
142 lines (125 loc) · 3.52 KB
/
Treap.java
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
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
/*
* Author : joney_000[developer.jaswant@gmail.com]
* Algorithm : Treap [As Kth Element] Tree + heap & Rope data structure
* Platform : Codeforces
* Ref : https://threads-iiith.quora.com/Treaps-One-Tree-to-Rule-em-all-Part-1
* https://www.youtube.com/watch?v=erKlLEXLKyY
* https://threads-iiith.quora.com/Treaps-One-Tree-to-Rule-em-all-Part-2
*/
class TreapNode {
static Random random = new Random();
int key;
long prio;
TreapNode left;
TreapNode right;
int count;
TreapNode(int key) {
this.key = key;
this.prio = random.nextLong();;
count = 1;
}
}
class TreapPair {
TreapNode left;
TreapNode right;
TreapPair(TreapNode left, TreapNode right) {
this.left = left;
this.right = right;
}
}
public static class TreapOperations {
public static void updateCount(TreapNode root) {
root.count = 1 + getCount(root.left) + getCount(root.right);
}
public static int getCount(TreapNode root) {
return root == null ? 0 : root.count;
}
public static TreapPair split(TreapNode root, int minRight){
if(root == null)return new TreapPair(null, null);
if(root.key >= minRight) {
TreapPair leftSplit = split(root.left, minRight);
root.left = leftSplit.right;
updateCount(root);
leftSplit.right = root;
return leftSplit;
} else {
TreapPair rightSplit = split(root.right, minRight);
root.right = rightSplit.left;
updateCount(root);
rightSplit.left = root;
return rightSplit;
}
}
public static TreapNode merge(TreapNode left, TreapNode right){
if(left == null)
return right;
if(right == null)
return left;
if(left.prio > right.prio){
left.right = merge(left.right, right);
updateCount(left);
return left;
}else{
right.left = merge(left, right.left);
updateCount(right);
return right;
}
}
public static TreapNode insert(TreapNode root, int x){
TreapPair t = split(root, x);
return merge(merge(t.left, new TreapNode(x)), t.right);
}
public static TreapNode remove(TreapNode root, int x){
if(root == null){
return null;
}
if(x < root.key){
root.left = remove(root.left, x);
updateCount(root);
return root;
}else if(x > root.key){
root.right = remove(root.right, x);
updateCount(root);
return root;
}else{
return merge(root.left, root.right);
}
}
public static int kth(TreapNode root, int k){
if(k < getCount(root.left))
return kth(root.left, k);
else if(k > getCount(root.left))
return kth(root.right, k - getCount(root.left) - 1);
return root.key;
}
public static void printTree(TreapNode root){
if(root == null)return;
printTree(root.left);
System.out.println(root.key);
printTree(root.right);
}
}
class TreapTest {
public static void main(String ...args){
TreapNode rootNode = null;
Random random = new Random();
Set<Integer> set = new TreeSet<Integer>();
for (int i = 0; i < 100000; i++) {
int x = random.nextInt(100000);
if (random.nextBoolean()) {
rootNode = TreapOperations.remove(rootNode, x);
set.remove(x);
} else if (!set.contains(x)) {
rootNode = TreapOperations.insert(rootNode, x);
set.add(x);
}
if (set.size() != TreapOperations.getCount(rootNode))
throw new RuntimeException();
}
TreapOperations.printTree(rootNode);
}
}