-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathtest_rb_tree2.rs
153 lines (135 loc) · 3.44 KB
/
test_rb_tree2.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
use algo::tree::binary::rb2::RedBlackTreeV2;
use algo::tree::binary::traverse::{InOrderVisitor, PreOrderVisitor};
use algo::tree::binary::Tree;
#[test]
fn height() {
let mut tree = Tree::default();
for v in 0..100 {
tree.insert(v, v);
}
// 即使输入升序数列,rb tree仍然是平衡的
// 证明:含有n个节点的红黑树,其高度不会超过 2 * lg(n + 1)
assert!(tree.height() as f32 <= 2.0 * 100.0f32.log2())
}
/// Algorithms 4th Edition by Robert Sedgewick, Kevin Wayne
/// P440
#[test]
fn insert1() {
let mut tree = Tree::default();
for v in "SEARCHXMPL".chars() {
tree.insert(v, v);
}
let r = unsafe { PreOrderVisitor::iterate(&tree) };
assert_eq!(r.iter().collect::<String>(), "MECALHRPXS");
let r = unsafe { InOrderVisitor::iterate(&tree) };
assert_eq!(r.iter().collect::<String>(), "ACEHLMPRSX");
}
#[test]
fn insert2() {
let mut tree = Tree::default();
for v in "ACEHLMPRSX".chars() {
tree.insert(v, v);
}
let r = unsafe { PreOrderVisitor::iterate(&tree) };
assert_eq!(r.iter().collect::<String>(), "HCAERMLPXS");
let r = unsafe { InOrderVisitor::iterate(&tree) };
assert_eq!(r.iter().collect::<String>(), "ACEHLMPRSX");
}
#[test]
fn repeat_insert() {
let mut tree = Tree::default();
for v in "ACEHLMPRSX".chars() {
tree.insert(v, v);
}
//重复insert 'A'
tree.insert('A', 'A');
let r = unsafe { PreOrderVisitor::iterate(&tree) };
assert_eq!(r.iter().collect::<String>(), "HCAERMLPXS");
}
#[test]
fn delete_min() {
let mut tree = Tree::default();
for v in 0..10 {
tree.insert(v, v);
}
for v in 0..10 {
assert!(tree.contains(&v));
tree.delete_min();
assert!(!tree.contains(&v));
}
}
#[test]
fn delete_max() {
let mut tree = Tree::default();
for v in 0..10 {
tree.insert(v, v);
}
for v in (0..10).rev() {
assert!(tree.contains(&v));
tree.delete_max();
assert!(!tree.contains(&v));
}
}
#[test]
fn delete() {
let mut tree = Tree::default();
for v in 0..10 {
tree.insert(v, v);
}
for v in (0..10).rev() {
assert!(tree.contains(&v));
tree.delete(&v);
assert!(!tree.contains(&v));
}
}
#[test]
fn min_max() {
let mut tree = Tree::default();
for v in 0..10 {
tree.insert(v, v);
}
assert_eq!(tree.min(), Some(&0));
assert_eq!(tree.max(), Some(&9));
}
#[test]
fn keys() {
let mut tree = Tree::default();
for v in 0..10 {
tree.insert(v, v);
}
assert_eq!(tree.keys(), vec![&0, &1, &2, &3, &4, &5, &6, &7, &8, &9]);
// 倒序加入
let mut tree = Tree::default();
for v in (0..10).rev() {
tree.insert(v, v);
}
assert_eq!(tree.keys(), vec![&0, &1, &2, &3, &4, &5, &6, &7, &8, &9]);
}
#[test]
fn drop_clear() {
use algo::common::drop::{self, Elem};
drop::with(|ctx| {
let mut tree = Tree::default();
for v in 0..100 {
tree.insert(v, Elem);
}
drop(tree);
assert_eq!(100, ctx.get());
});
}
#[test]
fn drop_with_delete() {
use algo::common::drop::{self, Elem};
drop::with(|ctx| {
let mut tree = Tree::default();
for v in 0..100 {
tree.insert(v, Elem);
}
for v in 0..10 {
tree.delete(&v);
}
assert_eq!(10, ctx.get());
drop(tree);
assert_eq!(100, ctx.get());
});
}