Skip to content

Commit affd30d

Browse files
implement Heap Class , guess it start with 1
1 parent d103018 commit affd30d

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

dist/lib/Heap.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var Heap = /** @class */ (function () {
4+
function Heap(capacity) {
5+
this.a = new Array(capacity + 1);
6+
this.n = capacity;
7+
this.count = 0;
8+
}
9+
Heap.prototype.insert = function (data) {
10+
if (this.count >= this.n)
11+
return;
12+
++this.count;
13+
this.a[this.count] = data;
14+
var i = this.count;
15+
while (i / 2 > 0 && this.a[i] > this.a[i / 2]) {
16+
this.swap(this.a, i, Math.floor(i / 2));
17+
}
18+
};
19+
Heap.prototype.swap = function (a, i, max) {
20+
var tmp = this.a[i];
21+
a[i] = a[max];
22+
a[max] = tmp;
23+
};
24+
return Heap;
25+
}());
26+
exports.Heap = Heap;

dist/lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var QuickSort_1 = require("./QuickSort");
1212
var CountingSort_1 = require("./CountingSort");
1313
var BinarySearch_1 = require("./BinarySearch");
1414
var Nodes_1 = require("./Nodes");
15+
var Heap_1 = require("./Heap");
1516
exports.default = {
1617
LinkedList: LinkedList_1.LinkedList,
1718
ArrayStack: ArrayStack_1.ArrayStack,
@@ -25,4 +26,5 @@ exports.default = {
2526
CountingSort: CountingSort_1.CountingSort,
2627
BinarySearch: BinarySearch_1.BinarySearch,
2728
Nodes: Nodes_1.Nodes,
29+
Heap: Heap_1.Heap,
2830
};

src/lib/Heap.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
export class Heap {
2+
private a: Array<number>
3+
private n: number
4+
private count: number
5+
6+
constructor(capacity: number) {
7+
this.a = new Array(capacity + 1)
8+
this.n = capacity
9+
this.count = 0
10+
}
11+
12+
public insert(data: number): void {
13+
if (this.count >= this.n) return
14+
++this.count
15+
this.a[this.count] = data
16+
let i = this.count
17+
while (i / 2 > 0 && this.a[i] > this.a[i / 2]) {
18+
this.swap(this.a, i, Math.floor(i / 2))
19+
}
20+
21+
}
22+
23+
public swap(a: Array<number>, i: number, max: number): void {
24+
let tmp = this.a[i]
25+
a[i] = a[max]
26+
a[max] = tmp
27+
}
28+
29+
public removeMax(): any {
30+
if (this.count == 0) {
31+
return -1
32+
}
33+
this.a[1] = this.a[this.count]
34+
--this.count
35+
this.heapify(this.a, this.count, 1)
36+
}
37+
38+
public heapify(a: Array<number>, n: number, i: number) {
39+
while (true) {
40+
let maxPos = i
41+
if (i * 2 < this.n && a[i] < a[i * 2]) {
42+
maxPos = i * 2
43+
}
44+
if (i * 2 + 1 <= n && a[maxPos] < a[i * 2 + 1]) {
45+
maxPos = i * 2 + 1
46+
}
47+
if (maxPos == i) {
48+
break
49+
}
50+
this.swap(a, i, maxPos)
51+
}
52+
}
53+
54+
public buildHelp(a: Array<number>, n: number): void {
55+
for (let i = Math.floor(n / 2); i >= 1; --i) {
56+
this.heapify(a, n, i)
57+
}
58+
}
59+
60+
public sort(a: Array<number>, n: number) {
61+
this.buildHelp(a, n)
62+
let k = n
63+
while (k > 1) {
64+
this.swap(a, 1, k)
65+
--k;
66+
this.heapify(a, k, 1)
67+
}
68+
}
69+
}

src/lib/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {QuickSort} from './QuickSort'
1010
import {CountingSort} from './CountingSort'
1111
import {BinarySearch} from './BinarySearch'
1212
import {Nodes} from './Nodes'
13+
import {Heap} from './Heap'
1314

1415
export default {
1516
LinkedList,
@@ -24,5 +25,6 @@ export default {
2425
CountingSort,
2526
BinarySearch,
2627
Nodes,
28+
Heap,
2729
}
2830

0 commit comments

Comments
 (0)