-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_tree.rs
315 lines (271 loc) · 10.2 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
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
// https://www.programiz.com/dsa/binary-search-tree
use crate::binary_tree::*;
use std::{collections::{HashSet, VecDeque}, rc::Rc};
pub struct BinarySearchTree {
pub tree: BinaryTree,
}
impl BinarySearchTree {
pub fn with_root(root: BinaryTreeNodeRef) -> Self {
BinarySearchTree {
tree: BinaryTree::with_root(root),
}
}
pub fn insert_recursion(node: Option<BinaryTreeNodeRef>,
new_node_ref: &BinaryTreeNodeRef,
) -> Option<BinaryTreeNodeRef> {
match node {
None => Some(new_node_ref.clone()),
Some(node_ref) => {
let mut parent = None;
if node_ref.borrow().data > new_node_ref.borrow().data {
let left = node_ref.borrow().left.clone();
if left.is_none() {
parent = Some(Rc::downgrade(&node_ref));
}
node_ref.borrow_mut().left = Self::insert_recursion(left, new_node_ref);
} else {
let right = node_ref.borrow().right.clone();
if right.is_none() {
parent = Some(Rc::downgrade(&node_ref));
}
node_ref.borrow_mut().right = Self::insert_recursion(right, new_node_ref);
}
if let Some(p) = parent {
new_node_ref.borrow_mut().parent = p;
}
Some(node_ref)
}
}
}
pub fn insert_iterative(root: Option<BinaryTreeNodeRef>, new_node: BinaryTreeNodeRef) -> Option<BinaryTreeNodeRef> {
let mut start = root.clone();
let mut insert_node = None;
while start.is_some() {
insert_node = start.clone();
let node = insert_node.as_ref().unwrap().borrow();
if new_node.borrow().data < node.data {
start = node.left.clone();
}
else {
start = node.right.clone();
}
}
if insert_node.is_none() {
insert_node = Some(new_node.clone());
}
else {
let node_ref = insert_node.as_ref().unwrap();
new_node.borrow_mut().parent = Rc::downgrade(node_ref);
let mut node = node_ref.borrow_mut();
if new_node.borrow().data < node.data {
node.left = Some(new_node.clone());
}
else {
node.right = Some(new_node.clone());
}
}
insert_node
}
pub fn is_binary_search_tree(node: &BinaryTreeNodeRef) -> bool {
BinarySearchTree::is_binary_search_tree_v3(node)
}
pub fn search(&self, data: u32) -> Option<BinaryTreeNodeRef> {
self.tree.root.as_ref()?;
let node = self.tree.root.as_ref().unwrap();
let mut queue = VecDeque::new();
queue.push_back(node.clone());
while let Some(node) = queue.pop_front() {
let n = node.borrow();
let node_data = n.data;
if data == node_data {
return Some(node.clone());
}
if data < node_data {
if let Some(left) = n.left.as_ref() {
queue.push_back(left.clone());
}
} else if let Some(right) = n.right.as_ref() {
queue.push_back(right.clone());
}
}
None
}
pub fn is_binary_search_tree_v3(node: &BinaryTreeNodeRef) -> bool {
// this is an optimized version of v2
let mut visited = None;
let mut root = Some(node.clone());
let mut leftdone = false;
while let Some(root_ref) = root.as_ref() {
let mut current_ref = root_ref.clone();
if !leftdone {
if let Some(leftmost) = BinaryTree::leftmost(¤t_ref) {
current_ref = leftmost.clone();
}
}
if let Some(node) = visited {
if current_ref <= node {
return false;
}
}
visited = Some(current_ref.clone());
root = Some(current_ref.clone());
leftdone = true;
let root_node = current_ref.borrow();
if let Some(right) = root_node.right.as_ref() {
leftdone = false;
root = Some(right.clone());
} else if let Some(parent) = root_node.parent.upgrade() {
let mut root_parent = Some(parent.clone());
let mut parent_right = parent.clone().borrow().right.clone();
while root_parent.is_some() {
if !BinaryTree::is_same(&root, &parent_right) {
break;
}
root = root_parent;
root_parent = if root.is_some() {
root.clone().unwrap().borrow().parent.upgrade()
} else {
None
};
parent_right = if root_parent.is_some() {
root_parent.clone().unwrap().borrow().right.clone()
} else {
None
};
}
root = root_parent;
} else {
break;
}
}
true
}
pub fn is_binary_search_tree_v2(node: &BinaryTreeNodeRef) -> bool {
let flatten = BinaryTree::flatten_inorder(node.clone());
let values: Vec<_> = flatten.iter().map(|n| n.borrow().data).collect();
values.windows(2).all(|v| v[0] < v[1])
}
pub fn is_binary_search_tree_v1(node: &BinaryTreeNodeRef) -> bool {
let mut keys = HashSet::<u32>::new();
let mut queue = VecDeque::new();
queue.push_back(node.clone());
while let Some(node) = queue.pop_front() {
let n = node.borrow();
let v = n.data;
if keys.contains(&v) {
return false; // (4)
}
keys.insert(v);
if let Some(left) = n.left.as_ref() {
let subtree = BinaryTree::flatten_top_down(left.clone());
let values = subtree.iter().map(|r| r.borrow().data).collect::<Vec<_>>();
for data in values {
if data >= v {
return false; // (1)
}
}
queue.push_back(left.clone()); // (3)
}
if let Some(right) = n.right.as_ref() {
let subtree = BinaryTree::flatten_top_down(right.clone());
let values = subtree.iter().map(|r| r.borrow().data).collect::<Vec<_>>();
for data in values {
if data <= v {
return false; // (2)
}
}
queue.push_back(right.clone()); // (3)
}
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::binary_tree::utils::*;
#[test]
fn insert_recursion() {
let list= populate_node_list();
let mut root = None;
for node_ref in &list {
root = BinarySearchTree::insert_recursion(root, node_ref);
}
assert!(root.is_some());
let root_node = root.unwrap();
assert_eq!(BinaryTree::count(&root_node), list.len());
assert!(BinarySearchTree::is_binary_search_tree_v1(&root_node));
assert!(BinarySearchTree::is_binary_search_tree_v2(&root_node));
assert!(BinarySearchTree::is_binary_search_tree_v3(&root_node));
assert!(BinarySearchTree::is_binary_search_tree(&root_node));
}
#[test]
fn insert_iterative() {
let list= populate_node_list();
let mut node = None;
for node_ref in &list {
node = BinarySearchTree::insert_iterative(node, node_ref.clone());
}
let root = BinaryTree::get_root(&node.unwrap());
assert_eq!(BinaryTree::count(&root), list.len());
assert!(BinarySearchTree::is_binary_search_tree_v1(&root));
assert!(BinarySearchTree::is_binary_search_tree_v2(&root));
assert!(BinarySearchTree::is_binary_search_tree_v3(&root));
assert!(BinarySearchTree::is_binary_search_tree(&root));
}
#[test]
fn is_binary_search_tree_v1() {
let root = populate_balanced_binary_search_tree();
assert!(BinarySearchTree::is_binary_search_tree_v1(&root));
}
#[test]
fn non_binary_search_tree_v1() {
let root = populate_balanced_binary_tree();
assert!(!BinarySearchTree::is_binary_search_tree_v1(&root));
}
#[test]
fn is_binary_search_tree_v2() {
let root = populate_balanced_binary_search_tree();
assert!(BinarySearchTree::is_binary_search_tree_v2(&root));
}
#[test]
fn non_binary_search_tree_v2() {
let root = populate_balanced_binary_tree();
assert!(!BinarySearchTree::is_binary_search_tree_v2(&root));
}
#[test]
fn is_binary_search_tree_v3() {
let root = populate_balanced_binary_search_tree();
assert!(BinarySearchTree::is_binary_search_tree_v3(&root));
}
#[test]
fn non_binary_search_tree_v3() {
let root = populate_balanced_binary_tree();
assert!(!BinarySearchTree::is_binary_search_tree_v3(&root));
}
#[test]
fn is_binary_search_tree() {
let root = populate_balanced_binary_search_tree();
assert!(BinarySearchTree::is_binary_search_tree(&root));
}
#[test]
fn non_binary_search_tree() {
let root = populate_balanced_binary_tree();
assert!(!BinarySearchTree::is_binary_search_tree(&root));
}
#[test]
fn search() {
let root = populate_balanced_binary_search_tree();
let bst = BinarySearchTree::with_root(root.clone());
let flatten = BinaryTree::flatten_inorder(root);
let values: Vec<_> = flatten.iter().map(|n| n.borrow().data).collect();
let not_exist = 1 + *values.last().unwrap();
for v in values {
let node = bst.search(v);
assert!(node.is_some());
assert_eq!(node.unwrap().borrow().data, v);
}
let node = bst.search(not_exist);
assert!(node.is_none());
}
}