Skip to content

Commit 0848b4f

Browse files
committed
[Heap]
1 parent 3ae9130 commit 0848b4f

14 files changed

+161
-14
lines changed

examples/PacktDataStructuresAlgorithms.min.js

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/PacktDataStructuresAlgorithms.min.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="01-UsingMinHeap.js"></script>
10+
</body>
11+
</html>

examples/chapter10/01-UsingMinHeap.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { MinHeap } = PacktDataStructuresAlgorithms;
2+
3+
const heap = new MinHeap();
4+
5+
heap.insert(2);
6+
heap.insert(3);
7+
heap.insert(4);
8+
heap.insert(5);
9+
10+
heap.insert(1);
11+
12+
console.log(heap.getAsArray());
13+
14+
console.log('Heap size: ', heap.size()); // 5
15+
console.log('Heap is empty: ', heap.isEmpty()); // false
16+
console.log('Heap min value: ', heap.findMinimum()); // 1
17+
18+
heap.insert(6);
19+
heap.insert(7);
20+
21+
console.log(heap.getAsArray());
22+
23+
console.log('Extract minimum: ', heap.extract());
24+
console.log(heap.getAsArray());
25+
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="02-UsingMaxHeap.js"></script>
10+
</body>
11+
</html>

examples/chapter10/02-UsingMaxHeap.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { MaxHeap } = PacktDataStructuresAlgorithms;
2+
3+
const maxHeap = new MaxHeap();
4+
5+
maxHeap.insert(2);
6+
maxHeap.insert(3);
7+
maxHeap.insert(4);
8+
maxHeap.insert(5);
9+
10+
maxHeap.insert(1);
11+
12+
console.log(maxHeap.getAsArray());
13+
14+
console.log('Heap size: ', maxHeap.size()); // 5
15+
console.log('Heap is empty: ', maxHeap.isEmpty()); // false
16+
console.log('Heap min value: ', maxHeap.findMinimum()); // 5
17+
18+
maxHeap.insert(6);
19+
maxHeap.insert(9);
20+
maxHeap.insert(10);
21+
maxHeap.insert(14);
22+
23+
console.log(maxHeap.getAsArray());
24+
25+
console.log('Extract minimum: ', maxHeap.extract());
26+
console.log(maxHeap.getAsArray());
27+

examples/chapter10/03-HeapSort.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="03-HeapSort.js"></script>
10+
</body>
11+
</html>

examples/chapter10/03-HeapSort.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const { heapSort } = PacktDataStructuresAlgorithms;
2+
3+
console.log('********** Heap Sort **********');
4+
const array = [7, 6, 3, 5, 4, 1, 2];
5+
6+
console.log('Before sorting: ', array);
7+
console.log('After sorting: ', heapSort(array));

examples/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,19 @@
190190
</div>
191191
</div>
192192
</section>
193+
<section class="mdl-layout__tab-panel" id="scroll-tab-10">
194+
<div class="page-content">
195+
<div class="page-content mdl-layout--fixed-drawer">
196+
<div class="mdl-layout__drawer is-visible">
197+
<nav class="mdl-navigation">
198+
<a class="mdl-navigation__link" href="chapter10/01-UsingMinHeap.html">01-UsingMinHeap</a>
199+
<a class="mdl-navigation__link" href="chapter10/02-UsingMaxHeap.html">02-UsingMaxHeap</a>
200+
<a class="mdl-navigation__link" href="chapter10/03-HeapSort.html">03-HeapSort</a>
201+
</nav>
202+
</div>
203+
</div>
204+
</div>
205+
</section>
193206
<section class="mdl-layout__tab-panel" id="scroll-tab-11">
194207
<div class="page-content">
195208
<div class="page-content mdl-layout--fixed-drawer">

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"tslint": "^5.9.1",
7676
"typescript": "^2.7.2",
7777
"webpack": "^4.1.1",
78+
"webpack-cli": "^2.0.12",
7879
"yargs": "^11.0.0"
7980
}
8081
}

src/js/data-structures/heap.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class MinHeap {
2626
clear() {
2727
this.heap = [];
2828
}
29-
find() {
29+
findMinimum() {
3030
return this.isEmpty() ? undefined : this.heap[0];
3131
}
3232
insert(value) {
@@ -43,12 +43,15 @@ export class MinHeap {
4343
const left = this.getLeftIndex(index);
4444
const right = this.getRightIndex(index);
4545
const size = this.size();
46-
if (left < size && this.compareFn(this.heap[element], this.heap[left]) > Compare.BIGGER_THAN) {
46+
if (
47+
left < size &&
48+
this.compareFn(this.heap[element], this.heap[left]) === Compare.BIGGER_THAN
49+
) {
4750
element = left;
4851
}
4952
if (
5053
right < size &&
51-
this.compareFn(this.heap[element], this.heap[right]) > Compare.BIGGER_THAN
54+
this.compareFn(this.heap[element], this.heap[right]) === Compare.BIGGER_THAN
5255
) {
5356
element = right;
5457
}
@@ -59,7 +62,10 @@ export class MinHeap {
5962
}
6063
siftUp(index) {
6164
let parent = this.getParentIndex(index);
62-
while (index > 0 && this.compareFn(this.heap[parent], this.heap[index]) > Compare.BIGGER_THAN) {
65+
while (
66+
index > 0 &&
67+
this.compareFn(this.heap[parent], this.heap[index]) === Compare.BIGGER_THAN
68+
) {
6369
swap(this.heap, parent, index);
6470
index = parent;
6571
parent = this.getParentIndex(index);
@@ -79,11 +85,15 @@ export class MinHeap {
7985
heapify(array) {
8086
if (array) {
8187
this.heap = array;
82-
this.heap.unshift(null); // remove all null elements
8388
}
84-
for (let i = this.size() - 1; i > 0; i--) {
89+
const maxIndex = Math.floor(this.size() / 2) - 1;
90+
for (let i = 0; i <= maxIndex; i++) {
8591
this.siftDown(i);
8692
}
93+
return this.heap;
94+
}
95+
getAsArray() {
96+
return this.heap;
8797
}
8898
}
8999
export class MaxHeap extends MinHeap {

src/ts/data-structures/heap.ts

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ export class MinHeap<T> {
103103

104104
return this.heap;
105105
}
106+
107+
getAsArray() {
108+
return this.heap;
109+
}
106110
}
107111

108112
export class MaxHeap<T> extends MinHeap<T> {

test/ts/data-structures/heap.spec.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'mocha';
2-
// import { expect } from 'chai';
2+
import { expect } from 'chai';
33
import { MinHeap } from '../../../src/ts/index';
44
import { MaxHeap } from '../../../src/ts/data-structures/heap';
55
import heapSort from '../../../src/ts/algorithms/sorting/heap-sort';
@@ -15,12 +15,27 @@ describe('Heap', () => {
1515

1616
});
1717

18-
it('inserts elements in the AVLTree', () => {
18+
it('inserts elements in the Heap', () => {
19+
20+
let min = 2;
1921

20-
heap.insert(3);
2122
heap.insert(2);
22-
heap.insert(1);
23+
expect(heap.findMinimum()).to.equal(min);
24+
heap.insert(3);
25+
expect(heap.findMinimum()).to.equal(min);
2326
heap.insert(4);
27+
expect(heap.findMinimum()).to.equal(min);
28+
heap.insert(5);
29+
expect(heap.findMinimum()).to.equal(min);
30+
31+
heap.insert(1);
32+
min = 1;
33+
expect(heap.findMinimum()).to.equal(min);
34+
35+
heap.insert(6);
36+
heap.insert(9);
37+
heap.insert(10);
38+
heap.insert(14);
2439

2540
heap.extract();
2641
heap.extract();

webpack.config.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22
/* eslint-disable */
33
const webpack = require('webpack');
4-
const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
4+
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');;
55
const path = require('path');
66
const env = require('yargs').argv.env;
77

@@ -11,7 +11,7 @@ let plugins = [],
1111
outputFile;
1212

1313
if (env === 'build') {
14-
plugins.push(new UglifyJsPlugin({ minimize: true }));
14+
// plugins.push(new UglifyJsPlugin({ minimize: true }));
1515
outputFile = libraryName + '.min.js';
1616
} else {
1717
outputFile = libraryName + '.js';
@@ -40,7 +40,17 @@ const config = {
4040
modules: [path.resolve('./node_modules'), path.resolve('./src/js')],
4141
extensions: ['.json', '.js']
4242
},
43-
plugins: plugins
43+
optimization: {
44+
minimizer: [
45+
// we specify a custom UglifyJsPlugin here to get source maps in production
46+
new UglifyJsPlugin({
47+
cache: true,
48+
parallel: true,
49+
sourceMap: true
50+
})
51+
]
52+
}
53+
// plugins: plugins
4454
};
4555

4656
module.exports = config;

0 commit comments

Comments
 (0)