nlptoolkit-datastructure 1.0.0 → 1.0.1

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,7 @@
1
+ export declare class Stack<T> {
2
+ private list;
3
+ constructor();
4
+ push(item: T): void;
5
+ pop(): T | null;
6
+ isEmpty(): boolean;
7
+ }
package/dist/Stack.js ADDED
@@ -0,0 +1,35 @@
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.Stack = void 0;
13
+ class Stack {
14
+ constructor() {
15
+ this.list = [];
16
+ }
17
+ push(item) {
18
+ this.list.push(item);
19
+ }
20
+ pop() {
21
+ let item = this.list.pop();
22
+ if (item == undefined) {
23
+ return null;
24
+ }
25
+ else {
26
+ return item;
27
+ }
28
+ }
29
+ isEmpty() {
30
+ return this.list.length == 0;
31
+ }
32
+ }
33
+ exports.Stack = Stack;
34
+ });
35
+ //# sourceMappingURL=Stack.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Stack.js","sourceRoot":"","sources":["../source/Stack.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,MAAa,KAAK;QAId;YAFQ,SAAI,GAAc,EAAE,CAAA;QAG5B,CAAC;QAEM,IAAI,CAAC,IAAO;YACf,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACzB,CAAC;QAEM,GAAG;YACN,IAAI,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAA;YAC1B,IAAI,IAAI,IAAI,SAAS,EAAC;gBAClB,OAAO,IAAI,CAAA;aACd;iBAAM;gBACH,OAAO,IAAI,CAAA;aACd;QACL,CAAC;QAEM,OAAO;YACV,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,CAAA;QAChC,CAAC;KAGJ;IAzBD,sBAyBC"}
@@ -0,0 +1,12 @@
1
+ import { Tree } from "./Tree";
2
+ import { AvlTreeNode } from "./AvlTreeNode";
3
+ export declare class AvlTree<T> extends Tree<T> {
4
+ constructor(comparator: <T>(item1: T, item2: T) => number);
5
+ height(d: AvlTreeNode<T>): number;
6
+ private rotateLeft;
7
+ private rotateRight;
8
+ private doubleRotateLeft;
9
+ private doubleRotateRight;
10
+ insert(node: AvlTreeNode<T>): void;
11
+ insertData(item: T): void;
12
+ }
@@ -0,0 +1,107 @@
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", "./Tree", "./AvlTreeNode", "../Stack"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.AvlTree = void 0;
13
+ const Tree_1 = require("./Tree");
14
+ const AvlTreeNode_1 = require("./AvlTreeNode");
15
+ const Stack_1 = require("../Stack");
16
+ class AvlTree extends Tree_1.Tree {
17
+ constructor(comparator) {
18
+ super(comparator);
19
+ }
20
+ height(d) {
21
+ if (d == null) {
22
+ return 0;
23
+ }
24
+ else {
25
+ return d.height;
26
+ }
27
+ }
28
+ rotateLeft(k2) {
29
+ let k1 = k2.left;
30
+ k2.left = k1.right;
31
+ k1.right = k2;
32
+ k2.height = Math.max(this.height(k2.left), this.height(k2.right)) + 1;
33
+ k1.height = Math.max(this.height(k1.left), k1.right.height) + 1;
34
+ return k1;
35
+ }
36
+ rotateRight(k1) {
37
+ let k2 = k1.right;
38
+ k1.right = k2.left;
39
+ k2.left = k1;
40
+ k2.height = Math.max(k2.left.height, this.height(k2.right)) + 1;
41
+ k1.height = Math.max(this.height(k1.left), this.height(k1.right)) + 1;
42
+ return k2;
43
+ }
44
+ doubleRotateLeft(k3) {
45
+ k3.left = this.rotateRight(k3.left);
46
+ return this.rotateLeft(k3);
47
+ }
48
+ doubleRotateRight(k1) {
49
+ k1.right = this.rotateLeft(k1.right);
50
+ return this.rotateRight(k1);
51
+ }
52
+ insert(node) {
53
+ let LEFT = 1, RIGHT = 2;
54
+ let y = null;
55
+ let x = this.root;
56
+ let t;
57
+ let dir1 = 0, dir2 = 0;
58
+ let c = new Stack_1.Stack();
59
+ while (x != null) {
60
+ y = x;
61
+ c.push(y);
62
+ dir1 = dir2;
63
+ if (this.comparator(node.data, x.data) < 0) {
64
+ x = x.left;
65
+ dir2 = LEFT;
66
+ }
67
+ else {
68
+ x = x.right;
69
+ dir2 = RIGHT;
70
+ }
71
+ }
72
+ this.insertChild(y, node);
73
+ while (!c.isEmpty()) {
74
+ x = c.pop();
75
+ if (x != null) {
76
+ x.height = Math.max(this.height(x.left), this.height(x.right)) + 1;
77
+ if (Math.abs(this.height(x.left) - this.height(x.right)) == 2) {
78
+ if (dir1 == LEFT) {
79
+ if (dir2 == LEFT) {
80
+ t = this.rotateLeft(x);
81
+ }
82
+ else {
83
+ t = this.doubleRotateLeft(x);
84
+ }
85
+ }
86
+ else {
87
+ if (dir2 == LEFT) {
88
+ t = this.doubleRotateRight(x);
89
+ }
90
+ else {
91
+ t = this.rotateRight(x);
92
+ }
93
+ }
94
+ y = c.pop();
95
+ this.insertChild(y, t);
96
+ break;
97
+ }
98
+ }
99
+ }
100
+ }
101
+ insertData(item) {
102
+ this.insert(new AvlTreeNode_1.AvlTreeNode(item));
103
+ }
104
+ }
105
+ exports.AvlTree = AvlTree;
106
+ });
107
+ //# sourceMappingURL=AvlTree.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AvlTree.js","sourceRoot":"","sources":["../../source/tree/AvlTree.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,iCAA4B;IAC5B,+CAA0C;IAC1C,oC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;IA5FD,0BA4FC"}
@@ -0,0 +1,5 @@
1
+ import { TreeNode } from "./TreeNode";
2
+ export declare class AvlTreeNode<T> extends TreeNode<T> {
3
+ height: number;
4
+ constructor(data: T);
5
+ }
@@ -0,0 +1,22 @@
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", "./TreeNode"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.AvlTreeNode = void 0;
13
+ const TreeNode_1 = require("./TreeNode");
14
+ class AvlTreeNode extends TreeNode_1.TreeNode {
15
+ constructor(data) {
16
+ super(data);
17
+ this.height = 1;
18
+ }
19
+ }
20
+ exports.AvlTreeNode = AvlTreeNode;
21
+ });
22
+ //# sourceMappingURL=AvlTreeNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AvlTreeNode.js","sourceRoot":"","sources":["../../source/tree/AvlTreeNode.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,yCAAoC;IAEpC,MAAa,WAAe,SAAQ,mBAAW;QAI3C,YAAa,IAAO;YAChB,KAAK,CAAC,IAAI,CAAC,CAAA;YACX,IAAI,CAAC,MAAM,GAAG,CAAC,CAAA;QACnB,CAAC;KAEJ;IATD,kCASC"}
@@ -0,0 +1,9 @@
1
+ import { BTreeNode } from "./BTreeNode";
2
+ export declare class BTree<T> {
3
+ root: BTreeNode<T> | null;
4
+ comparator: <T>(item1: T, item2: T) => number;
5
+ d: number;
6
+ constructor(d: number, comparator: <T>(item1: T, item2: T) => number);
7
+ search(value: T): BTreeNode<T> | null;
8
+ insertData(data: T): void;
9
+ }
@@ -0,0 +1,58 @@
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", "./BTreeNode"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.BTree = void 0;
13
+ const BTreeNode_1 = require("./BTreeNode");
14
+ class BTree {
15
+ constructor(d, comparator) {
16
+ this.root = null;
17
+ this.comparator = comparator;
18
+ this.d = d;
19
+ }
20
+ search(value) {
21
+ let b = this.root;
22
+ while (b != null && !b.leaf) {
23
+ let child = b.position(value, this.comparator);
24
+ if (child < b.m && b.K[child] == value) {
25
+ return b;
26
+ }
27
+ b = b.children[child];
28
+ }
29
+ if (b != null) {
30
+ let child = b.position(value, this.comparator);
31
+ if (child < b.m && b.K[child] == value) {
32
+ return b;
33
+ }
34
+ }
35
+ return null;
36
+ }
37
+ insertData(data) {
38
+ if (this.root == null) {
39
+ this.root = new BTreeNode_1.BTreeNode(this.d, undefined, undefined, undefined);
40
+ }
41
+ if (this.root.leaf) {
42
+ let s = this.root.insertLeaf(data, this.comparator);
43
+ if (s != null) {
44
+ let tmp = this.root;
45
+ this.root = new BTreeNode_1.BTreeNode(this.d, tmp, s, tmp.K[this.d]);
46
+ }
47
+ }
48
+ else {
49
+ let s = this.root.insertNode(data, this.comparator, true);
50
+ if (s != null) {
51
+ this.root = s;
52
+ }
53
+ }
54
+ }
55
+ }
56
+ exports.BTree = BTree;
57
+ });
58
+ //# sourceMappingURL=BTree.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BTree.js","sourceRoot":"","sources":["../../source/tree/BTree.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,2CAAsC;IAEtC,MAAa,KAAK;QAMd,YAAY,CAAS,EAAE,UAA6C;YAJpE,SAAI,GAAyB,IAAI,CAAC;YAK9B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;YAC5B,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;QACd,CAAC;QAEM,MAAM,CAAC,KAAQ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AAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;iBAC/D;aACJ;iBAAM;gBACH,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;gBAC1D,IAAI,CAAC,IAAI,IAAI,EAAC;oBACV,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;iBACjB;aACJ;QACL,CAAC;KAEJ;IA/CD,sBA+CC"}
@@ -0,0 +1,15 @@
1
+ export declare class BTreeNode<T> {
2
+ K: Array<T>;
3
+ m: number;
4
+ d: number;
5
+ leaf: boolean;
6
+ children: Array<BTreeNode<T>>;
7
+ constructor(d: number, firstChild: BTreeNode<T> | undefined, secondChild: BTreeNode<T> | undefined, newK: T | undefined);
8
+ position(value: T, comparator: <T>(item1: T, item2: T) => number): number;
9
+ private insertIntoK;
10
+ private moveHalfOfTheKToNewNode;
11
+ private moveHalfOfTheChildrenToNewNode;
12
+ private moveHalfOfTheElementsToNewNode;
13
+ insertNode(value: T, comparator: <T>(item1: T, item2: T) => number, isRoot: boolean): BTreeNode<T> | null;
14
+ insertLeaf(value: T, comparator: <T>(item1: T, item2: T) => number): BTreeNode<T> | null;
15
+ }
@@ -0,0 +1,122 @@
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.BTreeNode = void 0;
13
+ class BTreeNode {
14
+ constructor(d, firstChild, secondChild, newK) {
15
+ this.d = d;
16
+ this.K = [];
17
+ this.children = [];
18
+ if (firstChild == undefined) {
19
+ this.m = 0;
20
+ this.leaf = true;
21
+ }
22
+ else {
23
+ this.m = 1;
24
+ this.leaf = false;
25
+ this.children[0] = firstChild;
26
+ if (secondChild != undefined) {
27
+ this.children[1] = secondChild;
28
+ }
29
+ if (newK != undefined) {
30
+ this.K[0] = newK;
31
+ }
32
+ }
33
+ }
34
+ position(value, comparator) {
35
+ if (this.m == 0) {
36
+ return 0;
37
+ }
38
+ if (comparator(value, this.K[this.m - 1]) > 0) {
39
+ return this.m;
40
+ }
41
+ else {
42
+ for (let i = 0; i < this.m; i++) {
43
+ if (comparator(value, this.K[i]) <= 0) {
44
+ return i;
45
+ }
46
+ }
47
+ }
48
+ return -1;
49
+ }
50
+ insertIntoK(index, insertedK) {
51
+ for (let i = this.m; i > index; i--) {
52
+ this.K[i] = this.K[i - 1];
53
+ }
54
+ this.K[index] = insertedK;
55
+ }
56
+ moveHalfOfTheKToNewNode(newNode) {
57
+ for (let i = 0; i < this.d; i++) {
58
+ newNode.K[i] = this.K[i + this.d + 1];
59
+ }
60
+ newNode.m = this.d;
61
+ }
62
+ moveHalfOfTheChildrenToNewNode(newNode) {
63
+ for (let i = 0; i < this.d; i++) {
64
+ newNode.children[i] = this.children[i + this.d + 1];
65
+ }
66
+ }
67
+ moveHalfOfTheElementsToNewNode(newNode) {
68
+ this.moveHalfOfTheKToNewNode(newNode);
69
+ this.moveHalfOfTheChildrenToNewNode(newNode);
70
+ }
71
+ insertNode(value, comparator, isRoot) {
72
+ let s;
73
+ let newNode;
74
+ let child;
75
+ child = this.position(value, comparator);
76
+ if (!this.children[child].leaf) {
77
+ s = this.children[child].insertNode(value, comparator, false);
78
+ }
79
+ else {
80
+ s = this.children[child].insertLeaf(value, comparator);
81
+ }
82
+ if (s == null) {
83
+ return null;
84
+ }
85
+ this.insertIntoK(child, this.children[child].K[this.d]);
86
+ if (this.m < 2 * this.d) {
87
+ this.children[child + 1] = s;
88
+ this.m++;
89
+ return null;
90
+ }
91
+ else {
92
+ newNode = new BTreeNode(this.d, undefined, undefined, undefined);
93
+ newNode.leaf = false;
94
+ this.moveHalfOfTheElementsToNewNode(newNode);
95
+ newNode.children[this.d] = s;
96
+ this.m = this.d;
97
+ if (isRoot) {
98
+ return new BTreeNode(this.d, this, newNode, this.K[this.d]);
99
+ }
100
+ else {
101
+ return newNode;
102
+ }
103
+ }
104
+ }
105
+ insertLeaf(value, comparator) {
106
+ let child = this.position(value, comparator);
107
+ this.insertIntoK(child, value);
108
+ if (this.m < 2 * this.d) {
109
+ this.m++;
110
+ return null;
111
+ }
112
+ else {
113
+ let newNode = new BTreeNode(this.d, undefined, undefined, undefined);
114
+ this.moveHalfOfTheKToNewNode(newNode);
115
+ this.m = this.d;
116
+ return newNode;
117
+ }
118
+ }
119
+ }
120
+ exports.BTreeNode = BTreeNode;
121
+ });
122
+ //# sourceMappingURL=BTreeNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BTreeNode.js","sourceRoot":"","sources":["../../source/tree/BTreeNode.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,MAAa,SAAS;QAQlB,YAAY,CAAS,EAAE,UAAoC,EAAE,WAAqC,EAAE,IAAoB;YACpH,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;YACV,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC;YACZ,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;YACnB,IAAI,UAAU,IAAI,SAAS,EAAC;gBACxB,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;gBACV,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;aACnB;iBAAM;gBACH,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;gBACV,IAAI,CAAC,IAAI,GAAG,KAAK,CAAA;gBACjB,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC;gBAC9B,IAAI,WAAW,IAAI,SAAS,EAAC;oBACzB,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC;iBAClC;gBACD,IAAI,IAAI,IAAI,SAAS,EAAC;oBAClB,IAAI,CAAC,C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,CAAC,CAAC,CAAC;gBAChB,OAAO,OAAO,CAAC;aAClB;QACL,CAAC;KAEJ;IAnHD,8BAmHC"}
@@ -0,0 +1,10 @@
1
+ import { TreeNode } from "./TreeNode";
2
+ export declare class Tree<T> {
3
+ protected root: TreeNode<T> | null;
4
+ protected comparator: <T>(item1: T, item2: T) => number;
5
+ constructor(comparator: <T>(item1: T, item2: T) => number);
6
+ search(value: T): TreeNode<T> | null;
7
+ protected insertChild(parent: TreeNode<T> | null, child: TreeNode<T>): void;
8
+ insert(node: TreeNode<T>): void;
9
+ insertData(data: T): void;
10
+ }
@@ -0,0 +1,69 @@
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", "./TreeNode"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Tree = void 0;
13
+ const TreeNode_1 = require("./TreeNode");
14
+ class Tree {
15
+ constructor(comparator) {
16
+ this.root = null;
17
+ this.comparator = comparator;
18
+ }
19
+ search(value) {
20
+ let d = this.root;
21
+ while (d != null) {
22
+ if (this.comparator(d.data, value) == 0) {
23
+ return d;
24
+ }
25
+ else {
26
+ if (this.comparator(d.data, value) > 0) {
27
+ d = d.left;
28
+ }
29
+ else {
30
+ d = d.right;
31
+ }
32
+ }
33
+ }
34
+ return null;
35
+ }
36
+ insertChild(parent, child) {
37
+ if (parent == null) {
38
+ this.root = child;
39
+ }
40
+ else {
41
+ if (this.comparator(child.data, parent.data) < 0) {
42
+ parent.left = child;
43
+ }
44
+ else {
45
+ parent.right = child;
46
+ }
47
+ }
48
+ }
49
+ insert(node) {
50
+ let y = null;
51
+ let x = this.root;
52
+ while (x != null) {
53
+ y = x;
54
+ if (this.comparator(node.data, x.data) < 0) {
55
+ x = x.left;
56
+ }
57
+ else {
58
+ x = x.right;
59
+ }
60
+ }
61
+ this.insertChild(y, node);
62
+ }
63
+ insertData(data) {
64
+ this.insert(new TreeNode_1.TreeNode(data));
65
+ }
66
+ }
67
+ exports.Tree = Tree;
68
+ });
69
+ //# sourceMappingURL=Tree.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Tree.js","sourceRoot":"","sources":["../../source/tree/Tree.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,yCAAoC;IAEpC,MAAa,IAAI;QAKb,YAAY,UAA6C;YAH/C,SAAI,GAAwB,IAAI,CAAA;YAItC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAChC,CAAC;QAEM,MAAM,CAAC,KAAQ;YAClB,IAAI,CAAC,GAAwB,IAAI,CAAC,IAAI,CAAA;YACtC,OAAO,CAAC,IAAI,IAAI,EAAC;gBACb,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,EAAC;oBACpC,OAAO,CAAC,CAAA;iBACX;qBAAM;oBACH,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC,EAAC;wBACnC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAA;qBACb;yBAAM;wBACH,CAAC,GAAG,CAAC,CAAC,KAAK,CAAA;qBACd;iBACJ;aACJ;YACD,OAAO,IAAI,CAAA;QACf,CAAC;QAES,WAAW,CAAC,MAA2B,EAAE,KAAkB;YACjE,IAAI,MAAM,IAAI,IAAI,EAAE;gBAChB,IAAI,CAAC,IAAI,GAAG,KAAK,CAAA;aACpB;iBAAM;gBACH,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;oBAC9C,MAAM,CAAC,IAAI,GAAG,KAAK,CAAA;iBACtB;qBAAM;oBACH,MAAM,CAAC,KAAK,GAAG,KAAK,CAAA;iBACvB;aACJ;QACL,CAAC;QAEM,MAAM,CAAC,IAAiB;YAC3B,IAAI,CAAC,GAAwB,IAAI,CAAA;YACjC,IAAI,CAAC,GAAwB,IAAI,CAAC,IAAI,CAAC;YACvC,OAAO,CAAC,IAAI,IAAI,EAAC;gBACb,CAAC,GAAG,CAAC,CAAC;gBACN,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAC;oBACvC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;iBACd;qBAAM;oBACH,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC;iBACf;aACJ;YACD,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;QAC9B,CAAC;QAEM,UAAU,CAAC,IAAO;YACrB,IAAI,CAAC,MAAM,CAAC,IAAI,mBAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;QACpC,CAAC;KAEJ;IAvDD,oBAuDC"}
@@ -0,0 +1,6 @@
1
+ export declare class TreeNode<T> {
2
+ data: T;
3
+ left: TreeNode<T> | null;
4
+ right: TreeNode<T> | null;
5
+ constructor(data: T);
6
+ }
@@ -0,0 +1,22 @@
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.TreeNode = void 0;
13
+ class TreeNode {
14
+ constructor(data) {
15
+ this.left = null;
16
+ this.right = null;
17
+ this.data = data;
18
+ }
19
+ }
20
+ exports.TreeNode = TreeNode;
21
+ });
22
+ //# sourceMappingURL=TreeNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TreeNode.js","sourceRoot":"","sources":["../../source/tree/TreeNode.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,MAAa,QAAQ;QAMjB,YAAY,IAAO;YAHnB,SAAI,GAAwB,IAAI,CAAA;YAChC,UAAK,GAAwB,IAAI,CAAA;YAG7B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QACpB,CAAC;KAEJ;IAVD,4BAUC"}
package/index.js CHANGED
@@ -1,2 +1,9 @@
1
1
  export * from "./dist/CounterHashMap"
2
- export * from "./dist/LRUCache"
2
+ export * from "./dist/LRUCache"
3
+ export * from "./dist/Stack"
4
+ export * from "./dist/tree/BTree"
5
+ export * from "./dist/tree/BTreeNode"
6
+ export * from "./dist/tree/AvlTree"
7
+ export * from "./dist/tree/AvlTreeNode"
8
+ export * from "./dist/tree/Tree"
9
+ export * from "./dist/tree/TreeNode"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nlptoolkit-datastructure",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Simple Data Structures Library",
5
5
  "main": "index.js",
6
6
  "types": "index.js",
@@ -0,0 +1,26 @@
1
+ export class Stack<T> {
2
+
3
+ private list : Array<T> = []
4
+
5
+ constructor() {
6
+ }
7
+
8
+ public push(item: T){
9
+ this.list.push(item);
10
+ }
11
+
12
+ public pop(): T | null{
13
+ let item = this.list.pop()
14
+ if (item == undefined){
15
+ return null
16
+ } else {
17
+ return item
18
+ }
19
+ }
20
+
21
+ public isEmpty(): boolean{
22
+ return this.list.length == 0
23
+ }
24
+
25
+
26
+ }
@@ -0,0 +1,97 @@
1
+ import {Tree} from "./Tree";
2
+ import {AvlTreeNode} from "./AvlTreeNode";
3
+ import {Stack} from "../Stack";
4
+
5
+ export class AvlTree<T> extends Tree<T>{
6
+
7
+ constructor(comparator: <T>(item1: T, item2: T) => number){
8
+ super(comparator)
9
+ }
10
+
11
+ public height(d: AvlTreeNode<T>): number{
12
+ if (d == null){
13
+ return 0
14
+ } else {
15
+ return d.height
16
+ }
17
+ }
18
+
19
+ private rotateLeft(k2: AvlTreeNode<T>): AvlTreeNode<T>{
20
+ let k1 : AvlTreeNode<T> = <AvlTreeNode<T>> k2.left
21
+ k2.left = k1.right
22
+ k1.right = k2
23
+ k2.height = Math.max(this.height(<AvlTreeNode<T>> k2.left), this.height(<AvlTreeNode<T>> k2.right)) + 1
24
+ k1.height = Math.max(this.height(<AvlTreeNode<T>> k1.left), (<AvlTreeNode<T>> k1.right).height) + 1
25
+ return k1
26
+ }
27
+
28
+ private rotateRight(k1: AvlTreeNode<T>) : AvlTreeNode<T>{
29
+ let k2 : AvlTreeNode<T> = <AvlTreeNode<T>> k1.right
30
+ k1.right = k2.left
31
+ k2.left = k1
32
+ k2.height = Math.max((<AvlTreeNode<T>> k2.left).height, this.height(<AvlTreeNode<T>> k2.right)) + 1
33
+ k1.height = Math.max(this.height(<AvlTreeNode<T>> k1.left), this.height(<AvlTreeNode<T>> k1.right)) + 1
34
+ return k2
35
+ }
36
+
37
+ private doubleRotateLeft(k3: AvlTreeNode<T>): AvlTreeNode<T>{
38
+ k3.left = this.rotateRight(<AvlTreeNode<T>> k3.left)
39
+ return this.rotateLeft(k3)
40
+ }
41
+
42
+ private doubleRotateRight(k1: AvlTreeNode<T>): AvlTreeNode<T>{
43
+ k1.right = this.rotateLeft(<AvlTreeNode<T>> k1.right)
44
+ return this.rotateRight(k1)
45
+ }
46
+
47
+ public insert(node : AvlTreeNode<T>){
48
+ let LEFT = 1, RIGHT = 2
49
+ let y : AvlTreeNode<T> | null = null
50
+ let x : AvlTreeNode<T> | null = <AvlTreeNode<T>> this.root
51
+ let t : AvlTreeNode<T> | null
52
+ let dir1 = 0, dir2 = 0
53
+ let c : Stack<AvlTreeNode<T>> = new Stack<AvlTreeNode<T>>()
54
+ while (x != null){
55
+ y = x
56
+ c.push(y)
57
+ dir1 = dir2
58
+ if (this.comparator(node.data, x.data) < 0){
59
+ x = <AvlTreeNode<T>> x.left
60
+ dir2 = LEFT
61
+ } else {
62
+ x = <AvlTreeNode<T>> x.right
63
+ dir2 = RIGHT
64
+ }
65
+ }
66
+ this.insertChild(y, node)
67
+ while (!c.isEmpty()){
68
+ x = c.pop()
69
+ if (x != null){
70
+ x.height = Math.max(this.height(<AvlTreeNode<T>> x.left), this.height(<AvlTreeNode<T>> x.right)) + 1;
71
+ if (Math.abs(this.height(<AvlTreeNode<T>> x.left) - this.height(<AvlTreeNode<T>> x.right)) == 2){
72
+ if (dir1 == LEFT){
73
+ if (dir2 == LEFT){
74
+ t = this.rotateLeft(x)
75
+ } else {
76
+ t = this.doubleRotateLeft(x)
77
+ }
78
+ } else {
79
+ if (dir2 == LEFT){
80
+ t = this.doubleRotateRight(x)
81
+ } else {
82
+ t = this.rotateRight(x);
83
+ }
84
+ }
85
+ y = c.pop();
86
+ this.insertChild(y, t);
87
+ break;
88
+ }
89
+ }
90
+ }
91
+ }
92
+
93
+ public insertData( item : T){
94
+ this.insert(new AvlTreeNode<T>(item))
95
+ }
96
+
97
+ }
@@ -0,0 +1,12 @@
1
+ import {TreeNode} from "./TreeNode";
2
+
3
+ export class AvlTreeNode<T> extends TreeNode<T> {
4
+
5
+ height : number
6
+
7
+ constructor (data: T) {
8
+ super(data)
9
+ this.height = 1
10
+ }
11
+
12
+ }
@@ -0,0 +1,50 @@
1
+ import {BTreeNode} from "./BTreeNode";
2
+
3
+ export class BTree<T> {
4
+
5
+ root : BTreeNode<T> | null = null;
6
+ comparator : <T>(item1: T, item2: T) => number
7
+ d : number
8
+
9
+ constructor(d: number, comparator: <T>(item1: T, item2: T) => number){
10
+ this.comparator = comparator
11
+ this.d = d
12
+ }
13
+
14
+ public search(value: T): BTreeNode<T> | null{
15
+ let b = this.root;
16
+ while (b != null && !b.leaf){
17
+ let child = b.position(value, this.comparator)
18
+ if (child < b.m && b.K[child] == value){
19
+ return b
20
+ }
21
+ b = b.children[child]
22
+ }
23
+ if (b != null){
24
+ let child = b.position(value, this.comparator)
25
+ if (child < b.m && b.K[child] == value){
26
+ return b
27
+ }
28
+ }
29
+ return null
30
+ }
31
+
32
+ public insertData(data: T){
33
+ if (this.root == null){
34
+ this.root = new BTreeNode<T>(this.d, undefined, undefined, undefined)
35
+ }
36
+ if (this.root.leaf){
37
+ let s = this.root.insertLeaf(data, this.comparator)
38
+ if (s != null){
39
+ let tmp = this.root
40
+ this.root = new BTreeNode<T>(this.d, tmp, s, tmp.K[this.d]);
41
+ }
42
+ } else {
43
+ let s = this.root.insertNode(data, this.comparator, true);
44
+ if (s != null){
45
+ this.root = s;
46
+ }
47
+ }
48
+ }
49
+
50
+ }
@@ -0,0 +1,116 @@
1
+ export class BTreeNode<T> {
2
+
3
+ K : Array<T>;
4
+ m : number
5
+ d : number
6
+ leaf: boolean
7
+ children : Array<BTreeNode<T>>
8
+
9
+ constructor(d: number, firstChild: BTreeNode<T> | undefined, secondChild: BTreeNode<T> | undefined, newK : T | undefined) {
10
+ this.d = d
11
+ this.K = [];
12
+ this.children = [];
13
+ if (firstChild == undefined){
14
+ this.m = 0
15
+ this.leaf = true
16
+ } else {
17
+ this.m = 1
18
+ this.leaf = false
19
+ this.children[0] = firstChild;
20
+ if (secondChild != undefined){
21
+ this.children[1] = secondChild;
22
+ }
23
+ if (newK != undefined){
24
+ this.K[0] = newK;
25
+ }
26
+ }
27
+ }
28
+
29
+ position(value: T, comparator: <T>(item1: T, item2: T) => number){
30
+ if (this.m == 0){
31
+ return 0
32
+ }
33
+ if (comparator(value, this.K[this.m - 1]) > 0){
34
+ return this.m
35
+ } else {
36
+ for (let i = 0; i < this.m; i++){
37
+ if (comparator(value, this.K[i]) <= 0){
38
+ return i
39
+ }
40
+ }
41
+ }
42
+ return -1
43
+ }
44
+
45
+ private insertIntoK(index: number, insertedK: T){
46
+ for (let i = this.m; i > index; i--){
47
+ this.K[i] = this.K[i - 1]
48
+ }
49
+ this.K[index] = insertedK
50
+ }
51
+
52
+ private moveHalfOfTheKToNewNode(newNode: BTreeNode<T>) {
53
+ for (let i = 0; i < this.d; i++) {
54
+ newNode.K[i] = this.K[i + this.d + 1]
55
+ }
56
+ newNode.m = this.d
57
+ }
58
+
59
+ private moveHalfOfTheChildrenToNewNode(newNode: BTreeNode<T>) {
60
+ for (let i = 0 ; i < this.d; i++){
61
+ newNode.children[i] = this.children[i + this.d + 1];
62
+ }
63
+ }
64
+
65
+ private moveHalfOfTheElementsToNewNode(newNode: BTreeNode<T>){
66
+ this.moveHalfOfTheKToNewNode(newNode);
67
+ this.moveHalfOfTheChildrenToNewNode(newNode);
68
+ }
69
+
70
+ public insertNode(value: T, comparator: <T>(item1: T, item2: T) => number, isRoot: boolean): BTreeNode<T> | null{
71
+ let s : BTreeNode<T> | null
72
+ let newNode : BTreeNode<T>
73
+ let child : number
74
+ child = this.position(value, comparator);
75
+ if (!this.children[child].leaf){
76
+ s = this.children[child].insertNode(value, comparator, false);
77
+ } else {
78
+ s = this.children[child].insertLeaf(value, comparator);
79
+ }
80
+ if (s == null){
81
+ return null
82
+ }
83
+ this.insertIntoK(child, this.children[child].K[this.d])
84
+ if (this.m < 2 * this.d){
85
+ this.children[child + 1] = s
86
+ this.m++;
87
+ return null
88
+ } else {
89
+ newNode = new BTreeNode<T>(this.d, undefined, undefined, undefined);
90
+ newNode.leaf = false
91
+ this.moveHalfOfTheElementsToNewNode(newNode)
92
+ newNode.children[this.d] = s
93
+ this.m = this.d
94
+ if (isRoot){
95
+ return new BTreeNode<T>(this.d, this, newNode, this.K[this.d])
96
+ } else {
97
+ return newNode
98
+ }
99
+ }
100
+ }
101
+
102
+ public insertLeaf(value: T, comparator: <T>(item1: T, item2: T) => number): BTreeNode<T> | null{
103
+ let child = this.position(value, comparator)
104
+ this.insertIntoK(child, value)
105
+ if (this.m < 2 * this.d){
106
+ this.m++;
107
+ return null
108
+ } else {
109
+ let newNode = new BTreeNode<T>(this.d, undefined, undefined, undefined);
110
+ this.moveHalfOfTheKToNewNode(newNode);
111
+ this.m = this.d;
112
+ return newNode;
113
+ }
114
+ }
115
+
116
+ }
@@ -0,0 +1,58 @@
1
+ import {TreeNode} from "./TreeNode";
2
+
3
+ export class Tree<T> {
4
+
5
+ protected root : TreeNode<T> | null = null
6
+ protected comparator : <T>(item1: T, item2: T) => number
7
+
8
+ constructor(comparator: <T>(item1: T, item2: T) => number){
9
+ this.comparator = comparator
10
+ }
11
+
12
+ public search(value: T): TreeNode<T> | null{
13
+ let d : TreeNode<T> | null = this.root
14
+ while (d != null){
15
+ if (this.comparator(d.data, value) == 0){
16
+ return d
17
+ } else {
18
+ if (this.comparator(d.data, value) > 0){
19
+ d = d.left
20
+ } else {
21
+ d = d.right
22
+ }
23
+ }
24
+ }
25
+ return null
26
+ }
27
+
28
+ protected insertChild(parent : TreeNode<T> | null, child: TreeNode<T>){
29
+ if (parent == null) {
30
+ this.root = child
31
+ } else {
32
+ if (this.comparator(child.data, parent.data) < 0) {
33
+ parent.left = child
34
+ } else {
35
+ parent.right = child
36
+ }
37
+ }
38
+ }
39
+
40
+ public insert(node: TreeNode<T>){
41
+ let y : TreeNode<T> | null = null
42
+ let x : TreeNode<T> | null = this.root;
43
+ while (x != null){
44
+ y = x;
45
+ if (this.comparator(node.data, x.data) < 0){
46
+ x = x.left;
47
+ } else {
48
+ x = x.right;
49
+ }
50
+ }
51
+ this.insertChild(y, node);
52
+ }
53
+
54
+ public insertData(data: T){
55
+ this.insert(new TreeNode(data));
56
+ }
57
+
58
+ }
@@ -0,0 +1,11 @@
1
+ export class TreeNode<T> {
2
+
3
+ data : T
4
+ left : TreeNode<T> | null = null
5
+ right : TreeNode<T> | null = null
6
+
7
+ constructor(data: T){
8
+ this.data = data
9
+ }
10
+
11
+ }
@@ -1,4 +1,4 @@
1
- import {CounterHashMap} from "../source/CounterHashMap";
1
+ import {CounterHashMap} from "../dist/CounterHashMap";
2
2
  import * as assert from "assert";
3
3
 
4
4
  describe('CounterHashMapTest', function() {
@@ -1,5 +1,5 @@
1
1
  import * as assert from "assert";
2
- import {LRUCache} from "../source/LRUCache";
2
+ import {LRUCache} from "../dist/LRUCache";
3
3
 
4
4
  describe('LRUCacheTest', function() {
5
5
  describe('LRUCacheTest', function() {
@@ -0,0 +1,47 @@
1
+ import * as assert from "assert";
2
+ import {Tree} from "../dist/tree/Tree";
3
+ import {AvlTree} from "../dist/tree/AvlTree";
4
+ import {BTree} from "../dist/tree/BTree";
5
+
6
+ describe('TreeTest', function() {
7
+ describe('TreeTest', function() {
8
+ function compare<T>(item1: T, item2: T): number {
9
+ if (typeof item1 == "number" && typeof item2 == "number"){
10
+ return item1 - item2
11
+ }
12
+ return 0
13
+ }
14
+ it('testTree', function() {
15
+ let tree : Tree<number> = new Tree<number>(compare);
16
+ tree.insertData(4);
17
+ tree.insertData(6);
18
+ tree.insertData(2);
19
+ tree.insertData(5);
20
+ tree.insertData(3);
21
+ tree.insertData(1);
22
+ tree.insertData(7);
23
+ assert.notEqual(null, tree.search(3));
24
+ assert.equal(null, tree.search(8));
25
+ });
26
+ it('testTree2', function() {
27
+ let tree : AvlTree<number> = new AvlTree<number>(compare);
28
+ for (let i = 1; i <= 31; i++){
29
+ tree.insertData(i);
30
+ }
31
+ for (let i = 1; i < 32; i++){
32
+ assert.notEqual(null, tree.search(i));
33
+ }
34
+ assert.equal(null, tree.search(32));
35
+ });
36
+ it('testTree3', function() {
37
+ let tree : BTree<number> = new BTree<number>(1, compare);
38
+ for (let i = 1; i <= 31; i++){
39
+ tree.insertData(i);
40
+ }
41
+ for (let i = 1; i < 32; i++){
42
+ assert.notEqual(null, tree.search(i));
43
+ }
44
+ assert.equal(null, tree.search(32));
45
+ });
46
+ });
47
+ });