-
Notifications
You must be signed in to change notification settings - Fork 3
/
binary_search_tree.rs
142 lines (122 loc) · 3.23 KB
/
binary_search_tree.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
//! 二叉搜索树
use std::cmp::Ordering;
/// 二叉搜索树节点
pub struct BinarySearchNode<T> {
pub value: (T, usize),
pub left: Option<Box<BinarySearchNode<T>>>,
pub right: Option<Box<BinarySearchNode<T>>>,
}
/// 二叉搜索树
pub struct BinarySearchTree<T> {
pub root: Option<BinarySearchNode<T>>,
}
impl<T: Ord> Default for BinarySearchTree<T> {
fn default() -> Self {
Self::new()
}
}
impl<T: Ord> BinarySearchTree<T> {
pub fn new() -> Self {
BinarySearchTree { root: None }
}
/// 插入元素
pub fn insert(&mut self, value: T) {
match &mut self.root {
None => self.root = Some(BinarySearchNode::new(value)),
Some(node) => node.insert(value),
}
}
/// 查找元素是否存在
pub fn find(&self, value: &T) -> bool {
self.root
.as_ref()
.map(|node| node.find(value))
.unwrap_or_default()
}
/// 树中最大元素
pub fn max(&self) -> Option<&T> {
self.root.as_ref().map(|node| node.max())
}
/// 树中最小元素
pub fn min(&self) -> Option<&T> {
self.root.as_ref().map(|node| node.min())
}
}
impl<T: Ord> BinarySearchNode<T> {
pub fn new(value: T) -> Self {
Self {
value: (value, 1),
left: None,
right: None,
}
}
pub fn find(&self, v: &T) -> bool {
match self.value.0.cmp(v) {
Ordering::Equal => true,
Ordering::Less => self
.right
.as_ref()
.map(|node| node.find(v))
.unwrap_or_default(),
Ordering::Greater => self
.left
.as_ref()
.map(|node| node.find(v))
.unwrap_or_default(),
}
}
pub fn insert(&mut self, v: T) {
match self.value.0.cmp(&v) {
Ordering::Equal => {
self.value.1 += 1;
}
Ordering::Less => match &mut self.right {
None => {
self.right = Some(Box::new(Self::new(v)));
}
Some(node) => {
node.insert(v);
}
},
Ordering::Greater => match &mut self.left {
None => {
self.left = Some(Box::new(Self::new(v)));
}
Some(node) => {
node.insert(v);
}
},
}
}
pub fn min(&self) -> &T {
let mut node = self;
while let Some(child) = &node.left {
node = child.as_ref();
}
&node.value.0
}
pub fn max(&self) -> &T {
let mut node = self;
while let Some(child) = &node.right {
node = child.as_ref();
}
&node.value.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_binary_search_tree() {
let mut t = BinarySearchTree::new();
t.insert(1);
t.insert(9);
t.insert(2);
t.insert(8);
assert!(t.find(&2));
assert!(t.find(&9));
assert!(!t.find(&7));
assert_eq!(t.max(), Some(&9));
assert_eq!(t.min(), Some(&1));
}
}