nlptoolkit-datastructure 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ export declare class Heap<T> {
2
+ private readonly array;
3
+ protected comparator: <T>(item1: T, item2: T) => number;
4
+ private count;
5
+ private n;
6
+ constructor(N: number, comparator: <T>(item1: T, item2: T) => number);
7
+ compare(data1: T, data2: T): number;
8
+ isEmpty(): boolean;
9
+ private swapNode;
10
+ protected percolateDown(no: number): void;
11
+ protected percolateUp(no: number): void;
12
+ delete(): T;
13
+ insert(data: T): void;
14
+ }
@@ -0,0 +1,77 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./HeapNode"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Heap = void 0;
13
+ const HeapNode_1 = require("./HeapNode");
14
+ class Heap {
15
+ constructor(N, comparator) {
16
+ this.comparator = comparator;
17
+ this.array = new Array();
18
+ this.count = 0;
19
+ this.n = N;
20
+ for (let i = 0; i < N; i++) {
21
+ this.array.push();
22
+ }
23
+ }
24
+ compare(data1, data2) {
25
+ return 0;
26
+ }
27
+ isEmpty() {
28
+ return this.count == 0;
29
+ }
30
+ swapNode(index1, index2) {
31
+ let tmp = this.array[index1];
32
+ this.array[index1] = this.array[index2];
33
+ this.array[index2] = tmp;
34
+ }
35
+ percolateDown(no) {
36
+ let left = 2 * no + 1;
37
+ let right = 2 * no + 2;
38
+ while ((left < this.count && this.compare(this.array[no].getData(), this.array[left].getData()) < 0) ||
39
+ (right < this.count && this.compare(this.array[no].getData(), this.array[right].getData()) < 0)) {
40
+ if (right >= this.count || this.compare(this.array[left].getData(), this.array[right].getData()) > 0) {
41
+ this.swapNode(no, left);
42
+ no = left;
43
+ }
44
+ else {
45
+ this.swapNode(no, right);
46
+ no = right;
47
+ }
48
+ left = 2 * no + 1;
49
+ right = 2 * no + 2;
50
+ }
51
+ }
52
+ percolateUp(no) {
53
+ let parent = Math.floor((no - 1) / 2);
54
+ while (parent >= 0 && this.compare(this.array[parent].getData(), this.array[no].getData()) < 0) {
55
+ this.swapNode(parent, no);
56
+ no = parent;
57
+ parent = Math.floor((no - 1) / 2);
58
+ }
59
+ }
60
+ delete() {
61
+ let tmp = this.array[0];
62
+ this.array[0] = this.array[this.count - 1];
63
+ this.percolateDown(0);
64
+ this.count = this.count - 1;
65
+ return tmp.getData();
66
+ }
67
+ insert(data) {
68
+ if (this.count < this.n) {
69
+ this.count = this.count + 1;
70
+ }
71
+ this.array[this.count - 1] = new HeapNode_1.HeapNode(data);
72
+ this.percolateUp(this.count - 1);
73
+ }
74
+ }
75
+ exports.Heap = Heap;
76
+ });
77
+ //# sourceMappingURL=Heap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Heap.js","sourceRoot":"","sources":["../../source/heap/Heap.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,yCAAoC;IAEpC,MAAa,IAAI;QAOb,YAAY,CAAS,EAAE,UAA6C;YAChE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;YAC5B,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,EAAe,CAAA;YACrC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAA;YACd,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;YACV,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAC;gBACvB,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAA;aACpB;QACL,CAAC;QAED,OAAO,CAAC,KAAQ,EAAE,KAAQ;YACtB,OAAO,CAAC,CAAC;QACb,CAAC;QAEM,OAAO;YACV,OAAO,IAAI,CAAC,KAAK,IAAI,C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;aAC9B;YACD,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,IAAI,mBAAQ,CAAI,IAAI,CAAC,CAAA;YAClD,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAA;QACpC,CAAC;KAEJ;IAzED,oBAyEC"}
@@ -0,0 +1,5 @@
1
+ export declare class HeapNode<T> {
2
+ data: T;
3
+ constructor(data: T);
4
+ getData(): T;
5
+ }
@@ -0,0 +1,23 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.HeapNode = void 0;
13
+ class HeapNode {
14
+ constructor(data) {
15
+ this.data = data;
16
+ }
17
+ getData() {
18
+ return this.data;
19
+ }
20
+ }
21
+ exports.HeapNode = HeapNode;
22
+ });
23
+ //# sourceMappingURL=HeapNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HeapNode.js","sourceRoot":"","sources":["../../source/heap/HeapNode.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,MAAa,QAAQ;QAIjB,YAAY,IAAO;YACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QACpB,CAAC;QAEM,OAAO;YACV,OAAO,IAAI,CAAC,IAAI,CAAA;QACpB,CAAC;KAEJ;IAZD,4BAYC"}
@@ -0,0 +1,5 @@
1
+ import { Heap } from "./Heap";
2
+ export declare class MaxHeap<T> extends Heap<T> {
3
+ constructor(N: number, comparator: <T>(item1: T, item2: T) => number);
4
+ compare(data1: T, data2: T): number;
5
+ }
@@ -0,0 +1,24 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./Heap"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MaxHeap = void 0;
13
+ const Heap_1 = require("./Heap");
14
+ class MaxHeap extends Heap_1.Heap {
15
+ constructor(N, comparator) {
16
+ super(N, comparator);
17
+ }
18
+ compare(data1, data2) {
19
+ return this.comparator(data1, data2);
20
+ }
21
+ }
22
+ exports.MaxHeap = MaxHeap;
23
+ });
24
+ //# sourceMappingURL=MaxHeap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MaxHeap.js","sourceRoot":"","sources":["../../source/heap/MaxHeap.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,iCAA4B;IAG5B,MAAa,OAAW,SAAQ,WAAO;QAEnC,YAAY,CAAS,EAAE,UAA6C;YAChE,KAAK,CAAC,CAAC,EAAE,UAAU,CAAC,CAAA;QACxB,CAAC;QAED,OAAO,CAAC,KAAQ,EAAE,KAAQ;YACtB,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;QACxC,CAAC;KAEJ;IAVD,0BAUC"}
@@ -0,0 +1,5 @@
1
+ import { Heap } from "./Heap";
2
+ export declare class MinHeap<T> extends Heap<T> {
3
+ constructor(N: number, comparator: <T>(item1: T, item2: T) => number);
4
+ compare(data1: T, data2: T): number;
5
+ }
@@ -0,0 +1,24 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./Heap"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MinHeap = void 0;
13
+ const Heap_1 = require("./Heap");
14
+ class MinHeap extends Heap_1.Heap {
15
+ constructor(N, comparator) {
16
+ super(N, comparator);
17
+ }
18
+ compare(data1, data2) {
19
+ return -this.comparator(data1, data2);
20
+ }
21
+ }
22
+ exports.MinHeap = MinHeap;
23
+ });
24
+ //# sourceMappingURL=MinHeap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MinHeap.js","sourceRoot":"","sources":["../../source/heap/MinHeap.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,iCAA4B;IAE5B,MAAa,OAAW,SAAQ,WAAO;QAEnC,YAAY,CAAS,EAAE,UAA6C;YAChE,KAAK,CAAC,CAAC,EAAE,UAAU,CAAC,CAAA;QACxB,CAAC;QAED,OAAO,CAAC,KAAQ,EAAE,KAAQ;YACtB,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;QACzC,CAAC;KAEJ;IAVD,0BAUC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nlptoolkit-datastructure",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Simple Data Structures Library",
5
5
  "main": "index.js",
6
6
  "types": "index.js",
@@ -0,0 +1,77 @@
1
+ import {HeapNode} from "./HeapNode";
2
+
3
+ export class Heap<T> {
4
+
5
+ private readonly array: Array<HeapNode<T>>
6
+ protected comparator : <T>(item1: T, item2: T) => number
7
+ private count: number
8
+ private n: number
9
+
10
+ constructor(N: number, comparator: <T>(item1: T, item2: T) => number){
11
+ this.comparator = comparator
12
+ this.array = new Array<HeapNode<T>>()
13
+ this.count = 0
14
+ this.n = N
15
+ for (let i = 0; i < N; i++){
16
+ this.array.push()
17
+ }
18
+ }
19
+
20
+ compare(data1: T, data2: T): number{
21
+ return 0;
22
+ }
23
+
24
+ public isEmpty(): boolean{
25
+ return this.count == 0
26
+ }
27
+
28
+ private swapNode(index1: number, index2: number){
29
+ let tmp = this.array[index1]
30
+ this.array[index1] = this.array[index2]
31
+ this.array[index2] = tmp
32
+ }
33
+
34
+ protected percolateDown(no: number) {
35
+ let left = 2 * no + 1
36
+ let right = 2 * no + 2
37
+ while ((left < this.count && this.compare(this.array[no].getData(), this.array[left].getData()) < 0) ||
38
+ (right < this.count && this.compare(this.array[no].getData(), this.array[right].getData()) < 0)) {
39
+ if (right >= this.count || this.compare(this.array[left].getData(), this.array[right].getData()) > 0) {
40
+ this.swapNode(no, left)
41
+ no = left
42
+ } else {
43
+ this.swapNode(no, right)
44
+ no = right
45
+ }
46
+ left = 2 * no + 1
47
+ right = 2 * no + 2
48
+ }
49
+ }
50
+
51
+ protected percolateUp(no: number){
52
+ let parent = Math.floor((no - 1) / 2)
53
+ while (parent >= 0 && this.compare(this.array[parent].getData(), this.array[no].getData()) < 0){
54
+ this.swapNode(parent, no)
55
+ no = parent
56
+ parent = Math.floor((no - 1) / 2)
57
+ }
58
+ }
59
+
60
+ public delete(): T{
61
+ let tmp = this.array[0]
62
+ this.array[0] = this.array[this.count - 1]
63
+ this.percolateDown(0)
64
+ this.count = this.count - 1
65
+ return tmp.getData()
66
+ }
67
+
68
+ public insert(data: T){
69
+ if (this.count < this.n) {
70
+ this.count = this.count + 1
71
+ }
72
+ this.array[this.count - 1] = new HeapNode<T>(data)
73
+ this.percolateUp(this.count - 1)
74
+ }
75
+
76
+ }
77
+
@@ -0,0 +1,13 @@
1
+ export class HeapNode<T> {
2
+
3
+ data : T
4
+
5
+ constructor(data: T){
6
+ this.data = data
7
+ }
8
+
9
+ public getData(): T{
10
+ return this.data
11
+ }
12
+
13
+ }
@@ -0,0 +1,14 @@
1
+ import {Heap} from "./Heap";
2
+ import {HeapNode} from "./HeapNode";
3
+
4
+ export class MaxHeap<T> extends Heap<T>{
5
+
6
+ constructor(N: number, comparator: <T>(item1: T, item2: T) => number){
7
+ super(N, comparator)
8
+ }
9
+
10
+ compare(data1: T, data2: T): number {
11
+ return this.comparator(data1, data2)
12
+ }
13
+
14
+ }
@@ -0,0 +1,13 @@
1
+ import {Heap} from "./Heap";
2
+
3
+ export class MinHeap<T> extends Heap<T>{
4
+
5
+ constructor(N: number, comparator: <T>(item1: T, item2: T) => number){
6
+ super(N, comparator)
7
+ }
8
+
9
+ compare(data1: T, data2: T): number {
10
+ return -this.comparator(data1, data2)
11
+ }
12
+
13
+ }
@@ -0,0 +1,40 @@
1
+ import {MaxHeap} from "../dist/heap/MaxHeap";
2
+ import * as assert from "assert";
3
+ import {MinHeap} from "../dist/heap/MinHeap";
4
+
5
+ describe('HeapTest', function() {
6
+ describe('HeapTest', function() {
7
+ function compare<T>(item1: T, item2: T): number {
8
+ if (typeof item1 == "number" && typeof item2 == "number"){
9
+ return item1 - item2
10
+ }
11
+ return 0
12
+ }
13
+ it('testMaxHeap', function() {
14
+ let maxHeap : MaxHeap<number> = new MaxHeap<number>(8, compare)
15
+ maxHeap.insert(4)
16
+ maxHeap.insert(6)
17
+ maxHeap.insert(2)
18
+ maxHeap.insert(5)
19
+ maxHeap.insert(3)
20
+ maxHeap.insert(1)
21
+ maxHeap.insert(7)
22
+ assert.equal(7, maxHeap.delete())
23
+ assert.equal(6, maxHeap.delete())
24
+ assert.equal(5, maxHeap.delete())
25
+ });
26
+ it('testMinHeap', function() {
27
+ let minHeap : MinHeap<number> = new MinHeap<number>(8, compare)
28
+ minHeap.insert(4)
29
+ minHeap.insert(6)
30
+ minHeap.insert(2)
31
+ minHeap.insert(5)
32
+ minHeap.insert(3)
33
+ minHeap.insert(1)
34
+ minHeap.insert(7)
35
+ assert.equal(1, minHeap.delete())
36
+ assert.equal(2, minHeap.delete())
37
+ assert.equal(3, minHeap.delete())
38
+ });
39
+ });
40
+ });