Skip to content

Commit 1af63a6

Browse files
committed
Return an Option, not a Result.
1 parent 83fe1f9 commit 1af63a6

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/lib.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ impl<'a, T> SearchTree<T>
6464
/// assert!(!tree.contains(&String::from("cruel")));
6565
/// ```
6666
pub fn contains(&self, value: &T) -> bool {
67-
self.get_node(value).is_ok()
67+
match self.get_node(value) {
68+
None => false,
69+
Some(index) => self.nodes[index].value == *value
70+
}
6871
}
6972

7073
/// Add the specified value to this tree.
@@ -97,11 +100,9 @@ impl<'a, T> SearchTree<T>
97100
}
98101
}
99102

100-
/// Return a `Result` containing the index of the node that has the
101-
/// specified value. If no such node exists, the `Result` contains
102-
/// the index of the node that would be the parent of the node containing
103-
/// `value`, if `value` were inserted into the tree.
104-
fn get_node(&self, value: &T) -> Result<usize, Option<usize>> {
103+
/// Return an `Option` containing the index of the node that has the
104+
/// lowest value that is greater or equal to `value`.
105+
fn get_node(&self, value: &T) -> Option<usize> {
105106
let mut current_index = self.root;
106107
let mut previous_index = None;
107108
while let Some(index) = current_index {
@@ -113,35 +114,36 @@ impl<'a, T> SearchTree<T>
113114
} else {
114115
// the value already exists in the tree - return the index of
115116
// the corresponding node
116-
return Ok(index);
117+
return current_index;
117118
}
118119
}
119120
// return the index of the node containing the smallest value
120121
// greater than `value`
121-
Err(previous_index)
122+
previous_index
122123
}
123124

125+
/// Return a new node containing `value`, or None if `value` is already in
126+
/// the tree.
124127
fn create_node(&mut self, value: T) -> Option<Node<T>> {
125-
match self.get_node(&value) {
126-
Ok(_) => None, // a node containing `value` already exists
127-
Err(parent) => {
128-
if let Some(parent_index) = parent {
129-
if self.nodes[parent_index].value < value {
130-
self.nodes[parent_index].right_child =
131-
Some(self.nodes.len());
132-
} else {
133-
self.nodes[parent_index].left_child = Some(self.nodes.len());
134-
}
128+
let parent = self.get_node(&value);
129+
if let Some(index) = parent {
130+
if self.nodes[index].value == value {
131+
return None; // no need to create the node
132+
} else {
133+
if self.nodes[index].value < value {
134+
self.nodes[index].right_child = Some(self.nodes.len());
135+
} else {
136+
self.nodes[index].left_child = Some(self.nodes.len());
135137
}
136-
Some(Node {
137-
value: value,
138-
parent: parent,
139-
left_child: None,
140-
right_child: None,
141-
height: 0
142-
})
143138
}
144139
}
140+
Some(Node {
141+
value: value,
142+
parent: parent,
143+
left_child: None,
144+
right_child: None,
145+
height: 0
146+
})
145147
}
146148

147149
fn update_heights(&mut self, start_index: usize) {

0 commit comments

Comments
 (0)