Skip to content

Commit b5fe7a3

Browse files
authored
simplify heap.rs and follow best practices (rust-lang#398)
1 parent a2d96e0 commit b5fe7a3

File tree

3 files changed

+8
-32
lines changed

3 files changed

+8
-32
lines changed

src/data_structures/heap.rs

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff 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)]
158134
mod 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);

src/data_structures/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use self::binary_search_tree::BinarySearchTree;
1818
pub use self::fenwick_tree::FenwickTree;
1919
pub use self::graph::DirectedGraph;
2020
pub use self::graph::UndirectedGraph;
21-
pub use self::heap::{Heap, MaxHeap, MinHeap};
21+
pub use self::heap::Heap;
2222
pub use self::linked_list::LinkedList;
2323
pub use self::queue::Queue;
2424
pub use self::rb_tree::RBTree;

src/searching/kth_smallest_heap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::data_structures::MaxHeap;
1+
use crate::data_structures::Heap;
22
use std::cmp::{Ord, Ordering};
33

44
/// Returns k-th smallest element of an array.
@@ -28,7 +28,7 @@ where
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) {

0 commit comments

Comments
 (0)