Skip to content

Commit 1829d34

Browse files
committed
chapter 10: [Heap]
1 parent aa0bb50 commit 1829d34

File tree

9 files changed

+59
-50
lines changed

9 files changed

+59
-50
lines changed

examples/PacktDataStructuresAlgorithms.min.js

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

src/js/data-structures/binary-search-tree.js

+26-30
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ export default class BinarySearchTree {
2121
} else {
2222
this.insertNode(node.left, key);
2323
}
24+
} else if (node.right == null) {
25+
node.right = new Node(key);
2426
} else {
25-
if (node.right == null) {
26-
node.right = new Node(key);
27-
} else {
28-
this.insertNode(node.right, key);
29-
}
27+
this.insertNode(node.right, key);
3028
}
3129
}
3230
getRoot() {
@@ -43,9 +41,8 @@ export default class BinarySearchTree {
4341
return this.searchNode(node.left, key);
4442
} else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) {
4543
return this.searchNode(node.right, key);
46-
} else {
47-
return true;
4844
}
45+
return true;
4946
}
5047
inOrderTraverse(callback) {
5148
this.inOrderTraverseNode(this.root, callback);
@@ -110,30 +107,29 @@ export default class BinarySearchTree {
110107
} else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) {
111108
node.right = this.removeNode(node.right, key);
112109
return node;
113-
} else {
114-
// key is equal to node.item
115-
// handle 3 special conditions
116-
// 1 - a leaf node
117-
// 2 - a node with only 1 child
118-
// 3 - a node with 2 children
119-
// case 1
120-
if (node.left == null && node.right == null) {
121-
node = null;
122-
return node;
123-
}
124-
// case 2
125-
if (node.left == null) {
126-
node = node.right;
127-
return node;
128-
} else if (node.right == null) {
129-
node = node.left;
130-
return node;
131-
}
132-
// case 3
133-
const aux = this.minNode(node.right);
134-
node.key = aux.key;
135-
node.right = this.removeNode(node.right, aux.key);
110+
}
111+
// key is equal to node.item
112+
// handle 3 special conditions
113+
// 1 - a leaf node
114+
// 2 - a node with only 1 child
115+
// 3 - a node with 2 children
116+
// case 1
117+
if (node.left == null && node.right == null) {
118+
node = null;
119+
return node;
120+
}
121+
// case 2
122+
if (node.left == null) {
123+
node = node.right;
124+
return node;
125+
} else if (node.right == null) {
126+
node = node.left;
136127
return node;
137128
}
129+
// case 3
130+
const aux = this.minNode(node.right);
131+
node.key = aux.key;
132+
node.right = this.removeNode(node.right, aux.key);
133+
return node;
138134
}
139135
}

src/js/data-structures/heap.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ export class MinHeap {
66
this.heap = [];
77
}
88
getLeftIndex(index) {
9-
return 2 * index + 1;
9+
return (2 * index) + 1;
1010
}
1111
getRightIndex(index) {
12-
return 2 * index + 2;
12+
return (2 * index) + 2;
1313
}
1414
getParentIndex(index) {
1515
if (index === 0) {

src/js/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ export { default as BinarySearchTree } from './data-structures/binary-search-tre
4747
export { default as AVLTree } from './data-structures/avl-tree';
4848

4949
// chapter 10
50-
export { MinHeap as MinHeap } from './data-structures/heap';
51-
export { MaxHeap as MaxHeap } from './data-structures/heap';
52-
export { default as heapSort } from './data-structures/sorting/heap-sort';
50+
export { MinHeap } from './data-structures/heap';
51+
export { MaxHeap } from './data-structures/heap';
52+
export { default as heapSort } from './sorting/heap-sort';

src/js/sorting/heap-sort.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { defaultCompare, swap } from '../util';
22

33
function heapify(array, index, heapSize, compareFn) {
44
let largest = index;
5-
const left = 2 * index + 1;
6-
const right = 2 * index + 2;
5+
const left = (2 * index) + 1;
6+
const right = (2 * index) + 2;
77
if (left < heapSize && compareFn(array[left], array[index]) > 0) {
88
largest = left;
99
}

src/ts/data-structures/binary-search-tree.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ export default class BinarySearchTree<T> {
2222
} else {
2323
this.insertNode(node.left, key);
2424
}
25+
} else if (node.right == null) {
26+
node.right = new Node(key);
2527
} else {
26-
if (node.right == null) {
27-
node.right = new Node(key);
28-
} else {
29-
this.insertNode(node.right, key);
30-
}
28+
this.insertNode(node.right, key);
3129
}
3230
}
3331

@@ -48,10 +46,9 @@ export default class BinarySearchTree<T> {
4846
return this.searchNode(node.left, key);
4947
} else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) {
5048
return this.searchNode(node.right, key);
51-
} else {
52-
// key is equal to node.item
53-
return true;
5449
}
50+
// key is equal to node.item
51+
return true;
5552
}
5653

5754
inOrderTraverse(callback: Function) {

src/ts/data-structures/graph.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Dictionary from './dictionary';
2+
3+
export default class Graph<T> {
4+
private vertices: T[] = [];
5+
private adjList: Dictionary<T, T[]> = new Dictionary();
6+
7+
addVertex(v: T) {
8+
this.vertices.push(v);
9+
this.adjList.set(v, []); // initialize adjacency list with array as well;
10+
}
11+
12+
addEdge(v: T, w: T) {
13+
this.adjList.get(v).push(w);
14+
// adjList.get(w).push(v); //commented to run the improved DFS with topological sorting
15+
}
16+
}

src/ts/data-structures/heap.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export class MinHeap<T> {
66
constructor(protected compareFn: ICompareFunction<T> = defaultCompare) {}
77

88
private getLeftIndex(index: number) {
9-
return 2 * index + 1;
9+
return (2 * index) + 1;
1010
}
1111

1212
private getRightIndex(index: number) {
13-
return 2 * index + 2;
13+
return (2 * index) + 2;
1414
}
1515

1616
private getParentIndex(index: number) {

src/ts/sorting/heap-sort.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { defaultCompare, ICompareFunction, swap } from '../util';
22

33
function heapify(array: any[], index: number, heapSize: number, compareFn: ICompareFunction<any>) {
44
let largest = index;
5-
const left = 2 * index + 1;
6-
const right = 2 * index + 2;
5+
const left = (2 * index) + 1;
6+
const right = (2 * index) + 2;
77

88
if (left < heapSize && compareFn(array[left], array[index]) > 0) {
99
largest = left;

0 commit comments

Comments
 (0)