data-structure-typed 1.34.8 → 1.35.0
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/.eslintrc.js +1 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
- package/.github/ISSUE_TEMPLATE/custom.md +10 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- package/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +0 -0
- package/README.md +8 -8
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +280 -423
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +63 -89
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +11 -30
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +127 -214
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +19 -43
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/segment-tree.js +80 -122
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +147 -218
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +338 -572
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +145 -275
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +46 -87
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +90 -179
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.js +85 -247
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +87 -128
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/pair.js.map +1 -1
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-map.js.map +1 -1
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/hash/tree-set.js.map +1 -1
- package/dist/data-structures/heap/heap.js +56 -80
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +57 -70
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.js +63 -84
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/queue.js +57 -157
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/tree.js +32 -45
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/utils/utils.js +22 -107
- package/dist/utils/utils.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +79 -78
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +61 -141
- package/lib/data-structures/binary-tree/avl-tree.d.ts +9 -0
- package/lib/data-structures/binary-tree/avl-tree.js +22 -0
- package/lib/data-structures/binary-tree/bst.d.ts +3 -3
- package/lib/data-structures/binary-tree/bst.js +12 -15
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +16 -18
- package/lib/data-structures/binary-tree/tree-multiset.js +26 -36
- package/lib/data-structures/graph/abstract-graph.d.ts +2 -2
- package/lib/data-structures/graph/abstract-graph.js +5 -5
- package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
- package/lib/data-structures/priority-queue/priority-queue.js +3 -3
- package/lib/interfaces/abstract-binary-tree.d.ts +1 -83
- package/lib/interfaces/abstract-graph.d.ts +2 -14
- package/lib/interfaces/avl-tree.d.ts +3 -4
- package/lib/interfaces/binary-tree.d.ts +4 -2
- package/lib/interfaces/bst.d.ts +2 -12
- package/lib/interfaces/directed-graph.d.ts +0 -9
- package/lib/interfaces/rb-tree.d.ts +2 -3
- package/lib/interfaces/tree-multiset.d.ts +5 -4
- package/lib/interfaces/undirected-graph.d.ts +0 -2
- package/lib/types/data-structures/abstract-binary-tree.js +0 -1
- package/package.json +7 -7
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +151 -203
- package/src/data-structures/binary-tree/avl-tree.ts +29 -0
- package/src/data-structures/binary-tree/bst.ts +12 -15
- package/src/data-structures/binary-tree/tree-multiset.ts +27 -39
- package/src/data-structures/graph/abstract-graph.ts +5 -5
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/interfaces/abstract-binary-tree.ts +2 -183
- package/src/interfaces/abstract-graph.ts +2 -26
- package/src/interfaces/avl-tree.ts +4 -21
- package/src/interfaces/binary-tree.ts +3 -2
- package/src/interfaces/bst.ts +2 -27
- package/src/interfaces/directed-graph.ts +1 -18
- package/src/interfaces/rb-tree.ts +2 -5
- package/src/interfaces/tree-multiset.ts +4 -4
- package/src/interfaces/undirected-graph.ts +1 -4
- package/src/types/data-structures/abstract-binary-tree.ts +0 -1
- package/test/integration/avl-tree.test.ts +24 -24
- package/test/integration/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -9
- package/test/unit/data-structures/binary-tree/bst.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +9 -9
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -4
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -9,28 +9,15 @@ import { trampoline } from '../../utils';
|
|
|
9
9
|
import { FamilyPosition, LoopType } from '../../types';
|
|
10
10
|
export class AbstractBinaryTreeNode {
|
|
11
11
|
/**
|
|
12
|
-
* The constructor function initializes a BinaryTreeNode object with
|
|
12
|
+
* The constructor function initializes a BinaryTreeNode object with a key and an optional value.
|
|
13
13
|
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
14
14
|
* of the binary tree node. It is used to distinguish one node from another in the binary tree.
|
|
15
15
|
* @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
|
|
16
16
|
* stored in the binary tree node. If no value is provided, it will be set to undefined.
|
|
17
17
|
*/
|
|
18
18
|
constructor(key, val) {
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this._val = val;
|
|
22
|
-
}
|
|
23
|
-
get key() {
|
|
24
|
-
return this._key;
|
|
25
|
-
}
|
|
26
|
-
set key(v) {
|
|
27
|
-
this._key = v;
|
|
28
|
-
}
|
|
29
|
-
get val() {
|
|
30
|
-
return this._val;
|
|
31
|
-
}
|
|
32
|
-
set val(value) {
|
|
33
|
-
this._val = value;
|
|
19
|
+
this.key = key;
|
|
20
|
+
this.val = val;
|
|
34
21
|
}
|
|
35
22
|
get left() {
|
|
36
23
|
return this._left;
|
|
@@ -50,18 +37,6 @@ export class AbstractBinaryTreeNode {
|
|
|
50
37
|
}
|
|
51
38
|
this._right = v;
|
|
52
39
|
}
|
|
53
|
-
get parent() {
|
|
54
|
-
return this._parent;
|
|
55
|
-
}
|
|
56
|
-
set parent(v) {
|
|
57
|
-
this._parent = v;
|
|
58
|
-
}
|
|
59
|
-
get height() {
|
|
60
|
-
return this._height;
|
|
61
|
-
}
|
|
62
|
-
set height(v) {
|
|
63
|
-
this._height = v;
|
|
64
|
-
}
|
|
65
40
|
/**
|
|
66
41
|
* The function determines the position of a node in a family tree structure.
|
|
67
42
|
* @returns a value of type `FamilyPosition`.
|
|
@@ -110,9 +85,9 @@ export class AbstractBinaryTree {
|
|
|
110
85
|
this._root = null;
|
|
111
86
|
this._size = 0;
|
|
112
87
|
this._loopType = LoopType.ITERATIVE;
|
|
113
|
-
this.
|
|
114
|
-
this.
|
|
115
|
-
this.
|
|
88
|
+
this.visitedKey = [];
|
|
89
|
+
this.visitedVal = [];
|
|
90
|
+
this.visitedNode = [];
|
|
116
91
|
if (options !== undefined) {
|
|
117
92
|
const { loopType = LoopType.ITERATIVE } = options;
|
|
118
93
|
this._loopType = loopType;
|
|
@@ -128,15 +103,6 @@ export class AbstractBinaryTree {
|
|
|
128
103
|
get loopType() {
|
|
129
104
|
return this._loopType;
|
|
130
105
|
}
|
|
131
|
-
get visitedKey() {
|
|
132
|
-
return this._visitedKey;
|
|
133
|
-
}
|
|
134
|
-
get visitedVal() {
|
|
135
|
-
return this._visitedVal;
|
|
136
|
-
}
|
|
137
|
-
get visitedNode() {
|
|
138
|
-
return this._visitedNode;
|
|
139
|
-
}
|
|
140
106
|
/**
|
|
141
107
|
* The `swapLocation` function swaps the location of two nodes in a binary tree.
|
|
142
108
|
* @param {N} srcNode - The source node that you want to swap with the destination node.
|
|
@@ -145,16 +111,13 @@ export class AbstractBinaryTree {
|
|
|
145
111
|
* @returns The `destNode` is being returned.
|
|
146
112
|
*/
|
|
147
113
|
swapLocation(srcNode, destNode) {
|
|
148
|
-
const { key, val
|
|
114
|
+
const { key, val } = destNode;
|
|
149
115
|
const tempNode = this.createNode(key, val);
|
|
150
116
|
if (tempNode) {
|
|
151
|
-
tempNode.height = height;
|
|
152
117
|
destNode.key = srcNode.key;
|
|
153
118
|
destNode.val = srcNode.val;
|
|
154
|
-
destNode.height = srcNode.height;
|
|
155
119
|
srcNode.key = tempNode.key;
|
|
156
120
|
srcNode.val = tempNode.val;
|
|
157
|
-
srcNode.height = tempNode.height;
|
|
158
121
|
}
|
|
159
122
|
return destNode;
|
|
160
123
|
}
|
|
@@ -245,18 +208,18 @@ export class AbstractBinaryTree {
|
|
|
245
208
|
/**
|
|
246
209
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
247
210
|
* values, and adds them to the binary tree.
|
|
248
|
-
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]}
|
|
211
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
249
212
|
* objects, or null values.
|
|
250
213
|
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
251
214
|
* the nodes or node IDs being added. It is used to set the value of each node being added. If `data` is not provided,
|
|
252
215
|
* the value of the nodes will be `undefined`.
|
|
253
216
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
254
217
|
*/
|
|
255
|
-
addMany(
|
|
218
|
+
addMany(keysOrNodes, data) {
|
|
256
219
|
// TODO not sure addMany not be run multi times
|
|
257
220
|
const inserted = [];
|
|
258
|
-
for (let i = 0; i <
|
|
259
|
-
const keyOrNode =
|
|
221
|
+
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
222
|
+
const keyOrNode = keysOrNodes[i];
|
|
260
223
|
if (keyOrNode instanceof AbstractBinaryTreeNode) {
|
|
261
224
|
inserted.push(this.add(keyOrNode.key, keyOrNode.val));
|
|
262
225
|
continue;
|
|
@@ -271,17 +234,17 @@ export class AbstractBinaryTree {
|
|
|
271
234
|
return inserted;
|
|
272
235
|
}
|
|
273
236
|
/**
|
|
274
|
-
* The `
|
|
275
|
-
* @param {(BinaryTreeNodeKey | N)[]}
|
|
237
|
+
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
238
|
+
* @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
276
239
|
* `BinaryTreeNodeKey` or `N` values.
|
|
277
240
|
* @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
278
|
-
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `
|
|
241
|
+
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
279
242
|
* array. Each value in the `data` array will be assigned to the
|
|
280
243
|
* @returns The method is returning a boolean value.
|
|
281
244
|
*/
|
|
282
|
-
|
|
245
|
+
refill(keysOrNodes, data) {
|
|
283
246
|
this.clear();
|
|
284
|
-
return
|
|
247
|
+
return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
|
|
285
248
|
}
|
|
286
249
|
/**
|
|
287
250
|
* The `remove` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
|
|
@@ -338,7 +301,7 @@ export class AbstractBinaryTree {
|
|
|
338
301
|
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter can be one of the following:
|
|
339
302
|
* @returns the depth of the given node or binary tree.
|
|
340
303
|
*/
|
|
341
|
-
getDepth(beginRoot) {
|
|
304
|
+
getDepth(beginRoot = this.root) {
|
|
342
305
|
if (typeof beginRoot === 'number')
|
|
343
306
|
beginRoot = this.get(beginRoot, 'key');
|
|
344
307
|
let depth = 0;
|
|
@@ -355,8 +318,7 @@ export class AbstractBinaryTree {
|
|
|
355
318
|
* node), or `null`.
|
|
356
319
|
* @returns the height of the binary tree.
|
|
357
320
|
*/
|
|
358
|
-
getHeight(beginRoot) {
|
|
359
|
-
beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
|
|
321
|
+
getHeight(beginRoot = this.root) {
|
|
360
322
|
if (typeof beginRoot === 'number')
|
|
361
323
|
beginRoot = this.get(beginRoot, 'key');
|
|
362
324
|
if (!beginRoot)
|
|
@@ -398,9 +360,8 @@ export class AbstractBinaryTree {
|
|
|
398
360
|
* for `beginRoot`, the `this.root` property is used as the default value.
|
|
399
361
|
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
|
|
400
362
|
*/
|
|
401
|
-
getMinHeight(beginRoot) {
|
|
363
|
+
getMinHeight(beginRoot = this.root) {
|
|
402
364
|
var _a, _b, _c;
|
|
403
|
-
beginRoot = beginRoot || this.root;
|
|
404
365
|
if (!beginRoot)
|
|
405
366
|
return -1;
|
|
406
367
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
@@ -450,7 +411,7 @@ export class AbstractBinaryTree {
|
|
|
450
411
|
* tree or null if the tree is empty.
|
|
451
412
|
* @returns The method is returning a boolean value.
|
|
452
413
|
*/
|
|
453
|
-
isPerfectlyBalanced(beginRoot) {
|
|
414
|
+
isPerfectlyBalanced(beginRoot = this.root) {
|
|
454
415
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
455
416
|
}
|
|
456
417
|
/**
|
|
@@ -464,10 +425,9 @@ export class AbstractBinaryTree {
|
|
|
464
425
|
* function will stop traversing the tree and return the first matching node. If `only
|
|
465
426
|
* @returns an array of nodes (type N).
|
|
466
427
|
*/
|
|
467
|
-
getNodes(nodeProperty, propertyName, onlyOne) {
|
|
428
|
+
getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
|
|
468
429
|
if (!this.root)
|
|
469
430
|
return [];
|
|
470
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
|
|
471
431
|
const result = [];
|
|
472
432
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
473
433
|
const _traverse = (cur) => {
|
|
@@ -502,8 +462,7 @@ export class AbstractBinaryTree {
|
|
|
502
462
|
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
|
|
503
463
|
* @returns a boolean value.
|
|
504
464
|
*/
|
|
505
|
-
has(nodeProperty, propertyName) {
|
|
506
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
|
|
465
|
+
has(nodeProperty, propertyName = 'key') {
|
|
507
466
|
// TODO may support finding node by value equal
|
|
508
467
|
return this.getNodes(nodeProperty, propertyName).length > 0;
|
|
509
468
|
}
|
|
@@ -518,9 +477,8 @@ export class AbstractBinaryTree {
|
|
|
518
477
|
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
|
|
519
478
|
* If no matching node is found, it returns null.
|
|
520
479
|
*/
|
|
521
|
-
get(nodeProperty, propertyName) {
|
|
480
|
+
get(nodeProperty, propertyName = 'key') {
|
|
522
481
|
var _a;
|
|
523
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
|
|
524
482
|
// TODO may support finding node by value equal
|
|
525
483
|
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
526
484
|
}
|
|
@@ -557,10 +515,9 @@ export class AbstractBinaryTree {
|
|
|
557
515
|
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
|
|
558
516
|
* node is found (
|
|
559
517
|
*/
|
|
560
|
-
getLeftMost(beginRoot) {
|
|
518
|
+
getLeftMost(beginRoot = this.root) {
|
|
561
519
|
if (typeof beginRoot === 'number')
|
|
562
520
|
beginRoot = this.get(beginRoot, 'key');
|
|
563
|
-
beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
|
|
564
521
|
if (!beginRoot)
|
|
565
522
|
return beginRoot;
|
|
566
523
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
@@ -584,25 +541,24 @@ export class AbstractBinaryTree {
|
|
|
584
541
|
/**
|
|
585
542
|
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
|
|
586
543
|
* recursion optimization.
|
|
587
|
-
* @param {N | null} [
|
|
544
|
+
* @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
588
545
|
* starting node from which we want to find the rightmost node. If no node is provided, the function will default to
|
|
589
546
|
* using the root node of the data structure.
|
|
590
547
|
* @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
|
|
591
548
|
* it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
|
|
592
549
|
* rightmost node starting from the root of the binary tree.
|
|
593
550
|
*/
|
|
594
|
-
getRightMost(
|
|
551
|
+
getRightMost(beginRoot = this.root) {
|
|
595
552
|
// TODO support get right most by passing key in
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
return node;
|
|
553
|
+
if (!beginRoot)
|
|
554
|
+
return beginRoot;
|
|
599
555
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
600
556
|
const _traverse = (cur) => {
|
|
601
557
|
if (!cur.right)
|
|
602
558
|
return cur;
|
|
603
559
|
return _traverse(cur.right);
|
|
604
560
|
};
|
|
605
|
-
return _traverse(
|
|
561
|
+
return _traverse(beginRoot);
|
|
606
562
|
}
|
|
607
563
|
else {
|
|
608
564
|
// Indirect implementation of iteration using tail recursion optimization
|
|
@@ -611,7 +567,7 @@ export class AbstractBinaryTree {
|
|
|
611
567
|
return cur;
|
|
612
568
|
return _traverse.cont(cur.right);
|
|
613
569
|
});
|
|
614
|
-
return _traverse(
|
|
570
|
+
return _traverse(beginRoot);
|
|
615
571
|
}
|
|
616
572
|
}
|
|
617
573
|
/**
|
|
@@ -697,8 +653,7 @@ export class AbstractBinaryTree {
|
|
|
697
653
|
* not provided, it defaults to 'key'.
|
|
698
654
|
* @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
|
|
699
655
|
*/
|
|
700
|
-
subTreeSum(subTreeRoot, propertyName) {
|
|
701
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
|
|
656
|
+
subTreeSum(subTreeRoot, propertyName = 'key') {
|
|
702
657
|
if (typeof subTreeRoot === 'number')
|
|
703
658
|
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
704
659
|
if (!subTreeRoot)
|
|
@@ -748,8 +703,7 @@ export class AbstractBinaryTree {
|
|
|
748
703
|
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
749
704
|
* @returns a boolean value.
|
|
750
705
|
*/
|
|
751
|
-
subTreeAdd(subTreeRoot, delta, propertyName) {
|
|
752
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
|
|
706
|
+
subTreeAdd(subTreeRoot, delta, propertyName = 'key') {
|
|
753
707
|
if (typeof subTreeRoot === 'number')
|
|
754
708
|
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
755
709
|
if (!subTreeRoot)
|
|
@@ -784,14 +738,13 @@ export class AbstractBinaryTree {
|
|
|
784
738
|
return true;
|
|
785
739
|
}
|
|
786
740
|
/**
|
|
787
|
-
* The
|
|
741
|
+
* The bfs function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
|
|
788
742
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
|
|
789
|
-
* If a node is provided, the
|
|
790
|
-
* If a property name is provided, the
|
|
743
|
+
* If a node is provided, the bfs algorithm will be performed starting from that node.
|
|
744
|
+
* If a property name is provided, the bfs algorithm will be performed starting from the root node, accumulating the specified property.
|
|
791
745
|
* @returns An instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
|
|
792
746
|
*/
|
|
793
|
-
|
|
794
|
-
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'key';
|
|
747
|
+
bfs(nodeOrPropertyName = 'key') {
|
|
795
748
|
this._clearResults();
|
|
796
749
|
const queue = [this.root];
|
|
797
750
|
while (queue.length !== 0) {
|
|
@@ -807,15 +760,13 @@ export class AbstractBinaryTree {
|
|
|
807
760
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
808
761
|
}
|
|
809
762
|
/**
|
|
810
|
-
* The
|
|
763
|
+
* The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
|
|
811
764
|
* each node based on the specified pattern and property name.
|
|
812
765
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
813
766
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'key'`.
|
|
814
767
|
* @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
|
|
815
768
|
*/
|
|
816
|
-
|
|
817
|
-
pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
|
|
818
|
-
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'key';
|
|
769
|
+
dfs(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
819
770
|
this._clearResults();
|
|
820
771
|
const _traverse = (node) => {
|
|
821
772
|
switch (pattern) {
|
|
@@ -846,15 +797,13 @@ export class AbstractBinaryTree {
|
|
|
846
797
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
847
798
|
}
|
|
848
799
|
/**
|
|
849
|
-
* The
|
|
800
|
+
* The dfsIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
|
|
850
801
|
* specify the traversal pattern and the property name to accumulate results by.
|
|
851
802
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
852
803
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'key'`.
|
|
853
804
|
* @returns An object of type AbstractBinaryTreeNodeProperties<N>.
|
|
854
805
|
*/
|
|
855
|
-
|
|
856
|
-
pattern = pattern || 'in';
|
|
857
|
-
nodeOrPropertyName = nodeOrPropertyName || 'key';
|
|
806
|
+
dfsIterative(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
858
807
|
this._clearResults();
|
|
859
808
|
if (!this.root)
|
|
860
809
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
@@ -906,9 +855,7 @@ export class AbstractBinaryTree {
|
|
|
906
855
|
* accumulating results based on the 'key' property.
|
|
907
856
|
* @returns An object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
908
857
|
*/
|
|
909
|
-
levelIterative(node, nodeOrPropertyName) {
|
|
910
|
-
nodeOrPropertyName = nodeOrPropertyName || 'key';
|
|
911
|
-
node = node || this.root;
|
|
858
|
+
levelIterative(node = this.root, nodeOrPropertyName = 'key') {
|
|
912
859
|
if (!node)
|
|
913
860
|
return [];
|
|
914
861
|
this._clearResults();
|
|
@@ -933,9 +880,7 @@ export class AbstractBinaryTree {
|
|
|
933
880
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following values: 'key', 'val', or 'node'. If not provided, it defaults to 'key'.
|
|
934
881
|
* @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
|
|
935
882
|
*/
|
|
936
|
-
listLevels(node, nodeOrPropertyName) {
|
|
937
|
-
nodeOrPropertyName = nodeOrPropertyName || 'key';
|
|
938
|
-
node = node || this.root;
|
|
883
|
+
listLevels(node = this.root, nodeOrPropertyName = 'key') {
|
|
939
884
|
if (!node)
|
|
940
885
|
return [];
|
|
941
886
|
const levelsNodes = [];
|
|
@@ -1008,11 +953,9 @@ export class AbstractBinaryTree {
|
|
|
1008
953
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to 'key'.
|
|
1009
954
|
* @returns An array of AbstractBinaryTreeNodeProperties<N> objects.
|
|
1010
955
|
*/
|
|
1011
|
-
morris(pattern, nodeOrPropertyName) {
|
|
956
|
+
morris(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
1012
957
|
if (this.root === null)
|
|
1013
958
|
return [];
|
|
1014
|
-
pattern = pattern || 'in';
|
|
1015
|
-
nodeOrPropertyName = nodeOrPropertyName || 'key';
|
|
1016
959
|
this._clearResults();
|
|
1017
960
|
let cur = this.root;
|
|
1018
961
|
const _reverseEdge = (node) => {
|
|
@@ -1137,27 +1080,6 @@ export class AbstractBinaryTree {
|
|
|
1137
1080
|
_setLoopType(value) {
|
|
1138
1081
|
this._loopType = value;
|
|
1139
1082
|
}
|
|
1140
|
-
/**
|
|
1141
|
-
* The function sets the value of the `_visitedKey` property in a protected manner.
|
|
1142
|
-
* @param {BinaryTreeNodeKey[]} value - value is an array of BinaryTreeNodeKey values.
|
|
1143
|
-
*/
|
|
1144
|
-
_setVisitedKey(value) {
|
|
1145
|
-
this._visitedKey = value;
|
|
1146
|
-
}
|
|
1147
|
-
/**
|
|
1148
|
-
* The function sets the value of the "_visitedVal" property to the given array.
|
|
1149
|
-
* @param value - An array of type N.
|
|
1150
|
-
*/
|
|
1151
|
-
_setVisitedVal(value) {
|
|
1152
|
-
this._visitedVal = value;
|
|
1153
|
-
}
|
|
1154
|
-
/**
|
|
1155
|
-
* The function sets the value of the _visitedNode property.
|
|
1156
|
-
* @param {N[]} value - N[] is an array of elements of type N.
|
|
1157
|
-
*/
|
|
1158
|
-
_setVisitedNode(value) {
|
|
1159
|
-
this._visitedNode = value;
|
|
1160
|
-
}
|
|
1161
1083
|
/**
|
|
1162
1084
|
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
1163
1085
|
* parent property of the value to undefined.
|
|
@@ -1181,9 +1103,9 @@ export class AbstractBinaryTree {
|
|
|
1181
1103
|
* properties.
|
|
1182
1104
|
*/
|
|
1183
1105
|
_clearResults() {
|
|
1184
|
-
this.
|
|
1185
|
-
this.
|
|
1186
|
-
this.
|
|
1106
|
+
this.visitedKey = [];
|
|
1107
|
+
this.visitedVal = [];
|
|
1108
|
+
this.visitedNode = [];
|
|
1187
1109
|
}
|
|
1188
1110
|
/**
|
|
1189
1111
|
* The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
|
|
@@ -1200,24 +1122,24 @@ export class AbstractBinaryTree {
|
|
|
1200
1122
|
* `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
|
|
1201
1123
|
* @returns a boolean value indicating whether only one matching node should be pushed into the result array.
|
|
1202
1124
|
*/
|
|
1203
|
-
_pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne) {
|
|
1125
|
+
_pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName = 'key', onlyOne = false) {
|
|
1204
1126
|
switch (propertyName) {
|
|
1205
1127
|
case 'key':
|
|
1206
1128
|
if (cur.key === nodeProperty) {
|
|
1207
1129
|
result.push(cur);
|
|
1208
|
-
return
|
|
1130
|
+
return onlyOne;
|
|
1209
1131
|
}
|
|
1210
1132
|
break;
|
|
1211
1133
|
case 'val':
|
|
1212
1134
|
if (cur.val === nodeProperty) {
|
|
1213
1135
|
result.push(cur);
|
|
1214
|
-
return
|
|
1136
|
+
return onlyOne;
|
|
1215
1137
|
}
|
|
1216
1138
|
break;
|
|
1217
1139
|
default:
|
|
1218
1140
|
if (cur.key === nodeProperty) {
|
|
1219
1141
|
result.push(cur);
|
|
1220
|
-
return
|
|
1142
|
+
return onlyOne;
|
|
1221
1143
|
}
|
|
1222
1144
|
break;
|
|
1223
1145
|
}
|
|
@@ -1229,25 +1151,24 @@ export class AbstractBinaryTree {
|
|
|
1229
1151
|
* can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
|
|
1230
1152
|
* specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
|
|
1231
1153
|
*/
|
|
1232
|
-
_accumulatedByPropertyName(node, nodeOrPropertyName) {
|
|
1233
|
-
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'key';
|
|
1154
|
+
_accumulatedByPropertyName(node, nodeOrPropertyName = 'key') {
|
|
1234
1155
|
switch (nodeOrPropertyName) {
|
|
1235
1156
|
case 'key':
|
|
1236
|
-
this.
|
|
1157
|
+
this.visitedKey.push(node.key);
|
|
1237
1158
|
break;
|
|
1238
1159
|
case 'val':
|
|
1239
|
-
this.
|
|
1160
|
+
this.visitedVal.push(node.val);
|
|
1240
1161
|
break;
|
|
1241
1162
|
case 'node':
|
|
1242
|
-
this.
|
|
1163
|
+
this.visitedNode.push(node);
|
|
1243
1164
|
break;
|
|
1244
1165
|
default:
|
|
1245
|
-
this.
|
|
1166
|
+
this.visitedKey.push(node.key);
|
|
1246
1167
|
break;
|
|
1247
1168
|
}
|
|
1248
1169
|
}
|
|
1249
1170
|
/**
|
|
1250
|
-
* The time complexity of Morris traversal is O(n), it
|
|
1171
|
+
* The time complexity of Morris traversal is O(n), it may slower than others
|
|
1251
1172
|
* The space complexity Morris traversal is O(1) because no using stack
|
|
1252
1173
|
*/
|
|
1253
1174
|
/**
|
|
@@ -1257,17 +1178,16 @@ export class AbstractBinaryTree {
|
|
|
1257
1178
|
* can accept either a `NodeOrPropertyName` type or be undefined.
|
|
1258
1179
|
* @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
|
|
1259
1180
|
*/
|
|
1260
|
-
_getResultByPropertyName(nodeOrPropertyName) {
|
|
1261
|
-
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'key';
|
|
1181
|
+
_getResultByPropertyName(nodeOrPropertyName = 'key') {
|
|
1262
1182
|
switch (nodeOrPropertyName) {
|
|
1263
1183
|
case 'key':
|
|
1264
|
-
return this.
|
|
1184
|
+
return this.visitedKey;
|
|
1265
1185
|
case 'val':
|
|
1266
|
-
return this.
|
|
1186
|
+
return this.visitedVal;
|
|
1267
1187
|
case 'node':
|
|
1268
|
-
return this.
|
|
1188
|
+
return this.visitedNode;
|
|
1269
1189
|
default:
|
|
1270
|
-
return this.
|
|
1190
|
+
return this.visitedKey;
|
|
1271
1191
|
}
|
|
1272
1192
|
}
|
|
1273
1193
|
}
|
|
@@ -9,6 +9,7 @@ import { BST, BSTNode } from './bst';
|
|
|
9
9
|
import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../../types';
|
|
10
10
|
import { IAVLTree, IAVLTreeNode } from '../../interfaces';
|
|
11
11
|
export declare class AVLTreeNode<V = any, NEIGHBOR extends AVLTreeNode<V, NEIGHBOR> = AVLTreeNodeNested<V>> extends BSTNode<V, NEIGHBOR> implements IAVLTreeNode<V, NEIGHBOR> {
|
|
12
|
+
height: number;
|
|
12
13
|
constructor(key: BinaryTreeNodeKey, val?: V);
|
|
13
14
|
}
|
|
14
15
|
export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
|
|
@@ -19,6 +20,14 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
|
|
|
19
20
|
* options.
|
|
20
21
|
*/
|
|
21
22
|
constructor(options?: AVLTreeOptions);
|
|
23
|
+
/**
|
|
24
|
+
* The `swapLocation` function swaps the location of two nodes in a binary tree.
|
|
25
|
+
* @param {N} srcNode - The source node that you want to swap with the destination node.
|
|
26
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
27
|
+
* be swapped to.
|
|
28
|
+
* @returns The `destNode` is being returned.
|
|
29
|
+
*/
|
|
30
|
+
swapLocation(srcNode: N, destNode: N): N;
|
|
22
31
|
/**
|
|
23
32
|
* The function creates a new AVL tree node with the given key and value.
|
|
24
33
|
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
@@ -9,6 +9,7 @@ import { BST, BSTNode } from './bst';
|
|
|
9
9
|
export class AVLTreeNode extends BSTNode {
|
|
10
10
|
constructor(key, val) {
|
|
11
11
|
super(key, val);
|
|
12
|
+
this.height = 0;
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
15
|
export class AVLTree extends BST {
|
|
@@ -21,6 +22,27 @@ export class AVLTree extends BST {
|
|
|
21
22
|
constructor(options) {
|
|
22
23
|
super(options);
|
|
23
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* The `swapLocation` function swaps the location of two nodes in a binary tree.
|
|
27
|
+
* @param {N} srcNode - The source node that you want to swap with the destination node.
|
|
28
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
29
|
+
* be swapped to.
|
|
30
|
+
* @returns The `destNode` is being returned.
|
|
31
|
+
*/
|
|
32
|
+
swapLocation(srcNode, destNode) {
|
|
33
|
+
const { key, val, height } = destNode;
|
|
34
|
+
const tempNode = this.createNode(key, val);
|
|
35
|
+
if (tempNode) {
|
|
36
|
+
tempNode.height = height;
|
|
37
|
+
destNode.key = srcNode.key;
|
|
38
|
+
destNode.val = srcNode.val;
|
|
39
|
+
destNode.height = srcNode.height;
|
|
40
|
+
srcNode.key = tempNode.key;
|
|
41
|
+
srcNode.val = tempNode.val;
|
|
42
|
+
srcNode.height = tempNode.height;
|
|
43
|
+
}
|
|
44
|
+
return destNode;
|
|
45
|
+
}
|
|
24
46
|
/**
|
|
25
47
|
* The function creates a new AVL tree node with the given key and value.
|
|
26
48
|
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
@@ -40,14 +40,14 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
40
40
|
/**
|
|
41
41
|
* The `addMany` function overrides the base class method to add multiple nodes to a binary search tree in a balanced
|
|
42
42
|
* manner.
|
|
43
|
-
* @param {[BinaryTreeNodeKey | N , N['val']][]}
|
|
43
|
+
* @param {[BinaryTreeNodeKey | N , N['val']][]} keysOrNodes - The `keysOrNodes` parameter in the `addMany` function is an array of
|
|
44
44
|
* `BinaryTreeNodeKey` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
|
|
45
45
|
* to the binary search tree.
|
|
46
46
|
* @param {N['val'][]} data - The values of tree nodes
|
|
47
47
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
48
48
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
49
49
|
*/
|
|
50
|
-
addMany(
|
|
50
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd?: boolean): (N | null | undefined)[];
|
|
51
51
|
/**
|
|
52
52
|
* The function returns the first node in a binary tree that matches the given property name and value.
|
|
53
53
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
@@ -124,7 +124,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
124
124
|
/**
|
|
125
125
|
* The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
|
|
126
126
|
* greater than, less than, or equal to the second ID.
|
|
127
|
-
* @param {BinaryTreeNodeKey} a - a is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
|
|
127
|
+
* @param {BinaryTreeNodeKey} a - "a" is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
|
|
128
128
|
* @param {BinaryTreeNodeKey} b - The parameter "b" in the above code refers to a BinaryTreeNodeKey.
|
|
129
129
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
|
|
130
130
|
* than), or CP.eq (equal).
|