queue-typed 1.49.0 → 1.49.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +10 -12
- package/dist/data-structures/binary-tree/avl-tree.js +3 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +58 -58
- package/dist/data-structures/binary-tree/binary-tree.js +6 -6
- package/dist/data-structures/binary-tree/bst.d.ts +15 -15
- package/dist/data-structures/binary-tree/bst.js +3 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -11
- package/dist/data-structures/binary-tree/rb-tree.js +5 -6
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +14 -14
- package/dist/data-structures/binary-tree/tree-multimap.js +3 -3
- package/dist/data-structures/graph/abstract-graph.d.ts +9 -3
- package/dist/data-structures/graph/abstract-graph.js +27 -31
- package/dist/data-structures/graph/directed-graph.d.ts +8 -1
- package/dist/data-structures/graph/directed-graph.js +1 -8
- package/dist/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +8 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -8
- package/dist/data-structures/hash/hash-map.d.ts +14 -2
- package/dist/data-structures/hash/hash-map.js +19 -8
- package/dist/data-structures/hash/hash-table.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +14 -3
- package/dist/data-structures/heap/heap.js +12 -0
- package/dist/data-structures/heap/max-heap.d.ts +11 -1
- package/dist/data-structures/heap/max-heap.js +10 -7
- package/dist/data-structures/heap/min-heap.d.ts +11 -1
- package/dist/data-structures/heap/min-heap.js +10 -7
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +8 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +6 -7
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -7
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/priority-queue.d.ts +9 -1
- package/dist/data-structures/priority-queue/priority-queue.js +8 -7
- package/dist/data-structures/queue/deque.d.ts +6 -5
- package/dist/data-structures/queue/deque.js +6 -12
- package/dist/data-structures/queue/queue.d.ts +18 -3
- package/dist/data-structures/queue/queue.js +16 -6
- package/dist/data-structures/stack/stack.d.ts +15 -5
- package/dist/data-structures/stack/stack.js +7 -4
- package/dist/data-structures/trie/trie.d.ts +13 -3
- package/dist/data-structures/trie/trie.js +11 -8
- package/dist/interfaces/binary-tree.d.ts +4 -4
- package/dist/types/common.d.ts +32 -8
- package/dist/types/common.js +22 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -22
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +14 -14
- package/src/data-structures/binary-tree/binary-tree.ts +74 -77
- package/src/data-structures/binary-tree/bst.ts +18 -17
- package/src/data-structures/binary-tree/rb-tree.ts +17 -18
- package/src/data-structures/binary-tree/tree-multimap.ts +22 -20
- package/src/data-structures/graph/abstract-graph.ts +35 -25
- package/src/data-structures/graph/directed-graph.ts +2 -2
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +20 -3
- package/src/data-structures/hash/hash-table.ts +3 -3
- package/src/data-structures/heap/heap.ts +14 -3
- package/src/data-structures/heap/max-heap.ts +11 -2
- package/src/data-structures/heap/min-heap.ts +11 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +9 -2
- package/src/data-structures/queue/deque.ts +7 -9
- package/src/data-structures/queue/queue.ts +18 -3
- package/src/data-structures/stack/stack.ts +16 -6
- package/src/data-structures/trie/trie.ts +13 -4
- package/src/interfaces/binary-tree.ts +5 -5
- package/src/types/common.ts +37 -12
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
- package/src/types/data-structures/binary-tree/bst.ts +0 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
|
@@ -7,23 +7,20 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
|
+
BinaryTreeDeleteResult,
|
|
11
|
+
BinaryTreeNested,
|
|
10
12
|
BinaryTreeNodeNested,
|
|
11
13
|
BinaryTreeOptions,
|
|
12
|
-
BTNCallback,
|
|
13
|
-
BTNodeEntry,
|
|
14
|
-
BTNodeExemplar,
|
|
15
|
-
BTNodeKeyOrNode,
|
|
16
|
-
} from '../../types';
|
|
17
|
-
import {
|
|
18
|
-
BinaryTreeNested,
|
|
19
14
|
BinaryTreePrintOptions,
|
|
20
|
-
|
|
15
|
+
BTNCallback,
|
|
16
|
+
BTNEntry,
|
|
17
|
+
BTNExemplar,
|
|
18
|
+
BTNKeyOrNode,
|
|
21
19
|
DFSOrderPattern,
|
|
22
20
|
EntryCallback,
|
|
23
|
-
FamilyPosition,
|
|
24
|
-
IterationType,
|
|
25
21
|
NodeDisplayLayout
|
|
26
22
|
} from '../../types';
|
|
23
|
+
import { FamilyPosition, IterationType } from '../../types';
|
|
27
24
|
import { IBinaryTree } from '../../interfaces';
|
|
28
25
|
import { trampoline } from '../../utils';
|
|
29
26
|
import { Queue } from '../queue';
|
|
@@ -107,14 +104,14 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
107
104
|
|
|
108
105
|
/**
|
|
109
106
|
* The constructor function initializes a binary tree object with optional elements and options.
|
|
110
|
-
* @param [elements] - An optional iterable of
|
|
107
|
+
* @param [elements] - An optional iterable of BTNExemplar objects. These objects represent the
|
|
111
108
|
* elements to be added to the binary tree.
|
|
112
109
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
113
110
|
* configuration options for the binary tree. In this case, it is of type
|
|
114
111
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
115
112
|
* required.
|
|
116
113
|
*/
|
|
117
|
-
constructor(elements?: Iterable<
|
|
114
|
+
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<BinaryTreeOptions<K>>) {
|
|
118
115
|
super();
|
|
119
116
|
if (options) {
|
|
120
117
|
const { iterationType, extractor } = options;
|
|
@@ -172,22 +169,22 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
172
169
|
|
|
173
170
|
/**
|
|
174
171
|
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
|
|
175
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `
|
|
172
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V,N>`.
|
|
176
173
|
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
177
174
|
*/
|
|
178
|
-
isNode(exemplar:
|
|
175
|
+
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
179
176
|
return exemplar instanceof BinaryTreeNode;
|
|
180
177
|
}
|
|
181
178
|
|
|
182
179
|
/**
|
|
183
180
|
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
184
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
181
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
185
182
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
186
183
|
* `exemplarToNode` function. It represents the value associated with the exemplar node. If no value
|
|
187
184
|
* is provided, it will be `undefined`.
|
|
188
185
|
* @returns a value of type N (node), or null, or undefined.
|
|
189
186
|
*/
|
|
190
|
-
exemplarToNode(exemplar:
|
|
187
|
+
exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | null | undefined {
|
|
191
188
|
if (exemplar === undefined) return;
|
|
192
189
|
|
|
193
190
|
let node: N | null | undefined;
|
|
@@ -214,11 +211,11 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
214
211
|
|
|
215
212
|
/**
|
|
216
213
|
* The function checks if a given value is an entry in a binary tree node.
|
|
217
|
-
* @param kne -
|
|
214
|
+
* @param kne - BTNExemplar<K, V,N> - A generic type representing a node in a binary tree. It has
|
|
218
215
|
* two type parameters V and N, representing the value and node type respectively.
|
|
219
216
|
* @returns a boolean value.
|
|
220
217
|
*/
|
|
221
|
-
isEntry(kne:
|
|
218
|
+
isEntry(kne: BTNExemplar<K, V, N>): kne is BTNEntry<K, V> {
|
|
222
219
|
return Array.isArray(kne) && kne.length === 2;
|
|
223
220
|
}
|
|
224
221
|
|
|
@@ -237,7 +234,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
237
234
|
* @param {V} [value] - The value to be inserted into the binary tree.
|
|
238
235
|
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
239
236
|
*/
|
|
240
|
-
add(keyOrNodeOrEntry:
|
|
237
|
+
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | null | undefined {
|
|
241
238
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
242
239
|
if (newNode === undefined) return;
|
|
243
240
|
|
|
@@ -303,11 +300,11 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
303
300
|
*
|
|
304
301
|
* The `addMany` function takes in a collection of nodes and an optional collection of values, and
|
|
305
302
|
* adds each node with its corresponding value to the data structure.
|
|
306
|
-
* @param nodes - An iterable collection of
|
|
303
|
+
* @param nodes - An iterable collection of BTNExemplar objects.
|
|
307
304
|
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
308
305
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
309
306
|
*/
|
|
310
|
-
addMany(nodes: Iterable<
|
|
307
|
+
addMany(nodes: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[] {
|
|
311
308
|
// TODO not sure addMany not be run multi times
|
|
312
309
|
const inserted: (N | null | undefined)[] = [];
|
|
313
310
|
|
|
@@ -338,7 +335,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
338
335
|
* Space Complexity: O(1)
|
|
339
336
|
*/
|
|
340
337
|
|
|
341
|
-
refill(nodesOrKeysOrEntries: Iterable<
|
|
338
|
+
refill(nodesOrKeysOrEntries: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): void {
|
|
342
339
|
this.clear();
|
|
343
340
|
this.addMany(nodesOrKeysOrEntries, values);
|
|
344
341
|
}
|
|
@@ -348,11 +345,11 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
348
345
|
* Space Complexity: O(1)
|
|
349
346
|
*/
|
|
350
347
|
|
|
351
|
-
delete<C extends BTNCallback<N, K>>(identifier: K, callback?: C):
|
|
348
|
+
delete<C extends BTNCallback<N, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<N>[];
|
|
352
349
|
|
|
353
|
-
delete<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C):
|
|
350
|
+
delete<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C): BinaryTreeDeleteResult<N>[];
|
|
354
351
|
|
|
355
|
-
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C):
|
|
352
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C): BinaryTreeDeleteResult<N>[];
|
|
356
353
|
|
|
357
354
|
/**
|
|
358
355
|
* Time Complexity: O(n)
|
|
@@ -367,13 +364,13 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
367
364
|
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
368
365
|
* identifier of the node to be deleted. It is optional and has a default value of
|
|
369
366
|
* `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node.
|
|
370
|
-
* @returns an array of `
|
|
367
|
+
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
|
371
368
|
*/
|
|
372
369
|
delete<C extends BTNCallback<N>>(
|
|
373
370
|
identifier: ReturnType<C> | null | undefined,
|
|
374
371
|
callback: C = this._defaultOneParamCallback as C
|
|
375
|
-
):
|
|
376
|
-
const deletedResult:
|
|
372
|
+
): BinaryTreeDeleteResult<N>[] {
|
|
373
|
+
const deletedResult: BinaryTreeDeleteResult<N>[] = [];
|
|
377
374
|
if (!this.root) return deletedResult;
|
|
378
375
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
379
376
|
callback = (node => node) as C;
|
|
@@ -437,7 +434,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
437
434
|
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
438
435
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
439
436
|
*/
|
|
440
|
-
getDepth(distNode:
|
|
437
|
+
getDepth(distNode: BTNKeyOrNode<K, N>, beginRoot: BTNKeyOrNode<K, N> = this.root): number {
|
|
441
438
|
distNode = this.ensureNode(distNode);
|
|
442
439
|
beginRoot = this.ensureNode(beginRoot);
|
|
443
440
|
let depth = 0;
|
|
@@ -470,7 +467,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
470
467
|
* values:
|
|
471
468
|
* @returns the height of the binary tree.
|
|
472
469
|
*/
|
|
473
|
-
getHeight(beginRoot:
|
|
470
|
+
getHeight(beginRoot: BTNKeyOrNode<K, N> = this.root, iterationType = this.iterationType): number {
|
|
474
471
|
beginRoot = this.ensureNode(beginRoot);
|
|
475
472
|
if (!beginRoot) return -1;
|
|
476
473
|
|
|
@@ -519,7 +516,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
519
516
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
520
517
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
521
518
|
*/
|
|
522
|
-
getMinHeight(beginRoot:
|
|
519
|
+
getMinHeight(beginRoot: BTNKeyOrNode<K, N> = this.root, iterationType = this.iterationType): number {
|
|
523
520
|
beginRoot = this.ensureNode(beginRoot);
|
|
524
521
|
if (!beginRoot) return -1;
|
|
525
522
|
|
|
@@ -579,7 +576,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
579
576
|
* value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
|
|
580
577
|
* @returns a boolean value.
|
|
581
578
|
*/
|
|
582
|
-
isPerfectlyBalanced(beginRoot:
|
|
579
|
+
isPerfectlyBalanced(beginRoot: BTNKeyOrNode<K, N> = this.root): boolean {
|
|
583
580
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
584
581
|
}
|
|
585
582
|
|
|
@@ -592,7 +589,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
592
589
|
identifier: K,
|
|
593
590
|
callback?: C,
|
|
594
591
|
onlyOne?: boolean,
|
|
595
|
-
beginRoot?:
|
|
592
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
596
593
|
iterationType?: IterationType
|
|
597
594
|
): N[];
|
|
598
595
|
|
|
@@ -600,7 +597,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
600
597
|
identifier: N | null | undefined,
|
|
601
598
|
callback?: C,
|
|
602
599
|
onlyOne?: boolean,
|
|
603
|
-
beginRoot?:
|
|
600
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
604
601
|
iterationType?: IterationType
|
|
605
602
|
): N[];
|
|
606
603
|
|
|
@@ -608,7 +605,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
608
605
|
identifier: ReturnType<C>,
|
|
609
606
|
callback: C,
|
|
610
607
|
onlyOne?: boolean,
|
|
611
|
-
beginRoot?:
|
|
608
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
612
609
|
iterationType?: IterationType
|
|
613
610
|
): N[];
|
|
614
611
|
|
|
@@ -641,7 +638,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
641
638
|
identifier: ReturnType<C> | null | undefined,
|
|
642
639
|
callback: C = this._defaultOneParamCallback as C,
|
|
643
640
|
onlyOne = false,
|
|
644
|
-
beginRoot:
|
|
641
|
+
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
|
645
642
|
iterationType = this.iterationType
|
|
646
643
|
): N[] {
|
|
647
644
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
@@ -689,21 +686,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
689
686
|
has<C extends BTNCallback<N, K>>(
|
|
690
687
|
identifier: K,
|
|
691
688
|
callback?: C,
|
|
692
|
-
beginRoot?:
|
|
689
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
693
690
|
iterationType?: IterationType
|
|
694
691
|
): boolean;
|
|
695
692
|
|
|
696
693
|
has<C extends BTNCallback<N, N>>(
|
|
697
694
|
identifier: N | null | undefined,
|
|
698
695
|
callback?: C,
|
|
699
|
-
beginRoot?:
|
|
696
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
700
697
|
iterationType?: IterationType
|
|
701
698
|
): boolean;
|
|
702
699
|
|
|
703
700
|
has<C extends BTNCallback<N>>(
|
|
704
701
|
identifier: ReturnType<C> | null | undefined,
|
|
705
702
|
callback: C,
|
|
706
|
-
beginRoot?:
|
|
703
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
707
704
|
iterationType?: IterationType
|
|
708
705
|
): boolean;
|
|
709
706
|
|
|
@@ -730,7 +727,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
730
727
|
has<C extends BTNCallback<N>>(
|
|
731
728
|
identifier: ReturnType<C> | null | undefined,
|
|
732
729
|
callback: C = this._defaultOneParamCallback as C,
|
|
733
|
-
beginRoot:
|
|
730
|
+
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
|
734
731
|
iterationType = this.iterationType
|
|
735
732
|
): boolean {
|
|
736
733
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
@@ -747,21 +744,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
747
744
|
getNode<C extends BTNCallback<N, K>>(
|
|
748
745
|
identifier: K,
|
|
749
746
|
callback?: C,
|
|
750
|
-
beginRoot?:
|
|
747
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
751
748
|
iterationType?: IterationType
|
|
752
749
|
): N | null | undefined;
|
|
753
750
|
|
|
754
751
|
getNode<C extends BTNCallback<N, N>>(
|
|
755
752
|
identifier: N | null | undefined,
|
|
756
753
|
callback?: C,
|
|
757
|
-
beginRoot?:
|
|
754
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
758
755
|
iterationType?: IterationType
|
|
759
756
|
): N | null | undefined;
|
|
760
757
|
|
|
761
758
|
getNode<C extends BTNCallback<N>>(
|
|
762
759
|
identifier: ReturnType<C>,
|
|
763
760
|
callback: C,
|
|
764
|
-
beginRoot?:
|
|
761
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
765
762
|
iterationType?: IterationType
|
|
766
763
|
): N | null | undefined;
|
|
767
764
|
|
|
@@ -789,7 +786,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
789
786
|
getNode<C extends BTNCallback<N>>(
|
|
790
787
|
identifier: ReturnType<C> | null | undefined,
|
|
791
788
|
callback: C = this._defaultOneParamCallback as C,
|
|
792
|
-
beginRoot:
|
|
789
|
+
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
|
793
790
|
iterationType = this.iterationType
|
|
794
791
|
): N | null | undefined {
|
|
795
792
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
@@ -858,28 +855,28 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
858
855
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
859
856
|
* itself if it is not a valid node key.
|
|
860
857
|
*/
|
|
861
|
-
ensureNode(key:
|
|
858
|
+
ensureNode(key: BTNKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | null | undefined {
|
|
862
859
|
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
863
860
|
}
|
|
864
861
|
|
|
865
862
|
get<C extends BTNCallback<N, K>>(
|
|
866
863
|
identifier: K,
|
|
867
864
|
callback?: C,
|
|
868
|
-
beginRoot?:
|
|
865
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
869
866
|
iterationType?: IterationType
|
|
870
867
|
): V | undefined;
|
|
871
868
|
|
|
872
869
|
get<C extends BTNCallback<N, N>>(
|
|
873
870
|
identifier: N | null | undefined,
|
|
874
871
|
callback?: C,
|
|
875
|
-
beginRoot?:
|
|
872
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
876
873
|
iterationType?: IterationType
|
|
877
874
|
): V | undefined;
|
|
878
875
|
|
|
879
876
|
get<C extends BTNCallback<N>>(
|
|
880
877
|
identifier: ReturnType<C>,
|
|
881
878
|
callback: C,
|
|
882
|
-
beginRoot?:
|
|
879
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
883
880
|
iterationType?: IterationType
|
|
884
881
|
): V | undefined;
|
|
885
882
|
|
|
@@ -908,7 +905,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
908
905
|
get<C extends BTNCallback<N>>(
|
|
909
906
|
identifier: ReturnType<C> | null | undefined,
|
|
910
907
|
callback: C = this._defaultOneParamCallback as C,
|
|
911
|
-
beginRoot:
|
|
908
|
+
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
|
912
909
|
iterationType = this.iterationType
|
|
913
910
|
): V | undefined {
|
|
914
911
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
@@ -952,7 +949,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
952
949
|
* reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
|
|
953
950
|
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
954
951
|
*/
|
|
955
|
-
getPathToRoot(beginRoot:
|
|
952
|
+
getPathToRoot(beginRoot: BTNKeyOrNode<K, N>, isReverse = true): N[] {
|
|
956
953
|
// TODO to support get path through passing key
|
|
957
954
|
const result: N[] = [];
|
|
958
955
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -989,7 +986,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
989
986
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
990
987
|
*/
|
|
991
988
|
getLeftMost(
|
|
992
|
-
beginRoot:
|
|
989
|
+
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
|
993
990
|
iterationType = this.iterationType
|
|
994
991
|
): N | null | undefined {
|
|
995
992
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1035,7 +1032,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1035
1032
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
1036
1033
|
*/
|
|
1037
1034
|
getRightMost(
|
|
1038
|
-
beginRoot:
|
|
1035
|
+
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
|
1039
1036
|
iterationType = this.iterationType
|
|
1040
1037
|
): N | null | undefined {
|
|
1041
1038
|
// TODO support get right most by passing key in
|
|
@@ -1077,7 +1074,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1077
1074
|
* possible values:
|
|
1078
1075
|
* @returns a boolean value.
|
|
1079
1076
|
*/
|
|
1080
|
-
isSubtreeBST(beginRoot:
|
|
1077
|
+
isSubtreeBST(beginRoot: BTNKeyOrNode<K, N>, iterationType = this.iterationType): boolean {
|
|
1081
1078
|
// TODO there is a bug
|
|
1082
1079
|
beginRoot = this.ensureNode(beginRoot);
|
|
1083
1080
|
if (!beginRoot) return true;
|
|
@@ -1138,21 +1135,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1138
1135
|
|
|
1139
1136
|
subTreeTraverse<C extends BTNCallback<N>>(
|
|
1140
1137
|
callback?: C,
|
|
1141
|
-
beginRoot?:
|
|
1138
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
1142
1139
|
iterationType?: IterationType,
|
|
1143
1140
|
includeNull?: false
|
|
1144
1141
|
): ReturnType<C>[];
|
|
1145
1142
|
|
|
1146
1143
|
subTreeTraverse<C extends BTNCallback<N>>(
|
|
1147
1144
|
callback?: C,
|
|
1148
|
-
beginRoot?:
|
|
1145
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
1149
1146
|
iterationType?: IterationType,
|
|
1150
1147
|
includeNull?: undefined
|
|
1151
1148
|
): ReturnType<C>[];
|
|
1152
1149
|
|
|
1153
1150
|
subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
|
|
1154
1151
|
callback?: C,
|
|
1155
|
-
beginRoot?:
|
|
1152
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
1156
1153
|
iterationType?: IterationType,
|
|
1157
1154
|
includeNull?: true
|
|
1158
1155
|
): ReturnType<C>[];
|
|
@@ -1181,7 +1178,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1181
1178
|
*/
|
|
1182
1179
|
subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
|
|
1183
1180
|
callback: C = this._defaultOneParamCallback as C,
|
|
1184
|
-
beginRoot:
|
|
1181
|
+
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
|
1185
1182
|
iterationType = this.iterationType,
|
|
1186
1183
|
includeNull = false
|
|
1187
1184
|
): ReturnType<C>[] {
|
|
@@ -1236,7 +1233,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1236
1233
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
1237
1234
|
* @returns a boolean value.
|
|
1238
1235
|
*/
|
|
1239
|
-
isRealNode(node:
|
|
1236
|
+
isRealNode(node: BTNExemplar<K, V, N>): node is N {
|
|
1240
1237
|
return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
|
|
1241
1238
|
}
|
|
1242
1239
|
|
|
@@ -1245,7 +1242,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1245
1242
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
1246
1243
|
* @returns a boolean value.
|
|
1247
1244
|
*/
|
|
1248
|
-
isNIL(node:
|
|
1245
|
+
isNIL(node: BTNExemplar<K, V, N>) {
|
|
1249
1246
|
return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
|
|
1250
1247
|
}
|
|
1251
1248
|
|
|
@@ -1254,7 +1251,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1254
1251
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
1255
1252
|
* @returns a boolean value.
|
|
1256
1253
|
*/
|
|
1257
|
-
isNodeOrNull(node:
|
|
1254
|
+
isNodeOrNull(node: BTNExemplar<K, V, N>): node is N | null {
|
|
1258
1255
|
return this.isRealNode(node) || node === null;
|
|
1259
1256
|
}
|
|
1260
1257
|
|
|
@@ -1264,14 +1261,14 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1264
1261
|
* data type.
|
|
1265
1262
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
1266
1263
|
*/
|
|
1267
|
-
isNotNodeInstance(potentialKey:
|
|
1264
|
+
isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
|
1268
1265
|
return !(potentialKey instanceof BinaryTreeNode)
|
|
1269
1266
|
}
|
|
1270
1267
|
|
|
1271
1268
|
dfs<C extends BTNCallback<N>>(
|
|
1272
1269
|
callback?: C,
|
|
1273
1270
|
pattern?: DFSOrderPattern,
|
|
1274
|
-
beginRoot?:
|
|
1271
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
1275
1272
|
iterationType?: IterationType,
|
|
1276
1273
|
includeNull?: false
|
|
1277
1274
|
): ReturnType<C>[];
|
|
@@ -1279,7 +1276,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1279
1276
|
dfs<C extends BTNCallback<N>>(
|
|
1280
1277
|
callback?: C,
|
|
1281
1278
|
pattern?: DFSOrderPattern,
|
|
1282
|
-
beginRoot?:
|
|
1279
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
1283
1280
|
iterationType?: IterationType,
|
|
1284
1281
|
includeNull?: undefined
|
|
1285
1282
|
): ReturnType<C>[];
|
|
@@ -1287,7 +1284,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1287
1284
|
dfs<C extends BTNCallback<N | null | undefined>>(
|
|
1288
1285
|
callback?: C,
|
|
1289
1286
|
pattern?: DFSOrderPattern,
|
|
1290
|
-
beginRoot?:
|
|
1287
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
1291
1288
|
iterationType?: IterationType,
|
|
1292
1289
|
includeNull?: true
|
|
1293
1290
|
): ReturnType<C>[];
|
|
@@ -1318,7 +1315,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1318
1315
|
dfs<C extends BTNCallback<N | null | undefined>>(
|
|
1319
1316
|
callback: C = this._defaultOneParamCallback as C,
|
|
1320
1317
|
pattern: DFSOrderPattern = 'in',
|
|
1321
|
-
beginRoot:
|
|
1318
|
+
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
|
1322
1319
|
iterationType: IterationType = IterationType.ITERATIVE,
|
|
1323
1320
|
includeNull = false
|
|
1324
1321
|
): ReturnType<C>[] {
|
|
@@ -1417,21 +1414,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1417
1414
|
|
|
1418
1415
|
bfs<C extends BTNCallback<N>>(
|
|
1419
1416
|
callback?: C,
|
|
1420
|
-
beginRoot?:
|
|
1417
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
1421
1418
|
iterationType?: IterationType,
|
|
1422
1419
|
includeNull?: false
|
|
1423
1420
|
): ReturnType<C>[];
|
|
1424
1421
|
|
|
1425
1422
|
bfs<C extends BTNCallback<N>>(
|
|
1426
1423
|
callback?: C,
|
|
1427
|
-
beginRoot?:
|
|
1424
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
1428
1425
|
iterationType?: IterationType,
|
|
1429
1426
|
includeNull?: undefined
|
|
1430
1427
|
): ReturnType<C>[];
|
|
1431
1428
|
|
|
1432
1429
|
bfs<C extends BTNCallback<N | null | undefined>>(
|
|
1433
1430
|
callback?: C,
|
|
1434
|
-
beginRoot?:
|
|
1431
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
1435
1432
|
iterationType?: IterationType,
|
|
1436
1433
|
includeNull?: true
|
|
1437
1434
|
): ReturnType<C>[];
|
|
@@ -1459,7 +1456,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1459
1456
|
*/
|
|
1460
1457
|
bfs<C extends BTNCallback<N | null | undefined>>(
|
|
1461
1458
|
callback: C = this._defaultOneParamCallback as C,
|
|
1462
|
-
beginRoot:
|
|
1459
|
+
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
|
1463
1460
|
iterationType = this.iterationType,
|
|
1464
1461
|
includeNull = false
|
|
1465
1462
|
): ReturnType<C>[] {
|
|
@@ -1518,21 +1515,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1518
1515
|
|
|
1519
1516
|
listLevels<C extends BTNCallback<N>>(
|
|
1520
1517
|
callback?: C,
|
|
1521
|
-
beginRoot?:
|
|
1518
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
1522
1519
|
iterationType?: IterationType,
|
|
1523
1520
|
includeNull?: false
|
|
1524
1521
|
): ReturnType<C>[][];
|
|
1525
1522
|
|
|
1526
1523
|
listLevels<C extends BTNCallback<N>>(
|
|
1527
1524
|
callback?: C,
|
|
1528
|
-
beginRoot?:
|
|
1525
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
1529
1526
|
iterationType?: IterationType,
|
|
1530
1527
|
includeNull?: undefined
|
|
1531
1528
|
): ReturnType<C>[][];
|
|
1532
1529
|
|
|
1533
1530
|
listLevels<C extends BTNCallback<N | null | undefined>>(
|
|
1534
1531
|
callback?: C,
|
|
1535
|
-
beginRoot?:
|
|
1532
|
+
beginRoot?: BTNKeyOrNode<K, N>,
|
|
1536
1533
|
iterationType?: IterationType,
|
|
1537
1534
|
includeNull?: true
|
|
1538
1535
|
): ReturnType<C>[][];
|
|
@@ -1560,7 +1557,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1560
1557
|
*/
|
|
1561
1558
|
listLevels<C extends BTNCallback<N | null | undefined>>(
|
|
1562
1559
|
callback: C = this._defaultOneParamCallback as C,
|
|
1563
|
-
beginRoot:
|
|
1560
|
+
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
|
1564
1561
|
iterationType = this.iterationType,
|
|
1565
1562
|
includeNull = false
|
|
1566
1563
|
): ReturnType<C>[][] {
|
|
@@ -1677,7 +1674,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1677
1674
|
morris<C extends BTNCallback<N>>(
|
|
1678
1675
|
callback: C = this._defaultOneParamCallback as C,
|
|
1679
1676
|
pattern: DFSOrderPattern = 'in',
|
|
1680
|
-
beginRoot:
|
|
1677
|
+
beginRoot: BTNKeyOrNode<K, N> = this.root
|
|
1681
1678
|
): ReturnType<C>[] {
|
|
1682
1679
|
beginRoot = this.ensureNode(beginRoot);
|
|
1683
1680
|
if (beginRoot === null) return [];
|
|
@@ -1856,7 +1853,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1856
1853
|
* following types:
|
|
1857
1854
|
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
|
1858
1855
|
*/
|
|
1859
|
-
print(beginRoot:
|
|
1856
|
+
print(beginRoot: BTNKeyOrNode<K, N> = this.root, options?: BinaryTreePrintOptions): void {
|
|
1860
1857
|
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
1861
1858
|
beginRoot = this.ensureNode(beginRoot);
|
|
1862
1859
|
if (!beginRoot) return;
|
|
@@ -1968,7 +1965,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1968
1965
|
* @param {N} destNode - The destination node to swap.
|
|
1969
1966
|
* @returns {N} - The destination node after the swap.
|
|
1970
1967
|
*/
|
|
1971
|
-
protected _swapProperties(srcNode:
|
|
1968
|
+
protected _swapProperties(srcNode: BTNKeyOrNode<K, N>, destNode: BTNKeyOrNode<K, N>): N | undefined {
|
|
1972
1969
|
srcNode = this.ensureNode(srcNode);
|
|
1973
1970
|
destNode = this.ensureNode(destNode);
|
|
1974
1971
|
|
|
@@ -2026,7 +2023,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
2026
2023
|
* the binary tree. If neither the left nor right child is available, the function returns undefined.
|
|
2027
2024
|
* If the parent node is null, the function also returns undefined.
|
|
2028
2025
|
*/
|
|
2029
|
-
protected _addTo(newNode: N | null | undefined, parent:
|
|
2026
|
+
protected _addTo(newNode: N | null | undefined, parent: BTNKeyOrNode<K, N>): N | null | undefined {
|
|
2030
2027
|
if (this.isNotNodeInstance(parent)) parent = this.getNode(parent);
|
|
2031
2028
|
|
|
2032
2029
|
if (parent) {
|