-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Treap.java
214 lines (186 loc) · 7.39 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
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
package com.jwetherell.algorithms.data_structures;
import java.util.ArrayList;
import java.util.List;
/**
* The treap and the randomized binary search tree are two closely related forms
* of binary search tree data structures that maintain a dynamic set of ordered
* values and allow binary searches among the values. After any sequence of
* insertions and deletions of values, the shape of the tree is a random
* variable with the same probability distribution as a random binary tree; in
* particular, with high probability its height is proportional to the logarithm
* of the number of values, so that each search, insertion, or deletion
* operation takes logarithmic time to perform.
* <p>
* @see <a href="https://en.wikipedia.org/wiki/Treap">Treap (Wikipedia)</a>
* <br>
* @author Justin Wetherell <[email protected]>
*/
public class Treap<T extends Comparable<T>> extends BinarySearchTree<T> {
private static int randomSeed = Integer.MAX_VALUE; // This should be at least twice the number of Nodes
/**
* Default constructor.
*/
public Treap() {
this.creator = new BinarySearchTree.INodeCreator<T>() {
/**
* {@inheritDoc}
*/
@Override
public BinarySearchTree.Node<T> createNewNode(BinarySearchTree.Node<T> parent, T id) {
return (new TreapNode<T>(parent, id));
}
};
}
/**
* Constructor with a random seed.
*
* @param randomSeed
* to use as a random seed.
*/
public Treap(int randomSeed) {
this();
Treap.randomSeed = randomSeed;
}
/**
* Constructor with external Node creator.
*/
public Treap(INodeCreator<T> creator) {
super(creator);
}
/**
* Constructor with external Node creator.
*/
public Treap(INodeCreator<T> creator, int randomSeed) {
this(randomSeed);
this.creator = creator;
}
/**
* {@inheritDoc}
*/
@Override
protected Node<T> addValue(T id) {
Node<T> nodeToReturn = super.addValue(id);
TreapNode<T> nodeToAdd = (TreapNode<T>) nodeToReturn;
heapify(nodeToAdd);
return nodeToReturn;
}
/**
* Heapify up the treap at the current node to the root.
*
* @param current
* to heapify.
*/
private void heapify(TreapNode<T> current) {
// Bubble up the heap, if needed
TreapNode<T> parent = (TreapNode<T>) current.parent;
while (parent != null && current.priority > parent.priority) {
Node<T> grandParent = parent.parent;
if (grandParent != null) {
if (grandParent.greater != null && grandParent.greater == parent) {
// My parent is my grandparents greater branch
grandParent.greater = current;
current.parent = grandParent;
} else if (grandParent.lesser != null && grandParent.lesser == parent) {
// My parent is my grandparents lesser branch
grandParent.lesser = current;
current.parent = grandParent;
} else {
throw new RuntimeException("YIKES! Grandparent should have at least one non-NULL child which should be my parent.");
}
current.parent = grandParent;
} else {
root = current;
root.parent = null;
}
if (parent.lesser != null && parent.lesser == current) {
// LEFT
parent.lesser = null;
if (current.greater == null) {
current.greater = parent;
parent.parent = current;
} else {
Node<T> lost = current.greater;
current.greater = parent;
parent.parent = current;
parent.lesser = lost;
lost.parent = parent;
}
} else if (parent.greater != null && parent.greater == current) {
// RIGHT
parent.greater = null;
if (current.lesser == null) {
current.lesser = parent;
parent.parent = current;
} else {
Node<T> lost = current.lesser;
current.lesser = parent;
parent.parent = current;
parent.greater = lost;
lost.parent = parent;
}
} else {
// We really shouldn't get here
throw new RuntimeException("YIKES! Parent should have at least one non-NULL child which should be me.");
}
parent = (TreapNode<T>) current.parent;
}
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return TreapPrinter.getString(this);
}
private static class TreapNode<T extends Comparable<T>> extends Node<T> {
private int priority = Integer.MIN_VALUE;
private TreapNode(Node<T> parent, int priority, T value) {
super(parent, value);
this.priority = priority;
}
private TreapNode(Node<T> parent, T value) {
this(parent, RANDOM.nextInt(randomSeed), value);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("priorty=").append(priority).append(" value=").append(id);
if (parent != null) builder.append(" parent=").append(parent.id);
builder.append("\n");
if (lesser != null) builder.append("left=").append(lesser.toString()).append("\n");
if (greater != null) builder.append("right=").append(greater.toString()).append("\n");
return builder.toString();
}
}
protected static class TreapPrinter {
public static <T extends Comparable<T>> String getString(Treap<T> tree) {
if (tree.root == null)
return "Tree has no nodes.";
return getString((TreapNode<T>) tree.root, "", true);
}
private static <T extends Comparable<T>> String getString(TreapNode<T> node, String prefix, boolean isTail) {
StringBuilder builder = new StringBuilder();
builder.append(prefix + (isTail ? "└── " : "├── ") + "(" + node.priority + ") " + node.id + "\n");
List<Node<T>> children = null;
if (node.lesser != null || node.greater != null) {
children = new ArrayList<Node<T>>(2);
if (node.lesser != null) children.add(node.lesser);
if (node.greater != null) children.add(node.greater);
}
if (children != null) {
for (int i = 0; i < children.size() - 1; i++) {
TreapNode<T> treapNode = (TreapNode<T>) children.get(i);
builder.append(getString(treapNode, prefix + (isTail ? " " : "│ "), false));
}
if (children.size() >= 1) {
TreapNode<T> treapNode = (TreapNode<T>) children.get(children.size() - 1);
builder.append(getString(treapNode, prefix + (isTail ? " " : "│ "), true));
}
}
return builder.toString();
}
}
}