Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7b958e9

Browse files
committed
fix & update: handled case of min/max element 0, fixes in tests
1 parent 16006e0 commit 7b958e9

File tree

4 files changed

+71
-16
lines changed

4 files changed

+71
-16
lines changed

‎src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ describe('MaxHeap', () => {
1515
expect(mh instanceof MaxHeap).toEqual(true);
1616
});
1717

18+
it('Should create a MaxHeap using collection', () => {
19+
const mHBulk = new MaxHeap([1, 3, 21, 9, 101, 0]);
20+
expect(mHBulk.getMax()).toEqual(101);
21+
});
22+
1823
it('Should add an element to the MaxHeap', () => {
1924
mh.add(10);
2025
expect(mh.getMax()).toEqual(10);
@@ -51,12 +56,4 @@ describe('MaxHeap', () => {
5156
it('Should return `null` on `remove() called on empty heap`', () => {
5257
expect(mh.getMax()).toEqual(null);
5358
});
54-
55-
it('Should create MaxHeap using collection : [2, 12, 0, 90]', () => {
56-
const arr = [2, 12, 0, 90];
57-
const mHBulk = new MaxHeap(arr);
58-
59-
expect(mHBulk.getMax()).toEqual(90);
60-
// expect(mHBulk.()).toEqual(90);
61-
});
6259
});

‎src/_DataStructures_/Heaps/MaxHeap/index.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class MaxHeap {
1616
}
1717

1818
getMax() {
19-
return this.heap[0] || null;
19+
return this.heap[0] !==undefined ? this.heap[0] : null;
2020
}
2121

2222
remove() {
23-
const max = this.heap[0] || null;
23+
const max = this.heap[0] !==undefined ? this.heap[0] : null;
2424
// return the element at the root
2525
if (this.heap.length === 1) {
2626
this.heap.pop();
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const MinHeap = require('.');
2+
3+
describe('MinHeap', () => {
4+
it('Should be a class', () => {
5+
expect(typeof MinHeap.prototype.constructor).toEqual('function');
6+
});
7+
8+
const mh = new MinHeap();
9+
10+
beforeEach(() => {
11+
mh.destroy();
12+
});
13+
14+
it('Should create an instance of MinHeap', () => {
15+
expect(mh instanceof MinHeap).toEqual(true);
16+
});
17+
18+
it('Should create a MinHeap using collection', () => {
19+
const mHBulk = new MinHeap([112, 3, 21, 9, 10, 0]);
20+
expect(mHBulk.getMin()).toEqual(0);
21+
});
22+
23+
it('Should add an element to the MinHeap', () => {
24+
mh.add(10);
25+
expect(mh.getMin()).toEqual(10);
26+
});
27+
28+
it('Should keep the smallest element at the root', () => {
29+
[12, 5, 34].forEach(el => mh.add(el));
30+
expect(mh.getMin()).toEqual(5);
31+
});
32+
33+
it('Should retain Heap properties after removal of an element', () => {
34+
[12, 45, 1, 34].forEach(el => mh.add(el));
35+
expect(mh.getMin()).toEqual(1);
36+
mh.remove();
37+
expect(mh.getMin()).toEqual(12);
38+
});
39+
40+
it('Should return `null` when heap is empty', () => {
41+
[1, 34].forEach(el => mh.add(el));
42+
expect(mh.getMin()).toEqual(1);
43+
mh.remove();
44+
mh.remove();
45+
expect(mh.getMin()).toEqual(null);
46+
});
47+
48+
it('Should return the elelment value on `remove()`', () => {
49+
[1, 34].forEach(el => mh.add(el));
50+
expect(mh.getMin()).toEqual(1);
51+
expect(mh.remove()).toEqual(1);
52+
expect(mh.remove()).toEqual(34);
53+
expect(mh.getMin()).toEqual(null);
54+
});
55+
56+
it('Should return `null` on `remove() called on empty heap`', () => {
57+
expect(mh.getMin()).toEqual(null);
58+
});
59+
});

‎src/_DataStructures_/Heaps/MinHeap/index.js‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,20 @@ class MinHeap {
1717
}
1818

1919
getMin() {
20-
return this.heap[0] || null;
20+
return this.heap[0] !==undefined ? this.heap[0] : null;
2121
}
2222

2323
remove() {
24-
const min = this.heap[0] || null;
25-
24+
const min = this.heap[0] !== undefined ? this.heap[0] : null;
25+
if (this.heap.length === 1) {
26+
this.heap.pop();
27+
}
2628
if (this.heap.length > 1) {
2729
this.heap[0] = this.heap[this.heap.length - 1];
2830
this.heap.pop();
2931
// eslint-disable-next-line no-underscore-dangle
3032
this.__heapify(0);
3133
}
32-
if (this.heap.length === 1) {
33-
this.heap.pop();
34-
}
3534
return min;
3635
}
3736

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /