forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treap.rs
352 lines (313 loc) · 9.4 KB
/
treap.rs
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use std::{
cmp::Ordering,
iter::FromIterator,
mem,
ops::Not,
time::{SystemTime, UNIX_EPOCH},
};
/// An internal node of an `Treap`.
struct TreapNode<T: Ord> {
value: T,
priority: usize,
left: Option<Box<TreapNode<T>>>,
right: Option<Box<TreapNode<T>>>,
}
/// A set based on a Treap (Randomized Binary Search Tree).
///
/// A Treap is a self-balancing binary search tree. It matains a priority value for each node, such
/// that for every node, its children will have lower priority than itself. So, by just looking at
/// the priority, it is like a heap, and this is where the name, Treap, comes from, Tree + Heap.
pub struct Treap<T: Ord> {
root: Option<Box<TreapNode<T>>>,
length: usize,
}
/// Refers to the left or right subtree of a `Treap`.
#[derive(Clone, Copy)]
enum Side {
Left,
Right,
}
impl<T: Ord> Treap<T> {
pub fn new() -> Treap<T> {
Treap {
root: None,
length: 0,
}
}
/// Returns `true` if the tree contains a value.
pub fn contains(&self, value: &T) -> bool {
let mut current = &self.root;
while let Some(node) = current {
current = match value.cmp(&node.value) {
Ordering::Equal => return true,
Ordering::Less => &node.left,
Ordering::Greater => &node.right,
}
}
false
}
/// Adds a value to the tree
///
/// Returns `true` if the tree did not yet contain the value.
pub fn insert(&mut self, value: T) -> bool {
let inserted = insert(&mut self.root, value);
if inserted {
self.length += 1;
}
inserted
}
/// Removes a value from the tree.
///
/// Returns `true` if the tree contained the value.
pub fn remove(&mut self, value: &T) -> bool {
let removed = remove(&mut self.root, value);
if removed {
self.length -= 1;
}
removed
}
/// Returns the number of values in the tree.
pub fn len(&self) -> usize {
self.length
}
/// Returns `true` if the tree contains no values.
pub fn is_empty(&self) -> bool {
self.length == 0
}
/// Returns an iterator that visits the nodes in the tree in order.
fn node_iter(&self) -> NodeIter<T> {
let mut node_iter = NodeIter { stack: Vec::new() };
// Initialize stack with path to leftmost child
let mut child = &self.root;
while let Some(node) = child {
node_iter.stack.push(node.as_ref());
child = &node.left;
}
node_iter
}
/// Returns an iterator that visits the values in the tree in ascending order.
pub fn iter(&self) -> Iter<T> {
Iter {
node_iter: self.node_iter(),
}
}
}
/// Generating random number, should use rand::Rng if possible.
fn rand() -> usize {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.subsec_nanos() as usize
}
/// Recursive helper function for `Treap` insertion.
fn insert<T: Ord>(tree: &mut Option<Box<TreapNode<T>>>, value: T) -> bool {
if let Some(node) = tree {
let inserted = match value.cmp(&node.value) {
Ordering::Equal => false,
Ordering::Less => insert(&mut node.left, value),
Ordering::Greater => insert(&mut node.right, value),
};
if inserted {
node.rebalance();
}
inserted
} else {
*tree = Some(Box::new(TreapNode {
value,
priority: rand(),
left: None,
right: None,
}));
true
}
}
/// Recursive helper function for `Treap` deletion
fn remove<T: Ord>(tree: &mut Option<Box<TreapNode<T>>>, value: &T) -> bool {
if let Some(node) = tree {
let removed = match value.cmp(&node.value) {
Ordering::Less => remove(&mut node.left, value),
Ordering::Greater => remove(&mut node.right, value),
Ordering::Equal => {
*tree = match (node.left.take(), node.right.take()) {
(None, None) => None,
(Some(b), None) | (None, Some(b)) => Some(b),
(Some(left), Some(right)) => {
let side = match left.priority.cmp(&right.priority) {
Ordering::Greater => Side::Right,
_ => Side::Left,
};
node.left = Some(left);
node.right = Some(right);
node.rotate(side);
remove(node.child_mut(side), value);
Some(tree.take().unwrap())
}
};
return true;
}
};
if removed {
node.rebalance();
}
removed
} else {
false
}
}
impl<T: Ord> TreapNode<T> {
/// Returns a reference to the left or right child.
fn child(&self, side: Side) -> &Option<Box<TreapNode<T>>> {
match side {
Side::Left => &self.left,
Side::Right => &self.right,
}
}
/// Returns a mutable reference to the left or right child.
fn child_mut(&mut self, side: Side) -> &mut Option<Box<TreapNode<T>>> {
match side {
Side::Left => &mut self.left,
Side::Right => &mut self.right,
}
}
/// Returns the priority of the left or right subtree.
fn priority(&self, side: Side) -> usize {
self.child(side).as_ref().map_or(0, |n| n.priority)
}
/// Performs a left or right rotation
fn rotate(&mut self, side: Side) {
if self.child_mut(!side).is_none() {
return;
}
let mut subtree = self.child_mut(!side).take().unwrap();
*self.child_mut(!side) = subtree.child_mut(side).take();
// Swap root and child nodes in memory
mem::swap(self, subtree.as_mut());
// Set old root (subtree) as child of new root (self)
*self.child_mut(side) = Some(subtree);
}
/// Performs left or right tree rotations to balance this node.
fn rebalance(&mut self) {
match (
self.priority,
self.priority(Side::Left),
self.priority(Side::Right),
) {
(v, p, q) if p >= q && p > v => self.rotate(Side::Right),
(v, p, q) if p < q && q > v => self.rotate(Side::Left),
_ => (),
};
}
#[cfg(test)]
fn is_valid(&self) -> bool {
self.priority >= self.priority(Side::Left) && self.priority >= self.priority(Side::Right)
}
}
impl<T: Ord> Default for Treap<T> {
fn default() -> Self {
Self::new()
}
}
impl Not for Side {
type Output = Side;
fn not(self) -> Self::Output {
match self {
Side::Left => Side::Right,
Side::Right => Side::Left,
}
}
}
impl<T: Ord> FromIterator<T> for Treap<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut tree = Treap::new();
for value in iter {
tree.insert(value);
}
tree
}
}
/// An iterator over the nodes of an `Treap`.
///
/// This struct is created by the `node_iter` method of `Treap`.
struct NodeIter<'a, T: Ord> {
stack: Vec<&'a TreapNode<T>>,
}
impl<'a, T: Ord> Iterator for NodeIter<'a, T> {
type Item = &'a TreapNode<T>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(node) = self.stack.pop() {
// Push left path of right subtree to stack
let mut child = &node.right;
while let Some(subtree) = child {
self.stack.push(subtree.as_ref());
child = &subtree.left;
}
Some(node)
} else {
None
}
}
}
/// An iterator over the items of an `Treap`.
///
/// This struct is created by the `iter` method of `Treap`.
pub struct Iter<'a, T: Ord> {
node_iter: NodeIter<'a, T>,
}
impl<'a, T: Ord> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<&'a T> {
match self.node_iter.next() {
Some(node) => Some(&node.value),
None => None,
}
}
}
#[cfg(test)]
mod tests {
use super::Treap;
/// Returns `true` if all nodes in the tree are valid.
fn is_valid<T: Ord>(tree: &Treap<T>) -> bool {
tree.node_iter().all(|n| n.is_valid())
}
#[test]
fn len() {
let tree: Treap<_> = (1..4).collect();
assert_eq!(tree.len(), 3);
}
#[test]
fn contains() {
let tree: Treap<_> = (1..4).collect();
assert!(tree.contains(&1));
assert!(!tree.contains(&4));
}
#[test]
fn insert() {
let mut tree = Treap::new();
// First insert succeeds
assert!(tree.insert(1));
// Second insert fails
assert!(!tree.insert(1));
}
#[test]
fn remove() {
let mut tree: Treap<_> = (1..8).collect();
// First remove succeeds
assert!(tree.remove(&4));
// Second remove fails
assert!(!tree.remove(&4));
}
#[test]
fn sorted() {
let tree: Treap<_> = (1..8).rev().collect();
assert!((1..8).eq(tree.iter().map(|&x| x)));
}
#[test]
fn valid() {
let mut tree: Treap<_> = (1..8).collect();
assert!(is_valid(&tree));
for x in 1..8 {
tree.remove(&x);
assert!(is_valid(&tree));
}
}
}