@@ -79,7 +79,7 @@ impl<'a, T> SearchTree<T>
79
79
/// assert!(tree.contains(&String::from("world!")));
80
80
/// ```
81
81
pub fn insert ( & mut self , value : T ) {
82
- if let Ok ( node) = self . create_node ( value) {
82
+ if let Some ( node) = self . create_node ( value) {
83
83
self . nodes . push ( node) ;
84
84
// inserted the root
85
85
if self . nodes . len ( ) == 1 {
@@ -121,20 +121,21 @@ impl<'a, T> SearchTree<T>
121
121
Err ( previous_index)
122
122
}
123
123
124
- fn create_node ( & mut self , value : T ) -> Result < Node < T > , usize > {
124
+ fn create_node ( & mut self , value : T ) -> Option < Node < T > > {
125
125
match self . get_node ( & value) {
126
- Ok ( index) => Err ( index) ,
127
- Err ( previous_index) => {
128
- if let Some ( parent) = previous_index {
129
- if self . nodes [ parent] . value < value {
130
- self . nodes [ parent] . right_child = Some ( self . nodes . len ( ) ) ;
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 ( ) ) ;
131
132
} else {
132
- self . nodes [ parent ] . left_child = Some ( self . nodes . len ( ) ) ;
133
+ self . nodes [ parent_index ] . left_child = Some ( self . nodes . len ( ) ) ;
133
134
}
134
135
}
135
- Ok ( Node {
136
+ Some ( Node {
136
137
value : value,
137
- parent : previous_index ,
138
+ parent : parent ,
138
139
left_child : None ,
139
140
right_child : None ,
140
141
height : 0
0 commit comments