Skip to content

Commit b19e5c6

Browse files
committed
Init repo
0 parents  commit b19e5c6

File tree

12 files changed

+200
-0
lines changed

12 files changed

+200
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

dist/heap.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
class Heap {
4+
constructor(array = [], comparator, swapper) {
5+
this.array = array;
6+
this.pivot = Math.floor(this.array.length / 2);
7+
this.comparator = comparator;
8+
this.swapper = swapper;
9+
}
10+
build() {
11+
for (let i = this.pivot - 1; i >= 0; i--) {
12+
this.heapify(i);
13+
}
14+
}
15+
heapify(index) {
16+
let childIndex1;
17+
let childIndex2;
18+
while (index <= this.pivot - 1) {
19+
childIndex1 = 2 * index + 1;
20+
childIndex2 = 2 * index + 2;
21+
let swapIndex = childIndex1;
22+
if (this.compare(this.array[childIndex1], this.array[childIndex2])) {
23+
swapIndex = childIndex2;
24+
}
25+
if (this.compare(this.array[index], this.array[swapIndex])) {
26+
this.swap(index, swapIndex);
27+
}
28+
index = swapIndex;
29+
}
30+
}
31+
swap(index1, index2) {
32+
if (this.swapper) {
33+
this.swapper(index1, index2);
34+
}
35+
else {
36+
const temp = this.array[index1];
37+
this.array[index1] = this.array[index2];
38+
this.array[index2] = temp;
39+
}
40+
}
41+
compare(item1, item2) {
42+
if (this.comparator) {
43+
return this.comparator(item1, item2);
44+
}
45+
return item1 < item2;
46+
}
47+
}
48+
exports.Heap = Heap;

dist/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const heap_1 = require("./heap");
4+
const h = new heap_1.Heap([2, 7, 4, 1, 8, 1]);
5+
h.build();
6+
console.log(h.array);

dist/interface.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
;

dist/priority-queue.js

Whitespace-only changes.

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "priority-queue",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "tsc",
8+
"test": "node ./dist/index"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"devDependencies": {
14+
"typescript": "^3.8.3"
15+
}
16+
}

src/heap.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { IHeap } from './interface';
2+
3+
export class Heap<T> implements IHeap {
4+
public array: T[];
5+
private pivot: number;
6+
private comparator: (item1: T, item2: T) => boolean;
7+
private swapper: (index1: number, index2: number) => void;
8+
9+
constructor(
10+
array: T[] = [],
11+
comparator?: (item1: T, item2: T) => boolean,
12+
swapper?: (index1: number, index2: number) => void
13+
) {
14+
this.array = array;
15+
this.pivot = Math.floor(this.array.length / 2);
16+
this.comparator = comparator;
17+
this.swapper = swapper;
18+
}
19+
20+
public build() {
21+
for (let i = this.pivot - 1; i >= 0; i--) {
22+
this.heapify(i);
23+
}
24+
}
25+
26+
public heapify(index: number) {
27+
let childIndex1;
28+
let childIndex2;
29+
30+
while (index <= this.pivot - 1) {
31+
childIndex1 = 2 * index + 1;
32+
childIndex2 = 2 * index + 2;
33+
let swapIndex = childIndex1;
34+
35+
if (this.compare(this.array[childIndex1], this.array[childIndex2])) {
36+
swapIndex = childIndex2;
37+
}
38+
39+
if (this.compare(this.array[index], this.array[swapIndex])) {
40+
this.swap(index, swapIndex);
41+
}
42+
43+
index = swapIndex;
44+
}
45+
}
46+
47+
public swap(index1: number, index2: number) {
48+
if (this.swapper) {
49+
this.swapper(index1, index2);
50+
} else {
51+
const temp = this.array[index1];
52+
this.array[index1] = this.array[index2];
53+
this.array[index2] = temp;
54+
}
55+
}
56+
57+
private compare(item1: T, item2: T) {
58+
if (this.comparator) {
59+
return this.comparator(item1, item2);
60+
}
61+
return item1 < item2;
62+
}
63+
}

src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Heap } from './heap';
2+
3+
const h = new Heap([2, 7, 4, 1, 8, 1]);
4+
h.build();
5+
6+
console.log(h.array);
7+
8+
9+
// var stones = [2, 7, 4, 1, 8, 1];
10+
// var stones = [4, 3, 4, 3, 2];
11+
// var stones = [722, 512, 290, 559, 884, 608, 932, 781, 864, 398, 890, 574, 472, 125, 620, 728, 447, 150, 52, 116, 786, 717, 172, 549, 350, 268, 951, 315, 482, 170];

src/interface.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export interface IHeap {
2+
build: () => void;
3+
heapify: (index: number) => void;
4+
};
5+
6+
export interface IPriorityQueue<T> {
7+
add?: (e: T) => void;
8+
peek?: () => void;
9+
poll?: () => void;
10+
remove?: () => void;
11+
contains?: () => void;
12+
size?: () => void;
13+
clear?: () => void;
14+
}

0 commit comments

Comments
 (0)