nlptoolkit-datastructure 1.0.8 → 1.0.9
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/dist/CounterHashMap.js +108 -118
- package/dist/CounterHashMap.js.map +1 -1
- package/dist/LRUCache.js +55 -63
- package/dist/LRUCache.js.map +1 -1
- package/dist/Queue.js +57 -64
- package/dist/Queue.js.map +1 -1
- package/dist/Stack.js +40 -50
- package/dist/Stack.js.map +1 -1
- package/dist/heap/Heap.js +113 -119
- package/dist/heap/Heap.js.map +1 -1
- package/dist/heap/HeapNode.js +19 -28
- package/dist/heap/HeapNode.js.map +1 -1
- package/dist/heap/MaxHeap.js +11 -21
- package/dist/heap/MaxHeap.js.map +1 -1
- package/dist/heap/MinHeap.js +11 -21
- package/dist/heap/MinHeap.js.map +1 -1
- package/dist/index.js +16 -22
- package/dist/index.js.map +1 -1
- package/dist/tree/AvlTree.js +160 -170
- package/dist/tree/AvlTree.js.map +1 -1
- package/dist/tree/AvlTreeNode.js +11 -20
- package/dist/tree/AvlTreeNode.js.map +1 -1
- package/dist/tree/BTree.js +74 -82
- package/dist/tree/BTree.js.map +1 -1
- package/dist/tree/BTreeNode.js +168 -173
- package/dist/tree/BTreeNode.js.map +1 -1
- package/dist/tree/Tree.js +81 -90
- package/dist/tree/Tree.js.map +1 -1
- package/dist/tree/TreeNode.js +11 -20
- package/dist/tree/TreeNode.js.map +1 -1
- package/package.json +3 -2
- package/tsconfig.json +4 -4
- package/source/tsconfig.json +0 -13
package/dist/tree/BTreeNode.js
CHANGED
|
@@ -1,191 +1,186 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
this.children = [];
|
|
25
|
-
if (firstChild == undefined) {
|
|
26
|
-
this.m = 0;
|
|
27
|
-
this.leaf = true;
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
this.m = 1;
|
|
31
|
-
this.leaf = false;
|
|
32
|
-
this.children[0] = firstChild;
|
|
33
|
-
if (secondChild != undefined) {
|
|
34
|
-
this.children[1] = secondChild;
|
|
35
|
-
}
|
|
36
|
-
if (newK != undefined) {
|
|
37
|
-
this.K[0] = newK;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BTreeNode = void 0;
|
|
4
|
+
class BTreeNode {
|
|
5
|
+
K;
|
|
6
|
+
m;
|
|
7
|
+
d;
|
|
8
|
+
leaf;
|
|
9
|
+
children;
|
|
10
|
+
/**
|
|
11
|
+
* Another constructor of a B+ Tree node. By default, it is not a leaf node. Adds two children.
|
|
12
|
+
* @param d d in d-ary tree.
|
|
13
|
+
* @param firstChild First child of the root node.
|
|
14
|
+
* @param secondChild Second child of the root node.
|
|
15
|
+
* @param newK First value in the node.
|
|
16
|
+
*/
|
|
17
|
+
constructor(d, firstChild, secondChild, newK) {
|
|
18
|
+
this.d = d;
|
|
19
|
+
this.K = [];
|
|
20
|
+
this.children = [];
|
|
21
|
+
if (firstChild == undefined) {
|
|
22
|
+
this.m = 0;
|
|
23
|
+
this.leaf = true;
|
|
40
24
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
* @return The position of searched value in array K.
|
|
48
|
-
*/
|
|
49
|
-
position(value, comparator) {
|
|
50
|
-
if (this.m == 0) {
|
|
51
|
-
return 0;
|
|
25
|
+
else {
|
|
26
|
+
this.m = 1;
|
|
27
|
+
this.leaf = false;
|
|
28
|
+
this.children[0] = firstChild;
|
|
29
|
+
if (secondChild != undefined) {
|
|
30
|
+
this.children[1] = secondChild;
|
|
52
31
|
}
|
|
53
|
-
if (
|
|
54
|
-
|
|
32
|
+
if (newK != undefined) {
|
|
33
|
+
this.K[0] = newK;
|
|
55
34
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Searches the position of value in the list K. If the searched value is larger than the last value of node, we
|
|
39
|
+
* need to continue the search with the rightmost child. If the searched value is smaller than the i. value of node,
|
|
40
|
+
* we need to continue the search with the i. child.
|
|
41
|
+
* @param value Searched value
|
|
42
|
+
* @param comparator Comparator function which compares two elements.
|
|
43
|
+
* @return The position of searched value in array K.
|
|
44
|
+
*/
|
|
45
|
+
position(value, comparator) {
|
|
46
|
+
if (this.m == 0) {
|
|
47
|
+
return 0;
|
|
48
|
+
}
|
|
49
|
+
if (comparator(value, this.K[this.m - 1]) > 0) {
|
|
50
|
+
return this.m;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
for (let i = 0; i < this.m; i++) {
|
|
54
|
+
if (comparator(value, this.K[i]) <= 0) {
|
|
55
|
+
return i;
|
|
61
56
|
}
|
|
62
57
|
}
|
|
63
|
-
return -1;
|
|
64
58
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this.K[
|
|
59
|
+
return -1;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Add the new value insertedK to the array K into the calculated position index.
|
|
63
|
+
* @param index Place to insert new value
|
|
64
|
+
* @param insertedK New value to be inserted.
|
|
65
|
+
*/
|
|
66
|
+
insertIntoK(index, insertedK) {
|
|
67
|
+
for (let i = this.m; i > index; i--) {
|
|
68
|
+
this.K[i] = this.K[i - 1];
|
|
75
69
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
newNode.
|
|
70
|
+
this.K[index] = insertedK;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Transfers the last d values of the current node to the newNode.
|
|
74
|
+
* @param newNode New node.
|
|
75
|
+
*/
|
|
76
|
+
moveHalfOfTheKToNewNode(newNode) {
|
|
77
|
+
for (let i = 0; i < this.d; i++) {
|
|
78
|
+
newNode.K[i] = this.K[i + this.d + 1];
|
|
85
79
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
80
|
+
newNode.m = this.d;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Transfers the last d links of the current node to the newNode.
|
|
84
|
+
* @param newNode New node.
|
|
85
|
+
*/
|
|
86
|
+
moveHalfOfTheChildrenToNewNode(newNode) {
|
|
87
|
+
for (let i = 0; i < this.d; i++) {
|
|
88
|
+
newNode.children[i] = this.children[i + this.d + 1];
|
|
94
89
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Transfers the last d values and the last d links of the current node to the newNode.
|
|
93
|
+
* @param newNode New node.
|
|
94
|
+
*/
|
|
95
|
+
moveHalfOfTheElementsToNewNode(newNode) {
|
|
96
|
+
this.moveHalfOfTheKToNewNode(newNode);
|
|
97
|
+
this.moveHalfOfTheChildrenToNewNode(newNode);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* First the function position is used to determine the node or the subtree to which the new node will be added.
|
|
101
|
+
* If this subtree is a leaf node, we call the function insertLeaf that will add the value to a leaf node. If this
|
|
102
|
+
* subtree is not a leaf node the function calls itself with the determined subtree. Both insertNode and insertLeaf
|
|
103
|
+
* functions, if adding a new value/node to that node/subtree necessitates a new child node to be added to the
|
|
104
|
+
* parent node, they will both return the new added node and the node obtained by dividing the original node. If
|
|
105
|
+
* there is not such a restructuring, these functions will return null. If we add a new child node to the parent
|
|
106
|
+
* 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.
|
|
107
|
+
* After adding there are two possibilities:
|
|
108
|
+
* <ul>
|
|
109
|
+
* <li>After inserting the new child node, the current node did not exceed its capacity. In this case, we open
|
|
110
|
+
* space for the link, which points to the new node, in the array children and place that link inside of this
|
|
111
|
+
* array.</li>
|
|
112
|
+
* <li>After inserting the new child node, the current node exceed its capacity. In this case, we need to create
|
|
113
|
+
* newNode, transfer the last d values and the last d links of the current node to the newNode. As a last case,
|
|
114
|
+
* if the divided node is the root node, we need to create a new root node and the first child of this new root
|
|
115
|
+
* node will be b, and the second child of the new root node will be newNode.</li>
|
|
116
|
+
* </ul>
|
|
117
|
+
* @param value Value to be inserted into B+ tree.
|
|
118
|
+
* @param comparator Comparator function to compare two elements.
|
|
119
|
+
* @param isRoot If true, value is inserted as a root node, otherwise false.
|
|
120
|
+
* @return If inserted node results in a creation of a node, the function returns that node, otherwise null.
|
|
121
|
+
*/
|
|
122
|
+
insertNode(value, comparator, isRoot) {
|
|
123
|
+
let s;
|
|
124
|
+
let newNode;
|
|
125
|
+
let child;
|
|
126
|
+
child = this.position(value, comparator);
|
|
127
|
+
if (!this.children[child].leaf) {
|
|
128
|
+
s = this.children[child].insertNode(value, comparator, false);
|
|
102
129
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
*/
|
|
126
|
-
insertNode(value, comparator, isRoot) {
|
|
127
|
-
let s;
|
|
128
|
-
let newNode;
|
|
129
|
-
let child;
|
|
130
|
-
child = this.position(value, comparator);
|
|
131
|
-
if (!this.children[child].leaf) {
|
|
132
|
-
s = this.children[child].insertNode(value, comparator, false);
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
s = this.children[child].insertLeaf(value, comparator);
|
|
136
|
-
}
|
|
137
|
-
if (s == null) {
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
140
|
-
this.insertIntoK(child, this.children[child].K[this.d]);
|
|
141
|
-
if (this.m < 2 * this.d) {
|
|
142
|
-
this.children[child + 1] = s;
|
|
143
|
-
this.m++;
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
newNode = new BTreeNode(this.d, undefined, undefined, undefined);
|
|
148
|
-
newNode.leaf = false;
|
|
149
|
-
this.moveHalfOfTheElementsToNewNode(newNode);
|
|
150
|
-
newNode.children[this.d] = s;
|
|
151
|
-
this.m = this.d;
|
|
152
|
-
if (isRoot) {
|
|
153
|
-
return new BTreeNode(this.d, this, newNode, this.K[this.d]);
|
|
154
|
-
}
|
|
155
|
-
else {
|
|
156
|
-
return newNode;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
130
|
+
else {
|
|
131
|
+
s = this.children[child].insertLeaf(value, comparator);
|
|
132
|
+
}
|
|
133
|
+
if (s == null) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
this.insertIntoK(child, this.children[child].K[this.d]);
|
|
137
|
+
if (this.m < 2 * this.d) {
|
|
138
|
+
this.children[child + 1] = s;
|
|
139
|
+
this.m++;
|
|
140
|
+
return null;
|
|
159
141
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
-
*/
|
|
174
|
-
insertLeaf(value, comparator) {
|
|
175
|
-
let child = this.position(value, comparator);
|
|
176
|
-
this.insertIntoK(child, value);
|
|
177
|
-
if (this.m < 2 * this.d) {
|
|
178
|
-
this.m++;
|
|
179
|
-
return null;
|
|
142
|
+
else {
|
|
143
|
+
newNode = new BTreeNode(this.d, undefined, undefined, undefined);
|
|
144
|
+
newNode.leaf = false;
|
|
145
|
+
this.moveHalfOfTheElementsToNewNode(newNode);
|
|
146
|
+
newNode.children[this.d] = s;
|
|
147
|
+
this.m = this.d;
|
|
148
|
+
if (isRoot) {
|
|
149
|
+
return new BTreeNode(this.d, this, newNode, this.K[this.d]);
|
|
180
150
|
}
|
|
181
151
|
else {
|
|
182
|
-
let newNode = new BTreeNode(this.d, undefined, undefined, undefined);
|
|
183
|
-
this.moveHalfOfTheKToNewNode(newNode);
|
|
184
|
-
this.m = this.d;
|
|
185
152
|
return newNode;
|
|
186
153
|
}
|
|
187
154
|
}
|
|
188
155
|
}
|
|
189
|
-
|
|
190
|
-
|
|
156
|
+
/**
|
|
157
|
+
* First the function position is used to determine the position where the new value will be placed Then we open a
|
|
158
|
+
* space for that value in the value array K, then we add this new value to the array K into the calculated
|
|
159
|
+
* position. At this stage there are again two possibilities:
|
|
160
|
+
* <ul>
|
|
161
|
+
* <li>After inserting the new value, the current leaf node did not exceed its capacity. The function returns
|
|
162
|
+
* null.</li>
|
|
163
|
+
* <li>After inserting the new value, the current leaf node exceed its capacity. In this case, we need to create
|
|
164
|
+
* the newNode, and transfer the last d values of node b to this newNode.</li>
|
|
165
|
+
* </ul>
|
|
166
|
+
* @param value Value to be inserted into B+ tree.
|
|
167
|
+
* @param comparator Comparator function to compare two elements.
|
|
168
|
+
* @return If inserted node results in a creation of a node, the function returns that node, otherwise null.
|
|
169
|
+
*/
|
|
170
|
+
insertLeaf(value, comparator) {
|
|
171
|
+
let child = this.position(value, comparator);
|
|
172
|
+
this.insertIntoK(child, value);
|
|
173
|
+
if (this.m < 2 * this.d) {
|
|
174
|
+
this.m++;
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
let newNode = new BTreeNode(this.d, undefined, undefined, undefined);
|
|
179
|
+
this.moveHalfOfTheKToNewNode(newNode);
|
|
180
|
+
this.m = this.d;
|
|
181
|
+
return newNode;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
exports.BTreeNode = BTreeNode;
|
|
191
186
|
//# sourceMappingURL=BTreeNode.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BTreeNode.js","sourceRoot":"","sources":["../../source/tree/BTreeNode.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"BTreeNode.js","sourceRoot":"","sources":["../../source/tree/BTreeNode.ts"],"names":[],"mappings":";;;AAAA,MAAa,SAAS;IAElB,CAAC,CAAY;IACb,CAAC,CAAS;IACV,CAAC,CAAS;IACV,IAAI,CAAS;IACb,QAAQ,CAAsB;IAE9B;;;;;;OAMG;IACH,YAAY,CAAS,EAAE,UAAoC,EAAE,WAAqC,EAAE,IAAoB;QACpH,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;QACV,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC;Q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AC,CAAA;YACf,IAAI,MAAM,EAAC,CAAC;gBACR,OAAO,IAAI,SAAS,CAAI,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;YAClE,CAAC;iBAAM,CAAC;gBACJ,OAAO,OAAO,CAAA;YAClB,CAAC;QACL,CAAC;IACL,CAAC;IAED;;;;;;;;;;;;;OAaG;IACI,UAAU,CAAC,KAAQ,EAAE,UAA6C;QACrE,IAAI,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC,CAAA;QAC5C,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;QAC9B,IAAI,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,EAAC,CAAC;YACrB,IAAI,CAAC,CAAC,EAAE,CAAC;YACT,OAAO,IAAI,CAAA;QACf,CAAC;aAAM,CAAC;YACJ,IAAI,OAAO,GAAG,IAAI,SAAS,CAAI,IAAI,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC;YACxE,IAAI,CAAC,uBAAuB,CAAC,OAAO,CAAC,CAAC;YACtC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;YAChB,OAAO,OAAO,CAAC;QACnB,CAAC;IACL,CAAC;CAEJ;AAxLD,8BAwLC"}
|
package/dist/tree/Tree.js
CHANGED
|
@@ -1,106 +1,97 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Tree = void 0;
|
|
4
|
+
const TreeNode_1 = require("./TreeNode");
|
|
5
|
+
/**
|
|
6
|
+
* Tree structure is also a non-linear data structure. Different from the tree structure we see in the nature, the
|
|
7
|
+
* tree data structure has its root on top and develops its branches down.
|
|
8
|
+
* @param T Type of the data stored in the tree node.
|
|
9
|
+
*/
|
|
10
|
+
class Tree {
|
|
11
|
+
root = null;
|
|
12
|
+
comparator;
|
|
13
|
+
/**
|
|
14
|
+
* Constructor of the tree. According to the comparator, the tree could contain any object.
|
|
15
|
+
* @param comparator Comparator function to compare two elements.
|
|
16
|
+
*/
|
|
17
|
+
constructor(comparator) {
|
|
18
|
+
this.comparator = comparator;
|
|
8
19
|
}
|
|
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
20
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
21
|
+
* The search starts from the root node, and we represent the node, with which we compare the searched value, with
|
|
22
|
+
* d. If the searched value is equal to the value of the current node, we have found the node we search for, the
|
|
23
|
+
* function will return that node. If the searched value is smaller than the value of the current node , the number
|
|
24
|
+
* we search for must be on the left subtree of the current node, therefore the new current node must be the left
|
|
25
|
+
* child of the current node. As the last case, if the searched value is larger than the value of the current node,
|
|
26
|
+
* the number we search for must be on the right subtree of the current node, therefore the new current node must be
|
|
27
|
+
* the right child of the current node. If this search continues until the leaf nodes of the tree and we can't find
|
|
28
|
+
* the node we search for, node d will be null and the function will return null.
|
|
29
|
+
* @param value Searched value
|
|
30
|
+
* @return If the value exists in the tree, the function returns the node that contains the node. Otherwise, it
|
|
31
|
+
* returns null.
|
|
18
32
|
*/
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
*/
|
|
41
|
-
search(value) {
|
|
42
|
-
let d = this.root;
|
|
43
|
-
while (d != null) {
|
|
44
|
-
if (this.comparator(d.data, value) == 0) {
|
|
45
|
-
return d;
|
|
33
|
+
search(value) {
|
|
34
|
+
let d = this.root;
|
|
35
|
+
while (d != null) {
|
|
36
|
+
if (this.comparator(d.data, value) == 0) {
|
|
37
|
+
return d;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
if (this.comparator(d.data, value) > 0) {
|
|
41
|
+
d = d.left;
|
|
46
42
|
}
|
|
47
43
|
else {
|
|
48
|
-
|
|
49
|
-
d = d.left;
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
d = d.right;
|
|
53
|
-
}
|
|
44
|
+
d = d.right;
|
|
54
45
|
}
|
|
55
46
|
}
|
|
56
|
-
return null;
|
|
57
47
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Inserts a child to its parent as left or right child.
|
|
52
|
+
* @param parent New parent of the child node.
|
|
53
|
+
* @param child Child node.
|
|
54
|
+
*/
|
|
55
|
+
insertChild(parent, child) {
|
|
56
|
+
if (parent == null) {
|
|
57
|
+
this.root = child;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
if (this.comparator(child.data, parent.data) < 0) {
|
|
61
|
+
parent.left = child;
|
|
66
62
|
}
|
|
67
63
|
else {
|
|
68
|
-
|
|
69
|
-
parent.left = child;
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
parent.right = child;
|
|
73
|
-
}
|
|
64
|
+
parent.right = child;
|
|
74
65
|
}
|
|
75
66
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* In order to add a new node into a binary search tree, we need to first find out the place where we will insert
|
|
70
|
+
* the new node. For this, we start from the root node and traverse the tree down. At each step, we compare the
|
|
71
|
+
* value of the new node with the value of the current node. If the value of the new node is smaller than the value
|
|
72
|
+
* of the current node, the new node will be inserted into the left subtree of the current node. To accomplish this,
|
|
73
|
+
* we continue the process with the left child of the current node. If the situation is reverse, that is, if the
|
|
74
|
+
* value of the new node is larger than the value of the current node, the new node will be inserted into the right
|
|
75
|
+
* subtree of the current node. In this case, we continue the process with the right child of the current node.
|
|
76
|
+
* @param node Node to be inserted.
|
|
77
|
+
*/
|
|
78
|
+
insert(node) {
|
|
79
|
+
let y = null;
|
|
80
|
+
let x = this.root;
|
|
81
|
+
while (x != null) {
|
|
82
|
+
y = x;
|
|
83
|
+
if (this.comparator(node.data, x.data) < 0) {
|
|
84
|
+
x = x.left;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
x = x.right;
|
|
97
88
|
}
|
|
98
|
-
this.insertChild(y, node);
|
|
99
|
-
}
|
|
100
|
-
insertData(data) {
|
|
101
|
-
this.insert(new TreeNode_1.TreeNode(data));
|
|
102
89
|
}
|
|
90
|
+
this.insertChild(y, node);
|
|
91
|
+
}
|
|
92
|
+
insertData(data) {
|
|
93
|
+
this.insert(new TreeNode_1.TreeNode(data));
|
|
103
94
|
}
|
|
104
|
-
|
|
105
|
-
|
|
95
|
+
}
|
|
96
|
+
exports.Tree = Tree;
|
|
106
97
|
//# sourceMappingURL=Tree.js.map
|
package/dist/tree/Tree.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Tree.js","sourceRoot":"","sources":["../../source/tree/Tree.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Tree.js","sourceRoot":"","sources":["../../source/tree/Tree.ts"],"names":[],"mappings":";;;AAAA,yCAAoC;AAEpC;;;;GAIG;AACH,MAAa,IAAI;IAEH,IAAI,GAAwB,IAAI,CAAA;IAChC,UAAU,CAAoC;IAExD;;;OAGG;IACH,YAAY,UAA6C;QACrD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;IAChC,CAAC;IAED;;;;;;;;;;;;OAYG;IACI,MAAM,CAAC,KAAQ;QAClB,IAAI,CAAC,GAAwB,IAAI,CAAC,IAAI,CAAA;QACtC,OAAO,CAAC,IAAI,IAAI,EAAC,CAAC;YACd,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,EAAC,CAAC;gBACrC,OAAO,CAAC,CAAA;YACZ,CAAC;iBAAM,CAAC;gBACJ,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC,EAAC,CAAC;oBACpC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAA;gBACd,CAAC;qBAAM,CAAC;oBACJ,CAAC,GAAG,CAAC,CAAC,KAAK,CAAA;gBACf,CAAC;YACL,CAAC;QACL,CAAC;QACD,OAAO,IAAI,CAAA;IACf,CAAC;IAED;;;;OAIG;IACO,WAAW,CAAC,MAA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
class TreeNode {
|
|
5
|
+
data;
|
|
6
|
+
left = null;
|
|
7
|
+
right = null;
|
|
8
|
+
constructor(data) {
|
|
9
|
+
this.data = data;
|
|
5
10
|
}
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
});
|
|
11
|
+
}
|
|
12
|
+
exports.TreeNode = TreeNode;
|
|
22
13
|
//# sourceMappingURL=TreeNode.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TreeNode.js","sourceRoot":"","sources":["../../source/tree/TreeNode.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"TreeNode.js","sourceRoot":"","sources":["../../source/tree/TreeNode.ts"],"names":[],"mappings":";;;AAAA,MAAa,QAAQ;IAEjB,IAAI,CAAI;IACR,IAAI,GAAwB,IAAI,CAAA;IAChC,KAAK,GAAwB,IAAI,CAAA;IAEjC,YAAY,IAAO;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IACpB,CAAC;CAEJ;AAVD,4BAUC"}
|