Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c79dfac

Browse files
committedApr 4, 2018
Integrate codecov.
1 parent 7655acc commit c79dfac

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
 

‎src/data-structures/heap/MinHeap.js

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ export default class MinHeap {
126126
return 0;
127127
}
128128

129+
// Min heap may be converted to max heap by simply changing this line to:
130+
// a > b ? -1 : 1
129131
return a < b ? -1 : 1;
130132
}
131133

‎src/data-structures/heap/__test__/MinHeap.test.js

+29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ describe('MinHeap', () => {
2626
minHeap.add(1);
2727
expect(minHeap.peek()).toBe(1);
2828
expect(minHeap.toString()).toBe('1,3,10,5');
29+
30+
minHeap.add(1);
31+
expect(minHeap.peek()).toBe(1);
32+
expect(minHeap.toString()).toBe('1,1,10,5,3');
33+
34+
expect(minHeap.poll()).toBe(1);
35+
expect(minHeap.toString()).toBe('1,3,10,5');
36+
37+
expect(minHeap.poll()).toBe(1);
38+
expect(minHeap.toString()).toBe('3,5,10');
39+
40+
expect(minHeap.poll()).toBe(3);
41+
expect(minHeap.toString()).toBe('5,10');
2942
});
3043

3144
it('should poll items from the heap and heapify it down', () => {
@@ -57,4 +70,20 @@ describe('MinHeap', () => {
5770
expect(minHeap.poll()).toBeNull();
5871
expect(minHeap.toString()).toBe('');
5972
});
73+
74+
it('should heapify down through the right branch as well', () => {
75+
const minHeap = new MinHeap();
76+
77+
minHeap.add(3);
78+
minHeap.add(12);
79+
minHeap.add(10);
80+
81+
expect(minHeap.toString()).toBe('3,12,10');
82+
83+
minHeap.add(11);
84+
expect(minHeap.toString()).toBe('3,11,10,12');
85+
86+
expect(minHeap.poll()).toBe(3);
87+
expect(minHeap.toString()).toBe('10,11,12');
88+
});
6089
});

0 commit comments

Comments
 (0)
Please sign in to comment.