Skip to content

Commit d74a291

Browse files
committed
add algorithms
1 parent d0690f9 commit d74a291

File tree

1 file changed

+56
-20
lines changed

1 file changed

+56
-20
lines changed

exercises/algorithm/algorithm4.rs

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/*
2-
binary_search tree
3-
This problem requires you to implement a basic interface for a binary tree
2+
binary_search tree
3+
This problem requires you to implement a basic interface for a binary tree
44
*/
55

66
//I AM NOT DONE
77
use std::cmp::Ordering;
88
use std::fmt::Debug;
99

10-
1110
#[derive(Debug)]
1211
struct TreeNode<T>
1312
where
@@ -43,34 +42,80 @@ impl<T> BinarySearchTree<T>
4342
where
4443
T: Ord,
4544
{
46-
4745
fn new() -> Self {
4846
BinarySearchTree { root: None }
4947
}
5048

5149
// Insert a value into the BST
5250
fn insert(&mut self, value: T) {
53-
//TODO
51+
let current = &self.root;
52+
53+
if current.is_none() {
54+
self.root = Some(Box::new(TreeNode::new(value)));
55+
return;
56+
}
57+
58+
self.root.as_mut().unwrap().insert(value);
5459
}
5560

5661
// Search for a value in the BST
5762
fn search(&self, value: T) -> bool {
58-
//TODO
59-
true
63+
if self.root.is_none() {
64+
return false;
65+
}
66+
67+
return self.root.as_ref().unwrap().search(value);
6068
}
6169
}
6270

6371
impl<T> TreeNode<T>
6472
where
6573
T: Ord,
6674
{
75+
fn search(&self, value: T) -> bool {
76+
if self.value == value {
77+
return true;
78+
}
79+
80+
if self.value > value {
81+
if self.right.is_none() {
82+
return false;
83+
}
84+
85+
return self.right.as_ref().unwrap().search(value);
86+
}
87+
88+
if self.left.is_none() {
89+
return false;
90+
}
91+
92+
return self.left.as_ref().unwrap().search(value);
93+
}
94+
6795
// Insert a node into the tree
6896
fn insert(&mut self, value: T) {
69-
//TODO
97+
let current_val = &(self.value);
98+
99+
if value <= *current_val {
100+
if self.left.is_none() {
101+
self.left = Some(Box::new(TreeNode::new(value)));
102+
return;
103+
}
104+
105+
self.left.as_mut().unwrap().insert(value);
106+
} else {
107+
if self.right.is_none() {
108+
self.right = Some(Box::new(TreeNode::new(value)));
109+
return;
110+
}
111+
112+
self.right.as_mut().unwrap().insert(value);
113+
}
114+
115+
return;
70116
}
71117
}
72118

73-
74119
#[cfg(test)]
75120
mod tests {
76121
use super::*;
@@ -79,24 +124,20 @@ mod tests {
79124
fn test_insert_and_search() {
80125
let mut bst = BinarySearchTree::new();
81126

82-
83127
assert_eq!(bst.search(1), false);
84128

85-
86129
bst.insert(5);
87130
bst.insert(3);
88131
bst.insert(7);
89132
bst.insert(2);
90133
bst.insert(4);
91134

92-
93135
assert_eq!(bst.search(5), true);
94136
assert_eq!(bst.search(3), true);
95137
assert_eq!(bst.search(7), true);
96138
assert_eq!(bst.search(2), true);
97139
assert_eq!(bst.search(4), true);
98140

99-
100141
assert_eq!(bst.search(1), false);
101142
assert_eq!(bst.search(6), false);
102143
}
@@ -105,22 +146,17 @@ mod tests {
105146
fn test_insert_duplicate() {
106147
let mut bst = BinarySearchTree::new();
107148

108-
109149
bst.insert(1);
110150
bst.insert(1);
111151

112-
113152
assert_eq!(bst.search(1), true);
114153

115-
116154
match bst.root {
117155
Some(ref node) => {
118156
assert!(node.left.is_none());
119157
assert!(node.right.is_none());
120-
},
158+
}
121159
None => panic!("Root should not be None after insertion"),
122160
}
123161
}
124-
}
125-
126-
162+
}

0 commit comments

Comments
 (0)