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

Third edition #75

Merged
merged 5 commits into from
Apr 25, 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
2 changes: 1 addition & 1 deletion examples/PacktDataStructuresAlgorithms.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/PacktDataStructuresAlgorithms.min.js.map

Large diffs are not rendered by default.

41 changes: 21 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@
"istanbul": "^v1.1.0-alpha.1",
"mocha": "^5.0.4",
"mochawesome": "^3.0.2",
"nyc": "11.7.0",
"nyc": "11.7.1",
"ts-node": "^5.0.1",
"tslint": "^5.9.1",
"typescript": "^2.7.2",
"webpack": "^4.1.1",
"webpack-cli": "2.0.14",
"webpack-cli": "2.0.15",
"yargs": "^11.0.0"
},
"dependencies": {
Expand Down
6 changes: 3 additions & 3 deletions src/js/data-structures/binary-search-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Node } from './models/node';
export default class BinarySearchTree {
constructor(compareFn = defaultCompare) {
this.compareFn = compareFn;
this.root = null;
this.root = undefined;
}
insert(key) {
// special case: first key
Expand Down Expand Up @@ -99,7 +99,7 @@ export default class BinarySearchTree {
}
removeNode(node, key) {
if (node == null) {
return null;
return undefined;
}
if (this.compareFn(key, node.key) === Compare.LESS_THAN) {
node.left = this.removeNode(node.left, key);
Expand All @@ -115,7 +115,7 @@ export default class BinarySearchTree {
// 3 - a node with 2 children
// case 1
if (node.left == null && node.right == null) {
node = null;
node = undefined;
return node;
}
// case 2
Expand Down
4 changes: 2 additions & 2 deletions src/js/data-structures/models/node.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export class Node {
constructor(key) {
this.key = key;
this.left = null;
this.right = null;
this.left = undefined;
this.right = undefined;
}
toString() {
return `${this.key}`;
Expand Down
5 changes: 5 additions & 0 deletions test/js/algorithms/search/binary-search-recursive.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { binarySearchRecursive } from '../../../../src/js/index';
import { testSearchAlgorithm } from './search-algorithms-tests';

testSearchAlgorithm(binarySearchRecursive, 'Binary Search Recursive');

32 changes: 32 additions & 0 deletions test/js/data-structures/avl-tree.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'mocha';
import { expect } from 'chai';
import { AVLTree } from '../../../src/js/index';

describe('AVLTree', () => {
let tree;

beforeEach(() => {
tree = new AVLTree();
});

it('starts empty', () => {
expect(tree.getRoot()).to.equal(null);
});

it('inserts elements in the AVLTree', () => {
expect(tree.getRoot()).to.equal(null);

tree.insert(1);
tree.insert(2);
tree.insert(3);
tree.insert(4);
tree.insert(5);
tree.insert(6);
tree.insert(7);
tree.insert(14);
tree.insert(15);
tree.insert(13);
tree.insert(12);
tree.insert(11);
});
});
108 changes: 108 additions & 0 deletions test/js/data-structures/binary-search-tree.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import 'mocha';
import { expect } from 'chai';
import { BinarySearchTree } from '../../../src/js/index';

describe('BinarySearchTree', () => {
let tree;

beforeEach(() => {
tree = new BinarySearchTree();
});

it('starts empty', () => {
expect(tree.getRoot()).to.equal(undefined);
});

function assertNode(node, key, left, right) {
if (key != null) {
expect(node.key).to.equal(key);
} else {
expect(node).to.equal(key);
return;
}

if (left != null) {
expect(node.left.key).to.equal(left);
} else {
expect(node.left).to.equal(left);
}

if (right != null) {
expect(node.right.key).to.equal(right);
} else {
expect(node.right).to.equal(right);
}
}

it('inserts elements in the BST', () => {
expect(tree.getRoot()).to.equal(undefined);

tree.insert(11);
tree.insert(7);
tree.insert(15);
tree.insert(5);
tree.insert(3);
tree.insert(9);
tree.insert(8);
tree.insert(10);
tree.insert(13);
tree.insert(12);
tree.insert(14);
tree.insert(20);
tree.insert(18);
tree.insert(25);

let node = tree.getRoot();
assertNode(node, 11, 7, 15);

node = node.left;
assertNode(node, 7, 5, 9);

node = node.left;
assertNode(node, 5, 3, undefined);

node = node.left;
assertNode(node, 3, undefined, undefined);

node = tree.getRoot().left.left.right;
assertNode(node, undefined, undefined, undefined);

node = tree.getRoot().left.right;
assertNode(node, 9, 8, 10);

node = node.left;
assertNode(node, 8, undefined, undefined);

node = tree.getRoot().left.right.right;
assertNode(node, 10, undefined, undefined);

node = tree.getRoot().right;
assertNode(node, 15, 13, 20);

node = node.left;
assertNode(node, 13, 12, 14);

node = node.left;
assertNode(node, 12, undefined, undefined);

node = tree.getRoot().right.left.right;
assertNode(node, 14, undefined, undefined);

node = tree.getRoot().right.right;
assertNode(node, 20, 18, 25);

node = node.left;
assertNode(node, 18, undefined, undefined);

node = tree.getRoot().right.right.right;
assertNode(node, 25, undefined, undefined);
});

it('verifies if element exists', () => {
expect(tree.getRoot()).to.equal(undefined);
});

it('removes a leaf', () => {
expect(tree.getRoot()).to.equal(undefined);
});
});
75 changes: 75 additions & 0 deletions test/js/data-structures/heap.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'mocha';
import { expect } from 'chai';
import { MinHeap, heapSort } from '../../../src/js/index';

describe('Heap', () => {
let heap;

beforeEach(() => {
heap = new MinHeap();
});

it('starts empty MinHeap', () => {
expect(heap.size()).to.equal(0);
expect(heap.isEmpty()).to.equal(true);
});

it('inserts values in the MinHeap', () => {
const resultArray = [];
for (let i = 1; i < 10; i++) {
resultArray.push(i);
heap.insert(i);
expect(heap.getAsArray()).to.deep.equal(resultArray);
}
});

it('finds the min value from the MinHeap', () => {
const resultArray = [];
for (let i = 10; i >= 1; i--) {
resultArray.push(i);
heap.insert(i);
expect(heap.findMinimum()).to.equal(i);
}
});

it('performs heapify in the MinHeap', () => {
const resultArray = [];
for (let i = 10; i >= 1; i--) {
resultArray.push(i);
}
expect(heap.heapify(resultArray)).to.deep.equal(resultArray);
});

it('extracts the min value from the MinHeap', () => {
let resultArray = [];
for (let i = 1; i < 10; i++) {
resultArray.push(i);
heap.insert(i);
expect(heap.getAsArray()).to.deep.equal(resultArray);
}

resultArray = [
[],
[2, 4, 3, 8, 5, 6, 7, 9],
[3, 4, 6, 8, 5, 9, 7],
[4, 5, 6, 8, 7, 9],
[5, 7, 6, 8, 9],
[6, 7, 9, 8],
[7, 8, 9],
[8, 9],
[9],
[]
];

for (let i = 1; i < 10; i++) {
expect(heap.extract()).to.equal(i);
expect(heap.getAsArray()).to.deep.equal(resultArray[i]);
}
});

it('Heap Sort', () => {
const array = [3, 2, 5, 6, 1, 7, 8, 9];

expect(heapSort(array)).to.deep.equal([1, 2, 3, 5, 6, 7, 8, 9]);
});
});
6 changes: 2 additions & 4 deletions test/ts/data-structures/heap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'mocha';
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';
import { MinHeap, heapSort } from '../../../src/ts/index';

describe('Heap', () => {
let heap: MinHeap<number>;
Expand Down Expand Up @@ -55,7 +53,7 @@ describe('Heap', () => {
[2, 4, 3, 8, 5, 6, 7, 9],
[3, 4, 6, 8, 5, 9, 7],
[4, 5, 6, 8, 7, 9],
[5, 7 , 6, 8, 9],
[5, 7, 6, 8, 9],
[6, 7, 9, 8],
[7, 8, 9],
[8, 9],
Expand Down