Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Heap] #46

Merged
merged 2 commits into from
Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/PacktDataStructuresAlgorithms.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/PacktDataStructuresAlgorithms.min.js.map

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions examples/chapter10/01-UsingMinHeap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="01-UsingMinHeap.js"></script>
</body>
</html>
25 changes: 25 additions & 0 deletions examples/chapter10/01-UsingMinHeap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { MinHeap } = PacktDataStructuresAlgorithms;

const heap = new MinHeap();

heap.insert(2);
heap.insert(3);
heap.insert(4);
heap.insert(5);

heap.insert(1);

console.log(heap.getAsArray());

console.log('Heap size: ', heap.size()); // 5
console.log('Heap is empty: ', heap.isEmpty()); // false
console.log('Heap min value: ', heap.findMinimum()); // 1

heap.insert(6);
heap.insert(7);

console.log(heap.getAsArray());

console.log('Extract minimum: ', heap.extract());
console.log(heap.getAsArray());

11 changes: 11 additions & 0 deletions examples/chapter10/02-UsingMaxHeap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="02-UsingMaxHeap.js"></script>
</body>
</html>
27 changes: 27 additions & 0 deletions examples/chapter10/02-UsingMaxHeap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { MaxHeap } = PacktDataStructuresAlgorithms;

const maxHeap = new MaxHeap();

maxHeap.insert(2);
maxHeap.insert(3);
maxHeap.insert(4);
maxHeap.insert(5);

maxHeap.insert(1);

console.log(maxHeap.getAsArray());

console.log('Heap size: ', maxHeap.size()); // 5
console.log('Heap is empty: ', maxHeap.isEmpty()); // false
console.log('Heap min value: ', maxHeap.findMinimum()); // 5

maxHeap.insert(6);
maxHeap.insert(9);
maxHeap.insert(10);
maxHeap.insert(14);

console.log(maxHeap.getAsArray());

console.log('Extract minimum: ', maxHeap.extract());
console.log(maxHeap.getAsArray());

11 changes: 11 additions & 0 deletions examples/chapter10/03-HeapSort.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="03-HeapSort.js"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions examples/chapter10/03-HeapSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { heapSort } = PacktDataStructuresAlgorithms;

console.log('********** Heap Sort **********');
const array = [7, 6, 3, 5, 4, 1, 2];

console.log('Before sorting: ', array);
console.log('After sorting: ', heapSort(array));
6 changes: 3 additions & 3 deletions examples/chapter11/03-DFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ graph.addEdge('C', 'F');
graph.addEdge('F', 'E');

const result = DFS(graph);
console.log(result.discovery);
console.log(result.finished);
console.log(result.predecessors);
console.log('discovery', result.discovery);
console.log('finished', result.finished);
console.log('predecessors', result.predecessors);

const fTimes = result.finished;
s = '';
Expand Down
13 changes: 13 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@
</div>
</div>
</section>
<section class="mdl-layout__tab-panel" id="scroll-tab-10">
<div class="page-content">
<div class="page-content mdl-layout--fixed-drawer">
<div class="mdl-layout__drawer is-visible">
<nav class="mdl-navigation">
<a class="mdl-navigation__link" href="chapter10/01-UsingMinHeap.html">01-UsingMinHeap</a>
<a class="mdl-navigation__link" href="chapter10/02-UsingMaxHeap.html">02-UsingMaxHeap</a>
<a class="mdl-navigation__link" href="chapter10/03-HeapSort.html">03-HeapSort</a>
</nav>
</div>
</div>
</div>
</section>
<section class="mdl-layout__tab-panel" id="scroll-tab-11">
<div class="page-content">
<div class="page-content mdl-layout--fixed-drawer">
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"tslint": "^5.9.1",
"typescript": "^2.7.2",
"webpack": "^4.1.1",
"webpack-cli": "^2.0.12",
"yargs": "^11.0.0"
}
}
6 changes: 3 additions & 3 deletions src/js/algorithms/graph/depth-first-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const depthFirstSearch = (graph, callback) => {
const DFSVisit = (u, color, d, f, p, time, adjList) => {
// console.log('discovered ' + u);
color[u] = Colors.GREY;
d[u] = ++time;
d[u] = ++time.count;
const neighbors = adjList.get(u);
for (let i = 0; i < neighbors.length; i++) {
const w = neighbors[i];
Expand All @@ -56,7 +56,7 @@ const DFSVisit = (u, color, d, f, p, time, adjList) => {
}
}
color[u] = Colors.BLACK;
f[u] = ++time;
f[u] = ++time.count;
// console.log('explored ' + u);
};

Expand All @@ -67,7 +67,7 @@ export const DFS = graph => {
const d = {};
const f = {};
const p = {};
const time = 0;
const time = { count: 0 };
for (let i = 0; i < vertices.length; i++) {
f[vertices[i]] = 0;
d[vertices[i]] = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/js/data-structures/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class Graph {
this.addVertex(b);
}
this.adjList.get(a).push(b);
if (!this.isDirected) {
if (this.isDirected !== true) {
this.adjList.get(b).push(a);
}
}
Expand Down
22 changes: 16 additions & 6 deletions src/js/data-structures/heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class MinHeap {
clear() {
this.heap = [];
}
find() {
findMinimum() {
return this.isEmpty() ? undefined : this.heap[0];
}
insert(value) {
Expand All @@ -43,12 +43,15 @@ export class MinHeap {
const left = this.getLeftIndex(index);
const right = this.getRightIndex(index);
const size = this.size();
if (left < size && this.compareFn(this.heap[element], this.heap[left]) > Compare.BIGGER_THAN) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loiane, I got the third edition book but it seems it was still with this issue.

if (
left < size &&
this.compareFn(this.heap[element], this.heap[left]) === Compare.BIGGER_THAN
) {
element = left;
}
if (
right < size &&
this.compareFn(this.heap[element], this.heap[right]) > Compare.BIGGER_THAN
this.compareFn(this.heap[element], this.heap[right]) === Compare.BIGGER_THAN
) {
element = right;
}
Expand All @@ -59,7 +62,10 @@ export class MinHeap {
}
siftUp(index) {
let parent = this.getParentIndex(index);
while (index > 0 && this.compareFn(this.heap[parent], this.heap[index]) > Compare.BIGGER_THAN) {
while (
index > 0 &&
this.compareFn(this.heap[parent], this.heap[index]) === Compare.BIGGER_THAN
) {
swap(this.heap, parent, index);
index = parent;
parent = this.getParentIndex(index);
Expand All @@ -79,11 +85,15 @@ export class MinHeap {
heapify(array) {
if (array) {
this.heap = array;
this.heap.unshift(null); // remove all null elements
}
for (let i = this.size() - 1; i > 0; i--) {
const maxIndex = Math.floor(this.size() / 2) - 1;
for (let i = 0; i <= maxIndex; i++) {
this.siftDown(i);
}
return this.heap;
}
getAsArray() {
return this.heap;
}
}
export class MaxHeap extends MinHeap {
Expand Down
4 changes: 4 additions & 0 deletions src/ts/data-structures/heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export class MinHeap<T> {

return this.heap;
}

getAsArray() {
return this.heap;
}
}

export class MaxHeap<T> extends MinHeap<T> {
Expand Down
23 changes: 19 additions & 4 deletions test/ts/data-structures/heap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'mocha';
// import { expect } from 'chai';
import { expect } from 'chai';
import { MinHeap } from '../../../src/ts/index';
import { MaxHeap } from '../../../src/ts/data-structures/heap';
import heapSort from '../../../src/ts/algorithms/sorting/heap-sort';
Expand All @@ -15,12 +15,27 @@ describe('Heap', () => {

});

it('inserts elements in the AVLTree', () => {
it('inserts elements in the Heap', () => {

let min = 2;

heap.insert(3);
heap.insert(2);
heap.insert(1);
expect(heap.findMinimum()).to.equal(min);
heap.insert(3);
expect(heap.findMinimum()).to.equal(min);
heap.insert(4);
expect(heap.findMinimum()).to.equal(min);
heap.insert(5);
expect(heap.findMinimum()).to.equal(min);

heap.insert(1);
min = 1;
expect(heap.findMinimum()).to.equal(min);

heap.insert(6);
heap.insert(9);
heap.insert(10);
heap.insert(14);

heap.extract();
heap.extract();
Expand Down
16 changes: 13 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check
/* eslint-disable */
const webpack = require('webpack');
const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');;
const path = require('path');
const env = require('yargs').argv.env;

Expand All @@ -11,7 +11,7 @@ let plugins = [],
outputFile;

if (env === 'build') {
plugins.push(new UglifyJsPlugin({ minimize: true }));
// plugins.push(new UglifyJsPlugin({ minimize: true }));
outputFile = libraryName + '.min.js';
} else {
outputFile = libraryName + '.js';
Expand Down Expand Up @@ -40,7 +40,17 @@ const config = {
modules: [path.resolve('./node_modules'), path.resolve('./src/js')],
extensions: ['.json', '.js']
},
plugins: plugins
optimization: {
minimizer: [
// we specify a custom UglifyJsPlugin here to get source maps in production
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true
})
]
}
// plugins: plugins
};

module.exports = config;