@@ -64,7 +64,10 @@ impl<'a, T> SearchTree<T>
64
64
/// assert!(!tree.contains(&String::from("cruel")));
65
65
/// ```
66
66
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
+ }
68
71
}
69
72
70
73
/// Add the specified value to this tree.
@@ -97,11 +100,9 @@ impl<'a, T> SearchTree<T>
97
100
}
98
101
}
99
102
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 > {
105
106
let mut current_index = self . root ;
106
107
let mut previous_index = None ;
107
108
while let Some ( index) = current_index {
@@ -113,35 +114,36 @@ impl<'a, T> SearchTree<T>
113
114
} else {
114
115
// the value already exists in the tree - return the index of
115
116
// the corresponding node
116
- return Ok ( index ) ;
117
+ return current_index ;
117
118
}
118
119
}
119
120
// return the index of the node containing the smallest value
120
121
// greater than `value`
121
- Err ( previous_index)
122
+ previous_index
122
123
}
123
124
125
+ /// Return a new node containing `value`, or None if `value` is already in
126
+ /// the tree.
124
127
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 ( ) ) ;
135
137
}
136
- Some ( Node {
137
- value : value,
138
- parent : parent,
139
- left_child : None ,
140
- right_child : None ,
141
- height : 0
142
- } )
143
138
}
144
139
}
140
+ Some ( Node {
141
+ value : value,
142
+ parent : parent,
143
+ left_child : None ,
144
+ right_child : None ,
145
+ height : 0
146
+ } )
145
147
}
146
148
147
149
fn update_heights ( & mut self , start_index : usize ) {
0 commit comments