File tree Expand file tree Collapse file tree 3 files changed +8
-32
lines changed
Expand file tree Collapse file tree 3 files changed +8
-32
lines changed Original file line number Diff line number Diff line change @@ -88,12 +88,12 @@ where
8888 T : Default + Ord ,
8989{
9090 /// Create a new MinHeap
91- pub fn new_min ( ) -> Self {
91+ pub fn new_min ( ) -> Heap < T > {
9292 Self :: new ( |a, b| a < b)
9393 }
9494
9595 /// Create a new MaxHeap
96- pub fn new_max ( ) -> Self {
96+ pub fn new_max ( ) -> Heap < T > {
9797 Self :: new ( |a, b| a > b)
9898 }
9999}
@@ -130,42 +130,18 @@ where
130130 }
131131}
132132
133- pub struct MinHeap ;
134-
135- impl MinHeap {
136- #[ allow( clippy:: new_ret_no_self) ]
137- pub fn new < T > ( ) -> Heap < T >
138- where
139- T : Default + Ord ,
140- {
141- Heap :: new ( |a, b| a < b)
142- }
143- }
144-
145- pub struct MaxHeap ;
146-
147- impl MaxHeap {
148- #[ allow( clippy:: new_ret_no_self) ]
149- pub fn new < T > ( ) -> Heap < T >
150- where
151- T : Default + Ord ,
152- {
153- Heap :: new ( |a, b| a > b)
154- }
155- }
156-
157133#[ cfg( test) ]
158134mod tests {
159135 use super :: * ;
160136 #[ test]
161137 fn test_empty_heap ( ) {
162- let mut heap = MaxHeap :: new :: < i32 > ( ) ;
138+ let mut heap: Heap < i32 > = Heap :: new_max ( ) ;
163139 assert_eq ! ( heap. next( ) , None ) ;
164140 }
165141
166142 #[ test]
167143 fn test_min_heap ( ) {
168- let mut heap = MinHeap :: new ( ) ;
144+ let mut heap = Heap :: new_min ( ) ;
169145 heap. add ( 4 ) ;
170146 heap. add ( 2 ) ;
171147 heap. add ( 9 ) ;
@@ -180,7 +156,7 @@ mod tests {
180156
181157 #[ test]
182158 fn test_max_heap ( ) {
183- let mut heap = MaxHeap :: new ( ) ;
159+ let mut heap = Heap :: new_max ( ) ;
184160 heap. add ( 4 ) ;
185161 heap. add ( 2 ) ;
186162 heap. add ( 9 ) ;
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ pub use self::binary_search_tree::BinarySearchTree;
1818pub use self :: fenwick_tree:: FenwickTree ;
1919pub use self :: graph:: DirectedGraph ;
2020pub use self :: graph:: UndirectedGraph ;
21- pub use self :: heap:: { Heap , MaxHeap , MinHeap } ;
21+ pub use self :: heap:: Heap ;
2222pub use self :: linked_list:: LinkedList ;
2323pub use self :: queue:: Queue ;
2424pub use self :: rb_tree:: RBTree ;
Original file line number Diff line number Diff line change 1- use crate :: data_structures:: MaxHeap ;
1+ use crate :: data_structures:: Heap ;
22use std:: cmp:: { Ord , Ordering } ;
33
44/// Returns k-th smallest element of an array.
2828 // than it
2929 // otherwise, E_large cannot be the kth smallest, and should
3030 // be removed from the heap and E_new should be added
31- let mut heap = MaxHeap :: new ( ) ;
31+ let mut heap = Heap :: new_max ( ) ;
3232
3333 // first k elements goes to the heap as the baseline
3434 for & val in input. iter ( ) . take ( k) {
You can’t perform that action at this time.
0 commit comments