Skip to content

Commit 557d0f4

Browse files
Heaps ascending and decending
1 parent 3bc5e78 commit 557d0f4

File tree

4 files changed

+65
-19
lines changed

4 files changed

+65
-19
lines changed

Datastructure/Non_Linear/Heaps/binaryHeap.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
// Visual Algo - https://visualgo.net/en/heap?slide=1
1616

17+
18+
// ---- binaryHeap.js (CommonJS) ----
1719
class Heap {
1820
constructor(cmp = (a, b) => a - b) {
1921
this.data = [];
@@ -62,24 +64,20 @@ class Heap {
6264
#swap(i, j) { [this.data[i], this.data[j]] = [this.data[j], this.data[i]]; }
6365
}
6466

65-
// Helpers to make min-/max- heaps quickly
66-
exports.minHeap = (cmp) => new Heap(cmp ?? ((a, b) => a - b));
67-
exports.maxHeap = (cmp) => new Heap(cmp ?? ((a, b) => b - a));
68-
69-
// Create a min-heap
70-
const h = minHeap();
67+
const minHeap = (cmp) => new Heap(cmp ?? ((a, b) => a - b));
68+
const maxHeap = (cmp) => new Heap(cmp ?? ((a, b) => b - a));
7169

72-
// Add numbers
73-
h.push(10);
74-
h.push(5);
75-
h.push(20);
76-
h.push(1);
70+
module.exports = { Heap, minHeap, maxHeap };
7771

78-
console.log("Size:", h.size()); // 4
79-
console.log("Peek (min):", h.peek()); // 1
80-
81-
// Pop numbers (always gives the smallest first)
82-
console.log("Pop:", h.pop()); // 1
83-
console.log("Pop:", h.pop()); // 5
84-
console.log("Pop:", h.pop()); // 10
85-
console.log("Pop:", h.pop()); // 20
72+
// If you want to keep a local demo in THIS file, guard it:
73+
if (require.main === module) {
74+
const h = minHeap();
75+
h.push(10); h.push(5); h.push(20); h.push(1);
76+
console.log("Size:", h.size());
77+
console.log("Peek (min):", h.peek());
78+
console.log("Heaps are:", h.data);
79+
console.log("Pop:", h.pop());
80+
console.log("Pop:", h.pop());
81+
console.log("Pop:", h.pop());
82+
console.log("Pop:", h.pop());
83+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Your Heap class stays the same...
2+
3+
const { minHeap } = require('./binaryHeap.js');
4+
5+
function heapSort(arr) {
6+
const h = minHeap();
7+
for (const x of arr) h.push(x);
8+
const out = [];
9+
while (h.size()) out.push(h.pop());
10+
return out;
11+
}
12+
13+
const arr = [10, 5, 20, 1, 72, 33, 2];
14+
console.log("Original:", arr);
15+
console.log("Heap Sort Ascending:", heapSort(arr));
16+
17+
module.exports = { heapSort };
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Your Heap class stays the same...
2+
3+
const { maxHeap } = require('./binaryHeap.js');
4+
5+
function heapSort(arr) {
6+
const h = maxHeap();
7+
for (const x of arr) h.push(x);
8+
const out = [];
9+
while (h.size()) out.push(h.pop());
10+
return out;
11+
}
12+
13+
const arr = [10, 5, 20, 1, 72, 33, 2];
14+
console.log("Original:", arr);
15+
console.log("Heap Sort Decending:", heapSort(arr));
16+
17+
module.exports = { heapSort };
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Heap {
2+
constructor(cmp = (a, b) => ( a - b)) {
3+
this.data = [];
4+
this.cmp = cmp;
5+
}
6+
7+
size() {
8+
return this.data;
9+
}
10+
11+
peek(val) {
12+
return this.data[0];
13+
}
14+
}

0 commit comments

Comments
 (0)