File tree Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Original file line number Diff line number Diff line change @@ -105,15 +105,13 @@ where
105105 type Item = T ;
106106
107107 fn next ( & mut self ) -> Option < T > {
108- let next = if self . count == 0 {
109- None
110- } else {
111- // This feels like a function built for heap impl :)
112- // Removes an item at an index and fills in with the last item
113- // of the Vec
114- let next = self . items . swap_remove ( 1 ) ;
115- Some ( next)
116- } ;
108+ if self . count == 0 {
109+ return None ;
110+ }
111+ // This feels like a function built for heap impl :)
112+ // Removes an item at an index and fills in with the last item
113+ // of the Vec
114+ let next = Some ( self . items . swap_remove ( 1 ) ) ;
117115 self . count -= 1 ;
118116
119117 if self . count > 0 {
@@ -159,6 +157,11 @@ impl MaxHeap {
159157#[ cfg( test) ]
160158mod tests {
161159 use super :: * ;
160+ #[ test]
161+ fn test_empty_heap ( ) {
162+ let mut heap = MaxHeap :: new :: < i32 > ( ) ;
163+ assert_eq ! ( heap. next( ) , None ) ;
164+ }
162165
163166 #[ test]
164167 fn test_min_heap ( ) {
You can’t perform that action at this time.
0 commit comments