nlptoolkit-datastructure 1.0.6 → 1.0.8
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.
- package/README.md +1 -1
- package/dist/Queue.d.ts +25 -0
- package/dist/Queue.js +29 -0
- package/dist/Queue.js.map +1 -1
- package/dist/Stack.d.ts +23 -0
- package/dist/Stack.js +23 -0
- package/dist/Stack.js.map +1 -1
- package/dist/heap/Heap.d.ts +53 -0
- package/dist/heap/Heap.js +53 -0
- package/dist/heap/Heap.js.map +1 -1
- package/dist/heap/HeapNode.d.ts +8 -0
- package/dist/heap/HeapNode.js +8 -0
- package/dist/heap/HeapNode.js.map +1 -1
- package/dist/heap/MaxHeap.js.map +1 -1
- package/dist/index.d.ts +9 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/tree/AvlTree.d.ts +81 -0
- package/dist/tree/AvlTree.js +81 -0
- package/dist/tree/AvlTree.js.map +1 -1
- package/dist/tree/BTree.d.ts +35 -0
- package/dist/tree/BTree.js +35 -0
- package/dist/tree/BTree.js.map +1 -1
- package/dist/tree/BTreeNode.d.ts +69 -0
- package/dist/tree/BTreeNode.js +69 -0
- package/dist/tree/BTreeNode.js.map +1 -1
- package/dist/tree/Tree.d.ts +37 -0
- package/dist/tree/Tree.js +37 -0
- package/dist/tree/Tree.js.map +1 -1
- package/package.json +3 -6
- package/source/Queue.ts +30 -0
- package/source/Stack.ts +23 -0
- package/source/heap/Heap.ts +53 -0
- package/source/heap/HeapNode.ts +8 -0
- package/source/heap/MaxHeap.ts +0 -1
- package/source/index.ts +9 -0
- package/source/tree/AvlTree.ts +81 -0
- package/source/tree/BTree.ts +35 -0
- package/source/tree/BTreeNode.ts +69 -0
- package/source/tree/Tree.ts +37 -0
- package/source/tsconfig.json +1 -1
- package/tsconfig.json +1 -2
- package/index.js +0 -9
package/dist/tree/AvlTree.js
CHANGED
|
@@ -13,6 +13,14 @@
|
|
|
13
13
|
const Tree_1 = require("./Tree");
|
|
14
14
|
const AvlTreeNode_1 = require("./AvlTreeNode");
|
|
15
15
|
const Stack_1 = require("../Stack");
|
|
16
|
+
/**
|
|
17
|
+
* <p>AVL (Adelson-Velskii and Landis) tree is a balanced binary search tree structure. The balance property is very simple
|
|
18
|
+
* and ensures the depth of the tree is in the level of O(log N).</p>
|
|
19
|
+
*
|
|
20
|
+
* <p>In AVL tree, the heights of the left and right subtrees of each node can differ by at most 1. If the heights of the
|
|
21
|
+
* left and right subtrees of a single node differ by more than 1, then that binary search tree is not an AVL tree.</p>
|
|
22
|
+
* @param T Type of the data stored in the tree node.
|
|
23
|
+
*/
|
|
16
24
|
class AvlTree extends Tree_1.Tree {
|
|
17
25
|
constructor(comparator) {
|
|
18
26
|
super(comparator);
|
|
@@ -25,6 +33,18 @@
|
|
|
25
33
|
return d.height;
|
|
26
34
|
}
|
|
27
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* In rotate left, we move node k1 one level up, since due to the binary search tree
|
|
38
|
+
* property k2 > k1, we move node k2 one level down. The links updated are:
|
|
39
|
+
* <ul>
|
|
40
|
+
* <li>Since k2 > B > k1, the left child of node k2 is now the old right child of k1</li>
|
|
41
|
+
* <li>The right child of k1 is now k2 </li>
|
|
42
|
+
* </ul>
|
|
43
|
+
* Note that, the root node of the subtree is now k1. In order to modify the parent link of k2, the new root of the
|
|
44
|
+
* subtree is returned by the function.
|
|
45
|
+
* @param k2 Root of the subtree, which does not satisfy the AVL tree property.
|
|
46
|
+
* @return The new root of the subtree
|
|
47
|
+
*/
|
|
28
48
|
rotateLeft(k2) {
|
|
29
49
|
let k1 = k2.left;
|
|
30
50
|
k2.left = k1.right;
|
|
@@ -33,6 +53,18 @@
|
|
|
33
53
|
k1.height = Math.max(this.height(k1.left), k1.right.height) + 1;
|
|
34
54
|
return k1;
|
|
35
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* In order to restore the AVL tree property, we move node k2 one level up, since due to the binary search tree
|
|
58
|
+
* property k2 > k1, we move node k1 one level down. The links updated are:
|
|
59
|
+
* <ul>
|
|
60
|
+
* <li>Since k2 > B > k1, the right child of node k1 is now the old left child of k2.</li>
|
|
61
|
+
* <li>The left child of k2 is now k1</li>
|
|
62
|
+
* </ul>
|
|
63
|
+
* Note that, the root node of the subtree is now k2. In order to modify the parent link of k1, the new root of the
|
|
64
|
+
* subtree is returned by the function.
|
|
65
|
+
* @param k1 Root of the subtree, which does not satisfy the AVL tree property.
|
|
66
|
+
* @return The new root of the subtree
|
|
67
|
+
*/
|
|
36
68
|
rotateRight(k1) {
|
|
37
69
|
let k2 = k1.right;
|
|
38
70
|
k1.right = k2.left;
|
|
@@ -41,14 +73,63 @@
|
|
|
41
73
|
k1.height = Math.max(this.height(k1.left), this.height(k1.right)) + 1;
|
|
42
74
|
return k2;
|
|
43
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* <p>In the first phase we will do single right rotation on the subtree rooted with k1. With this rotation, the left
|
|
78
|
+
* child of node k2 will be k1, whereas the right child of node k1 will be B (the old left child of node k2).</p>
|
|
79
|
+
*
|
|
80
|
+
* <p>In the second phase, we will do single left rotation on the subtree rooted with k3. With this rotation, the
|
|
81
|
+
* right child of node k2 will be k3, whereas the left child of node k3 will be C (the old right child of k2).</p>
|
|
82
|
+
*
|
|
83
|
+
* Note that, the new root node of the subtree is now k2. In order to modify the parent link of k3, the new root of
|
|
84
|
+
* the subtree is returned by the function.
|
|
85
|
+
* @param k3 Root of the subtree, which does not satisfy the AVL tree property.
|
|
86
|
+
* @return The new root of the subtree
|
|
87
|
+
*/
|
|
44
88
|
doubleRotateLeft(k3) {
|
|
45
89
|
k3.left = this.rotateRight(k3.left);
|
|
46
90
|
return this.rotateLeft(k3);
|
|
47
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* <p>In the first phase we will do single right rotation on the subtree rooted with k3. With this rotation, the right
|
|
94
|
+
* child of node k2 will be k3, whereas the left child of node k3 will be C (the old right child of node k2).</p>
|
|
95
|
+
*
|
|
96
|
+
* <p>In the second phase, we will do single right rotation on the subtree rooted with k1. With this rotation, the left
|
|
97
|
+
* child of node k2 will be k1, whereas the left child of node k1 will be B (the old left child of k2).</p>
|
|
98
|
+
*
|
|
99
|
+
* Note that, the new root node of the subtree is now k2. In order to modify the parent link of k1, the new root of
|
|
100
|
+
* the subtree is returned by the function.
|
|
101
|
+
* @param k1 Root of the subtree, which does not satisfy the AVL tree property.
|
|
102
|
+
* @return The new root of the subtree
|
|
103
|
+
*/
|
|
48
104
|
doubleRotateRight(k1) {
|
|
49
105
|
k1.right = this.rotateLeft(k1.right);
|
|
50
106
|
return this.rotateRight(k1);
|
|
51
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* <p>First we will proceed with the stages the same when we add a new node into a binary search tree. For this, we
|
|
110
|
+
* start from the root node and traverse in down manner. The current node we visit is represented with x and the
|
|
111
|
+
* previous node is represented with y. At each time we compare the value of the current node with the value of the
|
|
112
|
+
* new node. If the value of the new node is smaller than the value of the current node, the new node will be
|
|
113
|
+
* inserted into the left subtree of the current node. For this, we will continue with the left child to process. If
|
|
114
|
+
* the reverse is true, that is, if the value of the new node is larger than the value of the current node, the new
|
|
115
|
+
* node will be inserted into the right subtree of the current node. In this case, we will continue with the right
|
|
116
|
+
* child to process. At each step, we store the current node in a separate stack.</p>
|
|
117
|
+
*
|
|
118
|
+
* <p>When we insert a new node into an AVL tree, we need to change the heights of the nodes and check if the AVL tree
|
|
119
|
+
* property is satisfied or not. Only the height of the nodes, which we visit while we are finding the place for the
|
|
120
|
+
* new node, can be changed. So, what we should do is to pop those nodes from the stack one by one and change the
|
|
121
|
+
* heights of those nodes.</p>
|
|
122
|
+
*
|
|
123
|
+
* <p>Similarly, the nodes, which we visit while we are finding the place for the new node, may no longer satisfy the
|
|
124
|
+
* AVL tree property. So what we should do is to pop those nodes from the stack one by one and calculate the
|
|
125
|
+
* difference of the heights of their left and right subtrees. If the height difference is 2, the AVL tree property
|
|
126
|
+
* is not satisfied. If we added the new node into the left subtree of the left child, we need to do left single
|
|
127
|
+
* rotation, if we added into the right subtree of the left child, we need to do left double rotation, if we added
|
|
128
|
+
* into the left subtree of the right child, we need to do right double rotation, if we added into the right subtree
|
|
129
|
+
* of the right child, we need to the right single rotation. Since the root node of the subtree will be changed
|
|
130
|
+
* after a rotation, the new child of y will be the new root node t.</p>
|
|
131
|
+
* @param node Node to be inserted.
|
|
132
|
+
*/
|
|
52
133
|
insert(node) {
|
|
53
134
|
let LEFT = 1, RIGHT = 2;
|
|
54
135
|
let y = null;
|
package/dist/tree/AvlTree.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AvlTree.js","sourceRoot":"","sources":["../../source/tree/AvlTree.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,iCAA4B;IAC5B,+CAA0C;IAC1C,oCAA+B;IAE/B,MAAa,OAAW,SAAQ,WAAO;QAEnC,YAAY,UAA6C;YACrD,KAAK,CAAC,UAAU,CAAC,CAAA;QACrB,CAAC;QAEM,MAAM,CAAC,CAAiB;YAC3B,IAAI,CAAC,IAAI,IAAI,EAAC;gBACV,OAAO,CAAC,CAAA;aACX;iBAAM;gBACH,OAAO,CAAC,CAAC,MAAM,CAAA;aAClB;QACL,CAAC;
|
|
1
|
+
{"version":3,"file":"AvlTree.js","sourceRoot":"","sources":["../../source/tree/AvlTree.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,iCAA4B;IAC5B,+CAA0C;IAC1C,oCAA+B;IAE/B;;;;;;;OAOG;IACH,MAAa,OAAW,SAAQ,WAAO;QAEnC,YAAY,UAA6C;YACrD,KAAK,CAAC,UAAU,CAAC,CAAA;QACrB,CAAC;QAEM,MAAM,CAAC,CAAiB;YAC3B,IAAI,CAAC,IAAI,IAAI,EAAC;gBACV,OAAO,CAAC,CAAA;aACX;iBAAM;gBACH,OAAO,CAAC,CAAC,MAAM,CAAA;aAClB;QACL,CAAC;QAED;;;;;;;;;;;WAWG;QACK,UAAU,CAAC,EAAkB;YAC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I,EAAC;oBACV,CAAC,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAkB,CAAC,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,MAAM,CAAkB,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC;oBACrG,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAkB,CAAC,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM,CAAkB,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,EAAC;wBAC5F,IAAI,IAAI,IAAI,IAAI,EAAC;4BACb,IAAI,IAAI,IAAI,IAAI,EAAC;gCACb,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;6BACzB;iCAAM;gCACH,CAAC,GAAG,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAA;6BAC/B;yBACJ;6BAAM;4BACH,IAAI,IAAI,IAAI,IAAI,EAAC;gCACb,CAAC,GAAG,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAA;6BAChC;iCAAM;gCACH,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;6BAC3B;yBACJ;wBACD,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;wBACZ,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;wBACvB,MAAM;qBACT;iBACJ;aACJ;QACL,CAAC;QAEM,UAAU,CAAE,IAAQ;YACvB,IAAI,CAAC,MAAM,CAAC,IAAI,yBAAW,CAAI,IAAI,CAAC,CAAC,CAAA;QACzC,CAAC;KAEJ;IArKD,0BAqKC"}
|
package/dist/tree/BTree.d.ts
CHANGED
|
@@ -1,9 +1,44 @@
|
|
|
1
1
|
import { BTreeNode } from "./BTreeNode";
|
|
2
|
+
/**
|
|
3
|
+
* <p>In the computer science literature, the structures such as AVL tree, splay tree, red-black tree are proposed, which
|
|
4
|
+
* show the search tree property and also remain balanced after insertion and deletion operations.</p>
|
|
5
|
+
*
|
|
6
|
+
* <p>Another possibility of constructing a balanced tree structure is to store not only a single value but more than one
|
|
7
|
+
* value in a node. These type of tree structures are generalizations of the binary trees and called d-ary tree
|
|
8
|
+
* structures in the computer science literature. 2-3-4 trees, B-tree, B+ trees can be given as example d-ary tree
|
|
9
|
+
* structures. B+ trees, is one of the d-ary tree structures and used often in database systems.</p>
|
|
10
|
+
*
|
|
11
|
+
* <p>B+ tree is a dynamic search tree structure and consists of two parts, an index part and a data part. The index part
|
|
12
|
+
* is of d-ary tree structure, each node stores d {@literal <} m {@literal <} 2d values. d is a parameter of B+ tree, shows the capacity of B+
|
|
13
|
+
* tree and called as the degree of the tree. The root node is the single exception to this rule and can store
|
|
14
|
+
* 1 {@literal <} m {@literal <} 2d values. Each node also contains m + 1 links to point to its m + 1 child nodes. With the help of these
|
|
15
|
+
* links, the tree can be traversed in top-down manner. Let Pi represent the link pointing to the node i and Ki
|
|
16
|
+
* represent the i'th value in the same node, the i'th child and the ascendants of this child can take values between
|
|
17
|
+
* the interval Ki {@literal <} K {@literal <} Ki+1. The data are stored in the leaf nodes and due to the definition of a tree, the leaf nodes
|
|
18
|
+
* can not have children.</p>
|
|
19
|
+
* @param T Type of the data stored in the B tree node.
|
|
20
|
+
*/
|
|
2
21
|
export declare class BTree<T> {
|
|
3
22
|
root: BTreeNode<T> | null;
|
|
4
23
|
comparator: <T>(item1: T, item2: T) => number;
|
|
5
24
|
d: number;
|
|
25
|
+
/**
|
|
26
|
+
* Constructor of the tree. According to the comparator, the tree could contain any object.
|
|
27
|
+
* @param d Parameter d in d-ary tree.
|
|
28
|
+
* @param comparator Comparator function to compare two elements.
|
|
29
|
+
*/
|
|
6
30
|
constructor(d: number, comparator: <T>(item1: T, item2: T) => number);
|
|
31
|
+
/**
|
|
32
|
+
* We start searching from the root node, the node with which we compare the searched value at each stage is
|
|
33
|
+
* represented by b, and we continue the search until we arrive the leaf nodes. In order to understand the subtree
|
|
34
|
+
* of node b where our searched value resides, we need to compare the searched value with the values Ki. For this,
|
|
35
|
+
* the function named position is given. If the searched value is larger than the last value of node b, we need to
|
|
36
|
+
* continue the search with the rightmost child. If the searched value is smaller than the i. value of node b, we
|
|
37
|
+
* need to continue the search with the i. child. As a last step, the function returns the leaf node of node b.
|
|
38
|
+
* @param value Value searched in B+ tree.
|
|
39
|
+
* @return If the value exists in the tree, the function returns the node that contains the node. Otherwise, it
|
|
40
|
+
* returns null.
|
|
41
|
+
*/
|
|
7
42
|
search(value: T): BTreeNode<T> | null;
|
|
8
43
|
insertData(data: T): void;
|
|
9
44
|
}
|
package/dist/tree/BTree.js
CHANGED
|
@@ -11,12 +11,47 @@
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.BTree = void 0;
|
|
13
13
|
const BTreeNode_1 = require("./BTreeNode");
|
|
14
|
+
/**
|
|
15
|
+
* <p>In the computer science literature, the structures such as AVL tree, splay tree, red-black tree are proposed, which
|
|
16
|
+
* show the search tree property and also remain balanced after insertion and deletion operations.</p>
|
|
17
|
+
*
|
|
18
|
+
* <p>Another possibility of constructing a balanced tree structure is to store not only a single value but more than one
|
|
19
|
+
* value in a node. These type of tree structures are generalizations of the binary trees and called d-ary tree
|
|
20
|
+
* structures in the computer science literature. 2-3-4 trees, B-tree, B+ trees can be given as example d-ary tree
|
|
21
|
+
* structures. B+ trees, is one of the d-ary tree structures and used often in database systems.</p>
|
|
22
|
+
*
|
|
23
|
+
* <p>B+ tree is a dynamic search tree structure and consists of two parts, an index part and a data part. The index part
|
|
24
|
+
* is of d-ary tree structure, each node stores d {@literal <} m {@literal <} 2d values. d is a parameter of B+ tree, shows the capacity of B+
|
|
25
|
+
* tree and called as the degree of the tree. The root node is the single exception to this rule and can store
|
|
26
|
+
* 1 {@literal <} m {@literal <} 2d values. Each node also contains m + 1 links to point to its m + 1 child nodes. With the help of these
|
|
27
|
+
* links, the tree can be traversed in top-down manner. Let Pi represent the link pointing to the node i and Ki
|
|
28
|
+
* represent the i'th value in the same node, the i'th child and the ascendants of this child can take values between
|
|
29
|
+
* the interval Ki {@literal <} K {@literal <} Ki+1. The data are stored in the leaf nodes and due to the definition of a tree, the leaf nodes
|
|
30
|
+
* can not have children.</p>
|
|
31
|
+
* @param T Type of the data stored in the B tree node.
|
|
32
|
+
*/
|
|
14
33
|
class BTree {
|
|
34
|
+
/**
|
|
35
|
+
* Constructor of the tree. According to the comparator, the tree could contain any object.
|
|
36
|
+
* @param d Parameter d in d-ary tree.
|
|
37
|
+
* @param comparator Comparator function to compare two elements.
|
|
38
|
+
*/
|
|
15
39
|
constructor(d, comparator) {
|
|
16
40
|
this.root = null;
|
|
17
41
|
this.comparator = comparator;
|
|
18
42
|
this.d = d;
|
|
19
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* We start searching from the root node, the node with which we compare the searched value at each stage is
|
|
46
|
+
* represented by b, and we continue the search until we arrive the leaf nodes. In order to understand the subtree
|
|
47
|
+
* of node b where our searched value resides, we need to compare the searched value with the values Ki. For this,
|
|
48
|
+
* the function named position is given. If the searched value is larger than the last value of node b, we need to
|
|
49
|
+
* continue the search with the rightmost child. If the searched value is smaller than the i. value of node b, we
|
|
50
|
+
* need to continue the search with the i. child. As a last step, the function returns the leaf node of node b.
|
|
51
|
+
* @param value Value searched in B+ tree.
|
|
52
|
+
* @return If the value exists in the tree, the function returns the node that contains the node. Otherwise, it
|
|
53
|
+
* returns null.
|
|
54
|
+
*/
|
|
20
55
|
search(value) {
|
|
21
56
|
let b = this.root;
|
|
22
57
|
while (b != null && !b.leaf) {
|
package/dist/tree/BTree.js.map
CHANGED
|
@@ -1 +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;
|
|
1
|
+
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AAE,SAAS,EAAE,SAAS,CAAC,CAAA;aACxE;YACD,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,EAAC;gBACf,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,UAAU,CAAC,CAAA;gBACnD,IAAI,CAAC,IAAI,IAAI,EAAC;oBACV,IAAI,GAAG,GAAG,IAAI,CAAC,IAAI,CAAA;oBACnB,IAAI,CAAC,IAAI,GAAG,IAAI,qBAAS,CAAI,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,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/DD,sBA+DC"}
|
package/dist/tree/BTreeNode.d.ts
CHANGED
|
@@ -4,12 +4,81 @@ export declare class BTreeNode<T> {
|
|
|
4
4
|
d: number;
|
|
5
5
|
leaf: boolean;
|
|
6
6
|
children: Array<BTreeNode<T>>;
|
|
7
|
+
/**
|
|
8
|
+
* Another constructor of a B+ Tree node. By default, it is not a leaf node. Adds two children.
|
|
9
|
+
* @param d d in d-ary tree.
|
|
10
|
+
* @param firstChild First child of the root node.
|
|
11
|
+
* @param secondChild Second child of the root node.
|
|
12
|
+
* @param newK First value in the node.
|
|
13
|
+
*/
|
|
7
14
|
constructor(d: number, firstChild: BTreeNode<T> | undefined, secondChild: BTreeNode<T> | undefined, newK: T | undefined);
|
|
15
|
+
/**
|
|
16
|
+
* Searches the position of value in the list K. If the searched value is larger than the last value of node, we
|
|
17
|
+
* need to continue the search with the rightmost child. If the searched value is smaller than the i. value of node,
|
|
18
|
+
* we need to continue the search with the i. child.
|
|
19
|
+
* @param value Searched value
|
|
20
|
+
* @param comparator Comparator function which compares two elements.
|
|
21
|
+
* @return The position of searched value in array K.
|
|
22
|
+
*/
|
|
8
23
|
position(value: T, comparator: <T>(item1: T, item2: T) => number): number;
|
|
24
|
+
/**
|
|
25
|
+
* Add the new value insertedK to the array K into the calculated position index.
|
|
26
|
+
* @param index Place to insert new value
|
|
27
|
+
* @param insertedK New value to be inserted.
|
|
28
|
+
*/
|
|
9
29
|
private insertIntoK;
|
|
30
|
+
/**
|
|
31
|
+
* Transfers the last d values of the current node to the newNode.
|
|
32
|
+
* @param newNode New node.
|
|
33
|
+
*/
|
|
10
34
|
private moveHalfOfTheKToNewNode;
|
|
35
|
+
/**
|
|
36
|
+
* Transfers the last d links of the current node to the newNode.
|
|
37
|
+
* @param newNode New node.
|
|
38
|
+
*/
|
|
11
39
|
private moveHalfOfTheChildrenToNewNode;
|
|
40
|
+
/**
|
|
41
|
+
* Transfers the last d values and the last d links of the current node to the newNode.
|
|
42
|
+
* @param newNode New node.
|
|
43
|
+
*/
|
|
12
44
|
private moveHalfOfTheElementsToNewNode;
|
|
45
|
+
/**
|
|
46
|
+
* First the function position is used to determine the node or the subtree to which the new node will be added.
|
|
47
|
+
* If this subtree is a leaf node, we call the function insertLeaf that will add the value to a leaf node. If this
|
|
48
|
+
* subtree is not a leaf node the function calls itself with the determined subtree. Both insertNode and insertLeaf
|
|
49
|
+
* functions, if adding a new value/node to that node/subtree necessitates a new child node to be added to the
|
|
50
|
+
* parent node, they will both return the new added node and the node obtained by dividing the original node. If
|
|
51
|
+
* there is not such a restructuring, these functions will return null. If we add a new child node to the parent
|
|
52
|
+
* node, first we open a space for that child node in the value array K, then we add this new node to the array K.
|
|
53
|
+
* After adding there are two possibilities:
|
|
54
|
+
* <ul>
|
|
55
|
+
* <li>After inserting the new child node, the current node did not exceed its capacity. In this case, we open
|
|
56
|
+
* space for the link, which points to the new node, in the array children and place that link inside of this
|
|
57
|
+
* array.</li>
|
|
58
|
+
* <li>After inserting the new child node, the current node exceed its capacity. In this case, we need to create
|
|
59
|
+
* newNode, transfer the last d values and the last d links of the current node to the newNode. As a last case,
|
|
60
|
+
* if the divided node is the root node, we need to create a new root node and the first child of this new root
|
|
61
|
+
* node will be b, and the second child of the new root node will be newNode.</li>
|
|
62
|
+
* </ul>
|
|
63
|
+
* @param value Value to be inserted into B+ tree.
|
|
64
|
+
* @param comparator Comparator function to compare two elements.
|
|
65
|
+
* @param isRoot If true, value is inserted as a root node, otherwise false.
|
|
66
|
+
* @return If inserted node results in a creation of a node, the function returns that node, otherwise null.
|
|
67
|
+
*/
|
|
13
68
|
insertNode(value: T, comparator: <T>(item1: T, item2: T) => number, isRoot: boolean): BTreeNode<T> | null;
|
|
69
|
+
/**
|
|
70
|
+
* First the function position is used to determine the position where the new value will be placed Then we open a
|
|
71
|
+
* space for that value in the value array K, then we add this new value to the array K into the calculated
|
|
72
|
+
* position. At this stage there are again two possibilities:
|
|
73
|
+
* <ul>
|
|
74
|
+
* <li>After inserting the new value, the current leaf node did not exceed its capacity. The function returns
|
|
75
|
+
* null.</li>
|
|
76
|
+
* <li>After inserting the new value, the current leaf node exceed its capacity. In this case, we need to create
|
|
77
|
+
* the newNode, and transfer the last d values of node b to this newNode.</li>
|
|
78
|
+
* </ul>
|
|
79
|
+
* @param value Value to be inserted into B+ tree.
|
|
80
|
+
* @param comparator Comparator function to compare two elements.
|
|
81
|
+
* @return If inserted node results in a creation of a node, the function returns that node, otherwise null.
|
|
82
|
+
*/
|
|
14
83
|
insertLeaf(value: T, comparator: <T>(item1: T, item2: T) => number): BTreeNode<T> | null;
|
|
15
84
|
}
|
package/dist/tree/BTreeNode.js
CHANGED
|
@@ -11,6 +11,13 @@
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.BTreeNode = void 0;
|
|
13
13
|
class BTreeNode {
|
|
14
|
+
/**
|
|
15
|
+
* Another constructor of a B+ Tree node. By default, it is not a leaf node. Adds two children.
|
|
16
|
+
* @param d d in d-ary tree.
|
|
17
|
+
* @param firstChild First child of the root node.
|
|
18
|
+
* @param secondChild Second child of the root node.
|
|
19
|
+
* @param newK First value in the node.
|
|
20
|
+
*/
|
|
14
21
|
constructor(d, firstChild, secondChild, newK) {
|
|
15
22
|
this.d = d;
|
|
16
23
|
this.K = [];
|
|
@@ -31,6 +38,14 @@
|
|
|
31
38
|
}
|
|
32
39
|
}
|
|
33
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Searches the position of value in the list K. If the searched value is larger than the last value of node, we
|
|
43
|
+
* need to continue the search with the rightmost child. If the searched value is smaller than the i. value of node,
|
|
44
|
+
* we need to continue the search with the i. child.
|
|
45
|
+
* @param value Searched value
|
|
46
|
+
* @param comparator Comparator function which compares two elements.
|
|
47
|
+
* @return The position of searched value in array K.
|
|
48
|
+
*/
|
|
34
49
|
position(value, comparator) {
|
|
35
50
|
if (this.m == 0) {
|
|
36
51
|
return 0;
|
|
@@ -47,27 +62,67 @@
|
|
|
47
62
|
}
|
|
48
63
|
return -1;
|
|
49
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Add the new value insertedK to the array K into the calculated position index.
|
|
67
|
+
* @param index Place to insert new value
|
|
68
|
+
* @param insertedK New value to be inserted.
|
|
69
|
+
*/
|
|
50
70
|
insertIntoK(index, insertedK) {
|
|
51
71
|
for (let i = this.m; i > index; i--) {
|
|
52
72
|
this.K[i] = this.K[i - 1];
|
|
53
73
|
}
|
|
54
74
|
this.K[index] = insertedK;
|
|
55
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Transfers the last d values of the current node to the newNode.
|
|
78
|
+
* @param newNode New node.
|
|
79
|
+
*/
|
|
56
80
|
moveHalfOfTheKToNewNode(newNode) {
|
|
57
81
|
for (let i = 0; i < this.d; i++) {
|
|
58
82
|
newNode.K[i] = this.K[i + this.d + 1];
|
|
59
83
|
}
|
|
60
84
|
newNode.m = this.d;
|
|
61
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Transfers the last d links of the current node to the newNode.
|
|
88
|
+
* @param newNode New node.
|
|
89
|
+
*/
|
|
62
90
|
moveHalfOfTheChildrenToNewNode(newNode) {
|
|
63
91
|
for (let i = 0; i < this.d; i++) {
|
|
64
92
|
newNode.children[i] = this.children[i + this.d + 1];
|
|
65
93
|
}
|
|
66
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Transfers the last d values and the last d links of the current node to the newNode.
|
|
97
|
+
* @param newNode New node.
|
|
98
|
+
*/
|
|
67
99
|
moveHalfOfTheElementsToNewNode(newNode) {
|
|
68
100
|
this.moveHalfOfTheKToNewNode(newNode);
|
|
69
101
|
this.moveHalfOfTheChildrenToNewNode(newNode);
|
|
70
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* First the function position is used to determine the node or the subtree to which the new node will be added.
|
|
105
|
+
* If this subtree is a leaf node, we call the function insertLeaf that will add the value to a leaf node. If this
|
|
106
|
+
* subtree is not a leaf node the function calls itself with the determined subtree. Both insertNode and insertLeaf
|
|
107
|
+
* functions, if adding a new value/node to that node/subtree necessitates a new child node to be added to the
|
|
108
|
+
* parent node, they will both return the new added node and the node obtained by dividing the original node. If
|
|
109
|
+
* there is not such a restructuring, these functions will return null. If we add a new child node to the parent
|
|
110
|
+
* node, first we open a space for that child node in the value array K, then we add this new node to the array K.
|
|
111
|
+
* After adding there are two possibilities:
|
|
112
|
+
* <ul>
|
|
113
|
+
* <li>After inserting the new child node, the current node did not exceed its capacity. In this case, we open
|
|
114
|
+
* space for the link, which points to the new node, in the array children and place that link inside of this
|
|
115
|
+
* array.</li>
|
|
116
|
+
* <li>After inserting the new child node, the current node exceed its capacity. In this case, we need to create
|
|
117
|
+
* newNode, transfer the last d values and the last d links of the current node to the newNode. As a last case,
|
|
118
|
+
* if the divided node is the root node, we need to create a new root node and the first child of this new root
|
|
119
|
+
* node will be b, and the second child of the new root node will be newNode.</li>
|
|
120
|
+
* </ul>
|
|
121
|
+
* @param value Value to be inserted into B+ tree.
|
|
122
|
+
* @param comparator Comparator function to compare two elements.
|
|
123
|
+
* @param isRoot If true, value is inserted as a root node, otherwise false.
|
|
124
|
+
* @return If inserted node results in a creation of a node, the function returns that node, otherwise null.
|
|
125
|
+
*/
|
|
71
126
|
insertNode(value, comparator, isRoot) {
|
|
72
127
|
let s;
|
|
73
128
|
let newNode;
|
|
@@ -102,6 +157,20 @@
|
|
|
102
157
|
}
|
|
103
158
|
}
|
|
104
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* First the function position is used to determine the position where the new value will be placed Then we open a
|
|
162
|
+
* space for that value in the value array K, then we add this new value to the array K into the calculated
|
|
163
|
+
* position. At this stage there are again two possibilities:
|
|
164
|
+
* <ul>
|
|
165
|
+
* <li>After inserting the new value, the current leaf node did not exceed its capacity. The function returns
|
|
166
|
+
* null.</li>
|
|
167
|
+
* <li>After inserting the new value, the current leaf node exceed its capacity. In this case, we need to create
|
|
168
|
+
* the newNode, and transfer the last d values of node b to this newNode.</li>
|
|
169
|
+
* </ul>
|
|
170
|
+
* @param value Value to be inserted into B+ tree.
|
|
171
|
+
* @param comparator Comparator function to compare two elements.
|
|
172
|
+
* @return If inserted node results in a creation of a node, the function returns that node, otherwise null.
|
|
173
|
+
*/
|
|
105
174
|
insertLeaf(value, comparator) {
|
|
106
175
|
let child = this.position(value, comparator);
|
|
107
176
|
this.insertIntoK(child, value);
|
|
@@ -1 +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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,SAAS,CAAI,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;iBACjE;qBAAM;oBACH,OAAO,OAAO,CAAA;iBACjB;aACJ;QACL,CAAC;QAED;;;;;;;;;;;;;WAaG;QACI,UAAU,CAAC,KAAQ,EAAE,UAA6C;YACrE,IAAI,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC,CAAA;YAC5C,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;YAC9B,IAAI,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,EAAC;gBACpB,IAAI,CAAC,CAAC,EAAE,CAAC;gBACT,OAAO,IAAI,CAAA;aACd;iBAAM;gBACH,IAAI,OAAO,GAAG,IAAI,SAAS,CAAI,IAAI,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC;gBACxE,IAAI,CAAC,uBAAuB,CAAC,OAAO,CAAC,CAAC;gBACtC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;gBAChB,OAAO,OAAO,CAAC;aAClB;QACL,CAAC;KAEJ;IAxLD,8BAwLC"}
|
package/dist/tree/Tree.d.ts
CHANGED
|
@@ -1,10 +1,47 @@
|
|
|
1
1
|
import { TreeNode } from "./TreeNode";
|
|
2
|
+
/**
|
|
3
|
+
* Tree structure is also a non-linear data structure. Different from the tree structure we see in the nature, the
|
|
4
|
+
* tree data structure has its root on top and develops its branches down.
|
|
5
|
+
* @param T Type of the data stored in the tree node.
|
|
6
|
+
*/
|
|
2
7
|
export declare class Tree<T> {
|
|
3
8
|
protected root: TreeNode<T> | null;
|
|
4
9
|
protected comparator: <T>(item1: T, item2: T) => number;
|
|
10
|
+
/**
|
|
11
|
+
* Constructor of the tree. According to the comparator, the tree could contain any object.
|
|
12
|
+
* @param comparator Comparator function to compare two elements.
|
|
13
|
+
*/
|
|
5
14
|
constructor(comparator: <T>(item1: T, item2: T) => number);
|
|
15
|
+
/**
|
|
16
|
+
* The search starts from the root node, and we represent the node, with which we compare the searched value, with
|
|
17
|
+
* d. If the searched value is equal to the value of the current node, we have found the node we search for, the
|
|
18
|
+
* function will return that node. If the searched value is smaller than the value of the current node , the number
|
|
19
|
+
* we search for must be on the left subtree of the current node, therefore the new current node must be the left
|
|
20
|
+
* child of the current node. As the last case, if the searched value is larger than the value of the current node,
|
|
21
|
+
* the number we search for must be on the right subtree of the current node, therefore the new current node must be
|
|
22
|
+
* the right child of the current node. If this search continues until the leaf nodes of the tree and we can't find
|
|
23
|
+
* the node we search for, node d will be null and the function will return null.
|
|
24
|
+
* @param value Searched value
|
|
25
|
+
* @return If the value exists in the tree, the function returns the node that contains the node. Otherwise, it
|
|
26
|
+
* returns null.
|
|
27
|
+
*/
|
|
6
28
|
search(value: T): TreeNode<T> | null;
|
|
29
|
+
/**
|
|
30
|
+
* Inserts a child to its parent as left or right child.
|
|
31
|
+
* @param parent New parent of the child node.
|
|
32
|
+
* @param child Child node.
|
|
33
|
+
*/
|
|
7
34
|
protected insertChild(parent: TreeNode<T> | null, child: TreeNode<T>): void;
|
|
35
|
+
/**
|
|
36
|
+
* In order to add a new node into a binary search tree, we need to first find out the place where we will insert
|
|
37
|
+
* the new node. For this, we start from the root node and traverse the tree down. At each step, we compare the
|
|
38
|
+
* value of the new node with the value of the current node. If the value of the new node is smaller than the value
|
|
39
|
+
* of the current node, the new node will be inserted into the left subtree of the current node. To accomplish this,
|
|
40
|
+
* we continue the process with the left child of the current node. If the situation is reverse, that is, if the
|
|
41
|
+
* value of the new node is larger than the value of the current node, the new node will be inserted into the right
|
|
42
|
+
* subtree of the current node. In this case, we continue the process with the right child of the current node.
|
|
43
|
+
* @param node Node to be inserted.
|
|
44
|
+
*/
|
|
8
45
|
insert(node: TreeNode<T>): void;
|
|
9
46
|
insertData(data: T): void;
|
|
10
47
|
}
|
package/dist/tree/Tree.js
CHANGED
|
@@ -11,11 +11,33 @@
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.Tree = void 0;
|
|
13
13
|
const TreeNode_1 = require("./TreeNode");
|
|
14
|
+
/**
|
|
15
|
+
* Tree structure is also a non-linear data structure. Different from the tree structure we see in the nature, the
|
|
16
|
+
* tree data structure has its root on top and develops its branches down.
|
|
17
|
+
* @param T Type of the data stored in the tree node.
|
|
18
|
+
*/
|
|
14
19
|
class Tree {
|
|
20
|
+
/**
|
|
21
|
+
* Constructor of the tree. According to the comparator, the tree could contain any object.
|
|
22
|
+
* @param comparator Comparator function to compare two elements.
|
|
23
|
+
*/
|
|
15
24
|
constructor(comparator) {
|
|
16
25
|
this.root = null;
|
|
17
26
|
this.comparator = comparator;
|
|
18
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* The search starts from the root node, and we represent the node, with which we compare the searched value, with
|
|
30
|
+
* d. If the searched value is equal to the value of the current node, we have found the node we search for, the
|
|
31
|
+
* function will return that node. If the searched value is smaller than the value of the current node , the number
|
|
32
|
+
* we search for must be on the left subtree of the current node, therefore the new current node must be the left
|
|
33
|
+
* child of the current node. As the last case, if the searched value is larger than the value of the current node,
|
|
34
|
+
* the number we search for must be on the right subtree of the current node, therefore the new current node must be
|
|
35
|
+
* the right child of the current node. If this search continues until the leaf nodes of the tree and we can't find
|
|
36
|
+
* the node we search for, node d will be null and the function will return null.
|
|
37
|
+
* @param value Searched value
|
|
38
|
+
* @return If the value exists in the tree, the function returns the node that contains the node. Otherwise, it
|
|
39
|
+
* returns null.
|
|
40
|
+
*/
|
|
19
41
|
search(value) {
|
|
20
42
|
let d = this.root;
|
|
21
43
|
while (d != null) {
|
|
@@ -33,6 +55,11 @@
|
|
|
33
55
|
}
|
|
34
56
|
return null;
|
|
35
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Inserts a child to its parent as left or right child.
|
|
60
|
+
* @param parent New parent of the child node.
|
|
61
|
+
* @param child Child node.
|
|
62
|
+
*/
|
|
36
63
|
insertChild(parent, child) {
|
|
37
64
|
if (parent == null) {
|
|
38
65
|
this.root = child;
|
|
@@ -46,6 +73,16 @@
|
|
|
46
73
|
}
|
|
47
74
|
}
|
|
48
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* In order to add a new node into a binary search tree, we need to first find out the place where we will insert
|
|
78
|
+
* the new node. For this, we start from the root node and traverse the tree down. At each step, we compare the
|
|
79
|
+
* value of the new node with the value of the current node. If the value of the new node is smaller than the value
|
|
80
|
+
* of the current node, the new node will be inserted into the left subtree of the current node. To accomplish this,
|
|
81
|
+
* we continue the process with the left child of the current node. If the situation is reverse, that is, if the
|
|
82
|
+
* value of the new node is larger than the value of the current node, the new node will be inserted into the right
|
|
83
|
+
* subtree of the current node. In this case, we continue the process with the right child of the current node.
|
|
84
|
+
* @param node Node to be inserted.
|
|
85
|
+
*/
|
|
49
86
|
insert(node) {
|
|
50
87
|
let y = null;
|
|
51
88
|
let x = this.root;
|
package/dist/tree/Tree.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Tree.js","sourceRoot":"","sources":["../../source/tree/Tree.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,yCAAoC;IAEpC,MAAa,IAAI;QAKb,YAAY,UAA6C;
|
|
1
|
+
{"version":3,"file":"Tree.js","sourceRoot":"","sources":["../../source/tree/Tree.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,yCAAoC;IAEpC;;;;OAIG;IACH,MAAa,IAAI;QAKb;;;WAGG;QACH,YAAY,UAA6C;YAP/C,SAAI,GAAwB,IAAI,CAAA;YAQtC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAChC,CAAC;QAED;;;;;;;;;;;;WAYG;QACI,MAAM,CAAC,KAAQ;YAClB,IAAI,CAAC,GAAwB,IAAI,CAAC,IAAI,CAAA;YACtC,OAAO,CAAC,IAAI,IAAI,EAAC;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AAU,CAAC,IAAO;YACrB,IAAI,CAAC,MAAM,CAAC,IAAI,mBAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;QACpC,CAAC;KAEJ;IAvFD,oBAuFC"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nlptoolkit-datastructure",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "Simple Data Structures Library",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"types": "index.
|
|
7
|
-
"directories": {
|
|
8
|
-
"test": "tests"
|
|
9
|
-
},
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
10
7
|
"scripts": {
|
|
11
8
|
"test": "Mocha"
|
|
12
9
|
},
|