-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
394 lines (360 loc) · 10 KB
/
mod.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
use std::cmp;
use std::cmp::Ordering;
use std::mem;
#[derive(Default, Debug)]
pub struct AVLTree {
root: Option<Box<Node>>,
}
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
enum Children {
Empty,
Leaf,
Left,
Right,
Full,
}
#[derive(Debug)]
struct Node {
key: i32,
val: i32,
height: u32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl AVLTree {
/// Makes a new empty AVLTree.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use yaar::avl::AVLTree;
///
/// let mut map = AVLTree::new();
///
/// // entries can now be inserted into the empty map
/// map.insert(1, 1);
/// ```
pub fn new() -> AVLTree {
Default::default()
}
/// Returns the value corresponding to the key.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use yaar::avl::AVLTree;
///
/// let mut map = AVLTree::new();
/// map.insert(1, 1);
/// assert_eq!(map.get(1), Some(1));
/// assert_eq!(map.get(2), None);
/// ```
pub fn get(&self, key: i32) -> Option<i32> {
self.root.get(key)
}
/// Returns true if the map contains no elements.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use yaar::avl::AVLTree;
///
/// let mut a = AVLTree::new();
/// assert!(a.is_empty());
/// a.insert(1, 1);
/// assert!(!a.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.root.is_none()
}
/// Inserts a key-value pair into the map.
///
/// If the map did not have this key present, `None` is returned.
///
/// If the map did have this key present, the value is updated, and the old
/// value is returned.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use yaar::avl::AVLTree;
///
/// let mut map = AVLTree::new();
/// assert_eq!(map.insert(37, 1), None);
/// assert_eq!(map.is_empty(), false);
///
/// map.insert(37, 2);
/// assert_eq!(map.insert(37, 3), Some(2));
/// ```
pub fn insert(&mut self, key: i32, value: i32) -> Option<i32> {
self.root.insert(key, value)
}
/// Removes a key from the map, returning the value at the key if the key
/// was previously in the map.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use yaar::avl::AVLTree;
///
/// let mut map = AVLTree::new();
/// map.insert(1, 1);
/// assert_eq!(map.remove(1), Some(1));
/// assert_eq!(map.remove(1), None);
/// ```
pub fn remove(&mut self, key: i32) -> Option<i32> {
self.root.remove(key)
}
}
impl Node {
fn leaf(key: i32, val: i32) -> Node {
Node {
key: key,
val: val,
height: 1,
left: None,
right: None,
}
}
fn min(&self) -> (i32, i32) {
match self.left {
None => (self.key, self.val),
Some(ref left) => left.min(),
}
}
fn balance(&self) -> i32 {
let left = self.left.height() as i32;
let right = self.right.height() as i32;
left - right
}
fn update_height(&mut self) {
self.height = cmp::max(self.left.height(), self.right.height()) + 1;
}
fn insert_rebalance(&mut self, key: i32) {
let balance = self.balance();
if balance > 1 {
match key.cmp(&self.left.key()) {
Ordering::Less => self.right_rotate(),
Ordering::Equal => panic!("key {} appears more than once", key),
Ordering::Greater => self.left_right_rotate(),
}
} else if balance < -1 {
match key.cmp(&self.right.key()) {
Ordering::Less => self.right_left_rotate(),
Ordering::Equal => panic!("key {} appears more than once", key),
Ordering::Greater => self.left_rotate(),
}
}
}
fn delete_rebalance(&mut self) {
let balance = self.balance();
if balance > 1 {
if self.left.balance() >= 0 {
self.right_rotate();
} else {
self.left_right_rotate();
}
} else if balance < -1 {
if self.right.balance() <= 0 {
self.left_rotate();
} else {
self.right_left_rotate();
}
}
}
fn left_rotate(&mut self) {
let mut child = self.right.take().unwrap();
mem::swap(self, &mut child);
mem::swap(&mut child.right, &mut self.left);
child.update_height();
self.left = Some(child);
self.update_height();
}
fn right_rotate(&mut self) {
let mut child = self.left.take().unwrap();
mem::swap(self, &mut child);
mem::swap(&mut child.left, &mut self.right);
child.update_height();
self.right = Some(child);
self.update_height();
}
fn left_right_rotate(&mut self) {
self.left.mutate().left_rotate();
self.right_rotate();
}
fn right_left_rotate(&mut self) {
self.right.mutate().right_rotate();
self.left_rotate();
}
}
trait OptionBoxNode {
fn get(&self, key: i32) -> Option<i32>;
fn height(&self) -> u32;
fn balance(&self) -> i32;
fn insert(&mut self, key: i32, val: i32) -> Option<i32>;
fn remove(&mut self, key: i32) -> Option<i32>;
fn reference(&self) -> &Box<Node>;
fn mutate(&mut self) -> &mut Box<Node>;
fn key(&self) -> i32;
fn children(&self) -> Children;
}
impl OptionBoxNode for Option<Box<Node>> {
fn get(&self, key: i32) -> Option<i32> {
match *self {
None => None,
Some(ref node) => {
match key.cmp(&node.key) {
Ordering::Equal => Some(node.val),
Ordering::Less => node.left.get(key),
Ordering::Greater => node.right.get(key),
}
}
}
}
fn height(&self) -> u32 {
match *self {
None => 0,
Some(ref node) => node.height,
}
}
fn balance(&self) -> i32 {
match *self {
None => 0,
Some(ref node) => node.balance(),
}
}
fn insert(&mut self, key: i32, value: i32) -> Option<i32> {
let prev = match *self {
None => {
*self = new_leaf(key, value);
None
}
Some(ref mut node) => {
match key.cmp(&node.key) {
Ordering::Equal => {
let prev = node.val;
node.val = value;
Some(prev)
}
Ordering::Less => node.left.insert(key, value),
Ordering::Greater => node.right.insert(key, value),
}
}
};
let node = self.mutate();
node.update_height();
node.insert_rebalance(key);
prev
}
fn remove(&mut self, key: i32) -> Option<i32> {
if self.is_none() {
return None;
}
let cmp = key.cmp(&self.key());
let children = self.children();
let prev = match cmp {
Ordering::Equal => {
let prev = self.reference().val;
match children {
Children::Empty => panic!("unexpected missing node"),
Children::Leaf => {
self.take();
}
Children::Left => {
*self = self.mutate().left.take();
}
Children::Right => {
*self = self.mutate().right.take();
}
Children::Full => {
let node = self.mutate();
let succ = node.right.reference().min();
node.key = succ.0;
node.val = succ.1;
node.right.remove(succ.0);
}
};
Some(prev)
}
Ordering::Less => self.mutate().left.remove(key),
Ordering::Greater => self.mutate().right.remove(key),
};
if self.is_some() {
let node = self.mutate();
node.update_height();
node.delete_rebalance();
}
prev
}
fn key(&self) -> i32 {
self.as_ref().unwrap().key
}
fn reference(&self) -> &Box<Node> {
self.as_ref().unwrap()
}
fn mutate(&mut self) -> &mut Box<Node> {
self.as_mut().unwrap()
}
fn children(&self) -> Children {
match *self {
None => Children::Empty,
Some(ref node) => {
if node.left.is_none() && node.right.is_none() {
Children::Leaf
} else if node.right.is_none() {
Children::Left
} else if node.left.is_none() {
Children::Right
} else {
Children::Full
}
}
}
}
}
fn new_leaf(key: i32, val: i32) -> Option<Box<Node>> {
Some(Box::new(Node::leaf(key, val)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basic_construction() {
let mut tree = AVLTree::new();
assert_eq!(tree.get(0), None);
assert_eq!(tree.insert(0, 1), None);
assert_eq!(tree.insert(0, 2), Some(1));
assert_eq!(tree.get(0), Some(2));
}
#[test]
fn insert_sequence() {
let mut tree = AVLTree::new();
for i in 0..256 {
assert_eq!(tree.insert(i, i), None);
}
for i in 0..256 {
assert_eq!(tree.get(i), Some(i));
}
}
#[test]
fn remove() {
let mut tree = AVLTree::new();
for i in 0..256 {
assert_eq!(tree.insert(i, i), None);
}
for i in 0..256 {
assert_eq!(tree.remove(i), Some(i));
assert_eq!(tree.remove(i), None);
}
}
}