min-heap-typed 1.39.0 → 1.39.2
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 +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +96 -32
- package/dist/data-structures/binary-tree/binary-tree.js +46 -8
- package/dist/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/data-structures/binary-tree/bst.js +2 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/data-structures/graph/abstract-graph.js +1 -1
- package/dist/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/data-structures/graph/map-graph.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/data-structures/matrix/matrix2d.js +3 -7
- package/dist/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/data-structures/matrix/vector2d.js +0 -1
- package/dist/data-structures/queue/deque.d.ts +20 -20
- package/dist/data-structures/queue/deque.js +22 -22
- package/dist/data-structures/queue/queue.d.ts +3 -3
- package/dist/data-structures/queue/queue.js +3 -3
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/types/helpers.d.ts +1 -4
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +76 -90
- package/src/data-structures/binary-tree/bst.ts +9 -16
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -2
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/graph/map-graph.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
- package/src/data-structures/matrix/matrix2d.ts +1 -3
- package/src/data-structures/matrix/vector2d.ts +0 -2
- package/src/data-structures/queue/deque.ts +22 -22
- package/src/data-structures/queue/queue.ts +3 -3
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
- package/src/types/helpers.ts +1 -7
|
@@ -85,44 +85,44 @@ class ObjectDeque {
|
|
|
85
85
|
this._size++;
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
|
-
* The function `
|
|
88
|
+
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
89
89
|
* @returns The value of the first element in the data structure.
|
|
90
90
|
*/
|
|
91
|
-
|
|
91
|
+
popFirst() {
|
|
92
92
|
if (!this._size)
|
|
93
93
|
return;
|
|
94
|
-
const value = this.
|
|
94
|
+
const value = this.getFirst();
|
|
95
95
|
delete this._nodes[this._first];
|
|
96
96
|
this._first++;
|
|
97
97
|
this._size--;
|
|
98
98
|
return value;
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
101
|
-
* The `
|
|
101
|
+
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
102
102
|
* @returns The element at the first position of the `_nodes` array.
|
|
103
103
|
*/
|
|
104
|
-
|
|
104
|
+
getFirst() {
|
|
105
105
|
if (this._size)
|
|
106
106
|
return this._nodes[this._first];
|
|
107
107
|
}
|
|
108
108
|
/**
|
|
109
|
-
* The `
|
|
109
|
+
* The `popLast()` function removes and returns the last element in a data structure.
|
|
110
110
|
* @returns The value that was removed from the data structure.
|
|
111
111
|
*/
|
|
112
|
-
|
|
112
|
+
popLast() {
|
|
113
113
|
if (!this._size)
|
|
114
114
|
return;
|
|
115
|
-
const value = this.
|
|
115
|
+
const value = this.getLast();
|
|
116
116
|
delete this._nodes[this._last];
|
|
117
117
|
this._last--;
|
|
118
118
|
this._size--;
|
|
119
119
|
return value;
|
|
120
120
|
}
|
|
121
121
|
/**
|
|
122
|
-
* The `
|
|
122
|
+
* The `getLast()` function returns the last element in an array-like data structure.
|
|
123
123
|
* @returns The last element in the array "_nodes" is being returned.
|
|
124
124
|
*/
|
|
125
|
-
|
|
125
|
+
getLast() {
|
|
126
126
|
if (this._size)
|
|
127
127
|
return this._nodes[this._last];
|
|
128
128
|
}
|
|
@@ -172,19 +172,19 @@ class ArrayDeque {
|
|
|
172
172
|
return this._nodes.push(value);
|
|
173
173
|
}
|
|
174
174
|
/**
|
|
175
|
-
* The function "
|
|
176
|
-
* @returns The method `
|
|
175
|
+
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
176
|
+
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
177
177
|
*/
|
|
178
|
-
|
|
178
|
+
popLast() {
|
|
179
179
|
var _a;
|
|
180
180
|
return (_a = this._nodes.pop()) !== null && _a !== void 0 ? _a : null;
|
|
181
181
|
}
|
|
182
182
|
/**
|
|
183
|
-
* The `
|
|
184
|
-
* @returns The `
|
|
183
|
+
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
184
|
+
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
185
185
|
* empty.
|
|
186
186
|
*/
|
|
187
|
-
|
|
187
|
+
popFirst() {
|
|
188
188
|
var _a;
|
|
189
189
|
return (_a = this._nodes.shift()) !== null && _a !== void 0 ? _a : null;
|
|
190
190
|
}
|
|
@@ -201,19 +201,19 @@ class ArrayDeque {
|
|
|
201
201
|
return this._nodes.unshift(value);
|
|
202
202
|
}
|
|
203
203
|
/**
|
|
204
|
-
* The `
|
|
205
|
-
* @returns The function `
|
|
204
|
+
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
205
|
+
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
206
206
|
* empty, it will return `null`.
|
|
207
207
|
*/
|
|
208
|
-
|
|
208
|
+
getFirst() {
|
|
209
209
|
var _a;
|
|
210
210
|
return (_a = this._nodes[0]) !== null && _a !== void 0 ? _a : null;
|
|
211
211
|
}
|
|
212
212
|
/**
|
|
213
|
-
* The `
|
|
214
|
-
* @returns The method `
|
|
213
|
+
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
214
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
215
215
|
*/
|
|
216
|
-
|
|
216
|
+
getLast() {
|
|
217
217
|
var _a;
|
|
218
218
|
return (_a = this._nodes[this._nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
|
|
219
219
|
}
|
|
@@ -68,11 +68,11 @@ export declare class Queue<E = any> {
|
|
|
68
68
|
*/
|
|
69
69
|
peek(): E | undefined;
|
|
70
70
|
/**
|
|
71
|
-
* The `
|
|
72
|
-
* @returns The method `
|
|
71
|
+
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
72
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
73
73
|
* array is empty, it returns `null`.
|
|
74
74
|
*/
|
|
75
|
-
|
|
75
|
+
getLast(): E | undefined;
|
|
76
76
|
/**
|
|
77
77
|
* The enqueue function adds a value to the end of a queue.
|
|
78
78
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -109,11 +109,11 @@ class Queue {
|
|
|
109
109
|
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
|
-
* The `
|
|
113
|
-
* @returns The method `
|
|
112
|
+
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
113
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
114
114
|
* array is empty, it returns `null`.
|
|
115
115
|
*/
|
|
116
|
-
|
|
116
|
+
getLast() {
|
|
117
117
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
118
118
|
}
|
|
119
119
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested,
|
|
2
|
+
import { BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, OneParamCallback } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
4
4
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
5
5
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
6
|
-
delete<C extends
|
|
6
|
+
delete<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
7
7
|
}
|
|
@@ -19,8 +19,6 @@ export declare enum FamilyPosition {
|
|
|
19
19
|
MAL_NODE = "MAL_NODE"
|
|
20
20
|
}
|
|
21
21
|
export type BinaryTreeNodeKey = number;
|
|
22
|
-
export type BFSCallback<N, D = any> = (node: N, level?: number) => D;
|
|
23
|
-
export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
|
|
24
22
|
export type BinaryTreeDeletedResult<N> = {
|
|
25
23
|
deleted: N | null | undefined;
|
|
26
24
|
needBalanced: N | null;
|
package/dist/types/helpers.d.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import { BinaryTreeNodeKey } from './data-structures';
|
|
2
1
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
3
2
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
4
|
-
export type
|
|
5
|
-
export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
|
|
6
|
-
export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
|
|
3
|
+
export type OneParamCallback<N, D = any> = (node: N) => D;
|
|
7
4
|
export declare enum CP {
|
|
8
5
|
lt = "lt",
|
|
9
6
|
eq = "eq",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.39.
|
|
3
|
+
"version": "1.39.2",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -131,6 +131,6 @@
|
|
|
131
131
|
"typescript": "^4.9.5"
|
|
132
132
|
},
|
|
133
133
|
"dependencies": {
|
|
134
|
-
"data-structure-typed": "^1.39.
|
|
134
|
+
"data-structure-typed": "^1.39.2"
|
|
135
135
|
}
|
|
136
136
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import {BST, BSTNode} from './bst';
|
|
9
9
|
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../../types';
|
|
10
|
-
import {
|
|
10
|
+
import {OneParamCallback} from '../../types';
|
|
11
11
|
import {IBinaryTree} from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
@@ -73,7 +73,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
73
73
|
* `this._defaultCallbackByKey`
|
|
74
74
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
75
75
|
*/
|
|
76
|
-
override delete<C extends
|
|
76
|
+
override delete<C extends OneParamCallback<N>>(
|
|
77
77
|
identifier: ReturnType<C>,
|
|
78
78
|
callback: C = this._defaultCallbackByKey as C
|
|
79
79
|
): BinaryTreeDeletedResult<N>[] {
|
|
@@ -6,16 +6,8 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type {
|
|
10
|
-
|
|
11
|
-
BFSCallbackReturn,
|
|
12
|
-
BinaryTreeNodeKey,
|
|
13
|
-
BinaryTreeNodeNested,
|
|
14
|
-
BinaryTreeOptions,
|
|
15
|
-
MapCallback,
|
|
16
|
-
MapCallbackReturn
|
|
17
|
-
} from '../../types';
|
|
18
|
-
import {BinaryTreeDeletedResult, DefaultMapCallback, DFSOrderPattern, FamilyPosition, IterationType} from '../../types';
|
|
9
|
+
import type {BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, OneParamCallback} from '../../types';
|
|
10
|
+
import {BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType} from '../../types';
|
|
19
11
|
import {IBinaryTree} from '../../interfaces';
|
|
20
12
|
import {trampoline} from '../../utils';
|
|
21
13
|
import {Queue} from '../queue';
|
|
@@ -285,10 +277,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
285
277
|
return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
|
|
286
278
|
}
|
|
287
279
|
|
|
288
|
-
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N): BinaryTreeDeletedResult<N>[];
|
|
289
|
-
|
|
290
|
-
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
|
|
291
|
-
|
|
292
280
|
/**
|
|
293
281
|
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
294
282
|
* with the parent node that needs to be balanced.
|
|
@@ -303,13 +291,13 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
303
291
|
* included in the result. The `callback` parameter has a default value of
|
|
304
292
|
* `this._defaultCallbackByKey`, which
|
|
305
293
|
*/
|
|
306
|
-
delete<C extends
|
|
307
|
-
identifier: ReturnType<C> |
|
|
294
|
+
delete<C extends OneParamCallback<N>>(
|
|
295
|
+
identifier: ReturnType<C> | null,
|
|
308
296
|
callback: C = this._defaultCallbackByKey as C
|
|
309
297
|
): BinaryTreeDeletedResult<N>[] {
|
|
310
298
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
311
299
|
if (!this.root) return bstDeletedResult;
|
|
312
|
-
if (identifier instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
300
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
313
301
|
|
|
314
302
|
const curr = this.get(identifier, callback);
|
|
315
303
|
if (!curr) return bstDeletedResult;
|
|
@@ -320,7 +308,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
320
308
|
|
|
321
309
|
if (!curr.left) {
|
|
322
310
|
if (!parent) {
|
|
323
|
-
|
|
311
|
+
// Handle the case when there's only one root node
|
|
312
|
+
this._setRoot(null);
|
|
324
313
|
} else {
|
|
325
314
|
const {familyPosition: fp} = curr;
|
|
326
315
|
if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
|
|
@@ -489,29 +478,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
489
478
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
490
479
|
}
|
|
491
480
|
|
|
492
|
-
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N): N[];
|
|
493
|
-
|
|
494
|
-
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): N[];
|
|
495
|
-
|
|
496
|
-
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, onlyOne: boolean): N[];
|
|
497
|
-
|
|
498
|
-
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, onlyOne: boolean): N[];
|
|
499
|
-
|
|
500
|
-
getNodes<C extends MapCallback<N>>(
|
|
501
|
-
identifier: ReturnType<C> | N,
|
|
502
|
-
callback: C,
|
|
503
|
-
onlyOne: boolean,
|
|
504
|
-
beginRoot: N | null
|
|
505
|
-
): N[];
|
|
506
|
-
|
|
507
|
-
getNodes<C extends MapCallback<N>>(
|
|
508
|
-
identifier: ReturnType<C> | N,
|
|
509
|
-
callback: C,
|
|
510
|
-
onlyOne: boolean,
|
|
511
|
-
beginRoot: N | null,
|
|
512
|
-
iterationType: IterationType
|
|
513
|
-
): N[];
|
|
514
|
-
|
|
515
481
|
/**
|
|
516
482
|
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
517
483
|
* recursive or iterative traversal.
|
|
@@ -533,15 +499,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
533
499
|
* traverse the binary tree. It can have two possible values:
|
|
534
500
|
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
535
501
|
*/
|
|
536
|
-
getNodes<C extends
|
|
537
|
-
identifier: ReturnType<C> |
|
|
502
|
+
getNodes<C extends OneParamCallback<N>>(
|
|
503
|
+
identifier: ReturnType<C> | null,
|
|
538
504
|
callback: C = this._defaultCallbackByKey as C,
|
|
539
505
|
onlyOne = false,
|
|
540
506
|
beginRoot: N | null = this.root,
|
|
541
507
|
iterationType = this.iterationType
|
|
542
508
|
): N[] {
|
|
543
509
|
if (!beginRoot) return [];
|
|
544
|
-
if (identifier instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
510
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
545
511
|
const ans: N[] = [];
|
|
546
512
|
|
|
547
513
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -574,14 +540,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
574
540
|
return ans;
|
|
575
541
|
}
|
|
576
542
|
|
|
577
|
-
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N): boolean;
|
|
578
|
-
|
|
579
|
-
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): boolean;
|
|
580
|
-
|
|
581
|
-
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, beginRoot: N | null): boolean;
|
|
582
|
-
|
|
583
|
-
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null): boolean;
|
|
584
|
-
|
|
585
543
|
/**
|
|
586
544
|
* The function checks if a binary tree has a node with a given property or key.
|
|
587
545
|
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
|
|
@@ -599,32 +557,17 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
599
557
|
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
600
558
|
* @returns a boolean value.
|
|
601
559
|
*/
|
|
602
|
-
has<C extends
|
|
603
|
-
identifier: ReturnType<C> |
|
|
560
|
+
has<C extends OneParamCallback<N>>(
|
|
561
|
+
identifier: ReturnType<C> | null,
|
|
604
562
|
callback: C = this._defaultCallbackByKey as C,
|
|
605
563
|
beginRoot = this.root,
|
|
606
564
|
iterationType = this.iterationType
|
|
607
565
|
): boolean {
|
|
608
|
-
if (identifier instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
566
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
609
567
|
// TODO may support finding node by value equal
|
|
610
568
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
611
569
|
}
|
|
612
570
|
|
|
613
|
-
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N): N | null;
|
|
614
|
-
|
|
615
|
-
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): N | null;
|
|
616
|
-
|
|
617
|
-
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, beginRoot: N | null): N | null;
|
|
618
|
-
|
|
619
|
-
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null): N | null;
|
|
620
|
-
|
|
621
|
-
get<C extends MapCallback<N>>(
|
|
622
|
-
identifier: ReturnType<C> | N,
|
|
623
|
-
callback: C,
|
|
624
|
-
beginRoot: N | null,
|
|
625
|
-
iterationType: IterationType
|
|
626
|
-
): N | null;
|
|
627
|
-
|
|
628
571
|
/**
|
|
629
572
|
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
630
573
|
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
|
|
@@ -640,13 +583,13 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
640
583
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
641
584
|
* @returns either the found node (of type N) or null if no node is found.
|
|
642
585
|
*/
|
|
643
|
-
get<C extends
|
|
644
|
-
identifier: ReturnType<C> |
|
|
586
|
+
get<C extends OneParamCallback<N>>(
|
|
587
|
+
identifier: ReturnType<C> | null,
|
|
645
588
|
callback: C = this._defaultCallbackByKey as C,
|
|
646
589
|
beginRoot = this.root,
|
|
647
590
|
iterationType = this.iterationType
|
|
648
591
|
): N | null {
|
|
649
|
-
if (identifier instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
592
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
650
593
|
// TODO may support finding node by value equal
|
|
651
594
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
652
595
|
}
|
|
@@ -750,7 +693,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
750
693
|
* possible values:
|
|
751
694
|
* @returns The function `isSubtreeBST` returns a boolean value.
|
|
752
695
|
*/
|
|
753
|
-
isSubtreeBST(beginRoot: N, iterationType = this.iterationType): boolean {
|
|
696
|
+
isSubtreeBST(beginRoot: N | null, iterationType = this.iterationType): boolean {
|
|
754
697
|
// TODO there is a bug
|
|
755
698
|
if (!beginRoot) return true;
|
|
756
699
|
|
|
@@ -805,16 +748,16 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
805
748
|
* start from the root of the tree.
|
|
806
749
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
807
750
|
* performed on the binary tree. It can have two possible values:
|
|
808
|
-
* @returns The function `subTreeTraverse` returns an array of `
|
|
751
|
+
* @returns The function `subTreeTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
|
|
809
752
|
*/
|
|
810
|
-
subTreeTraverse<C extends
|
|
753
|
+
subTreeTraverse<C extends OneParamCallback<N>>(
|
|
811
754
|
callback: C = this._defaultCallbackByKey as C,
|
|
812
755
|
beginRoot: BinaryTreeNodeKey | N | null = this.root,
|
|
813
756
|
iterationType = this.iterationType
|
|
814
757
|
): ReturnType<C>[] {
|
|
815
758
|
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
816
759
|
|
|
817
|
-
const ans:
|
|
760
|
+
const ans: ReturnType<OneParamCallback<N>>[] = [];
|
|
818
761
|
if (!beginRoot) return ans;
|
|
819
762
|
|
|
820
763
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -852,16 +795,16 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
852
795
|
* is `null`, an empty array will be returned.
|
|
853
796
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
854
797
|
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
855
|
-
* @returns The function `dfs` returns an array of `
|
|
798
|
+
* @returns The function `dfs` returns an array of `ReturnType<OneParamCallback<N>>` values.
|
|
856
799
|
*/
|
|
857
|
-
dfs<C extends
|
|
800
|
+
dfs<C extends OneParamCallback<N>>(
|
|
858
801
|
callback: C = this._defaultCallbackByKey as C,
|
|
859
802
|
pattern: DFSOrderPattern = 'in',
|
|
860
803
|
beginRoot: N | null = this.root,
|
|
861
804
|
iterationType: IterationType = IterationType.ITERATIVE
|
|
862
805
|
): ReturnType<C>[] {
|
|
863
806
|
if (!beginRoot) return [];
|
|
864
|
-
const ans:
|
|
807
|
+
const ans: ReturnType<OneParamCallback<N>>[] = [];
|
|
865
808
|
if (iterationType === IterationType.RECURSIVE) {
|
|
866
809
|
const _traverse = (node: N) => {
|
|
867
810
|
switch (pattern) {
|
|
@@ -930,22 +873,22 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
930
873
|
* function on each node.
|
|
931
874
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
932
875
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
933
|
-
* `
|
|
876
|
+
* `ReturnType<OneParamCallback<N>>`. The default value for this parameter is `this._defaultCallbackByKey
|
|
934
877
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
935
878
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
936
879
|
* will not be performed and an empty array will be returned.
|
|
937
880
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
938
881
|
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
939
|
-
* @returns The function `bfs` returns an array of `
|
|
882
|
+
* @returns The function `bfs` returns an array of `ReturnType<OneParamCallback<N>>[]`.
|
|
940
883
|
*/
|
|
941
|
-
bfs<C extends
|
|
884
|
+
bfs<C extends OneParamCallback<N>>(
|
|
942
885
|
callback: C = this._defaultCallbackByKey as C,
|
|
943
886
|
beginRoot: N | null = this.root,
|
|
944
887
|
iterationType = this.iterationType
|
|
945
888
|
): ReturnType<C>[] {
|
|
946
889
|
if (!beginRoot) return [];
|
|
947
890
|
|
|
948
|
-
const ans:
|
|
891
|
+
const ans: ReturnType<OneParamCallback<N>>[] = [];
|
|
949
892
|
|
|
950
893
|
if (iterationType === IterationType.RECURSIVE) {
|
|
951
894
|
const queue = new Queue<N>([beginRoot]);
|
|
@@ -995,7 +938,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
995
938
|
* level in a binary tree. Each inner array contains the return type of the provided callback
|
|
996
939
|
* function `C` applied to the nodes at that level.
|
|
997
940
|
*/
|
|
998
|
-
listLevels<C extends
|
|
941
|
+
listLevels<C extends OneParamCallback<N>>(
|
|
999
942
|
callback: C = this._defaultCallbackByKey as C,
|
|
1000
943
|
beginRoot: N | null = this.root,
|
|
1001
944
|
iterationType = this.iterationType
|
|
@@ -1054,7 +997,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1054
997
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
1055
998
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
1056
999
|
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
1057
|
-
* tree. It takes a node of type `N` as input and returns a value of type `
|
|
1000
|
+
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<OneParamCallback<N>>`. The
|
|
1058
1001
|
* default value for this parameter is `this._defaultCallbackByKey`.
|
|
1059
1002
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1060
1003
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
@@ -1062,15 +1005,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1062
1005
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
1063
1006
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
1064
1007
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
1065
|
-
* @returns The `morris` function returns an array of `
|
|
1008
|
+
* @returns The `morris` function returns an array of `ReturnType<OneParamCallback<N>>` values.
|
|
1066
1009
|
*/
|
|
1067
|
-
morris<C extends
|
|
1010
|
+
morris<C extends OneParamCallback<N>>(
|
|
1068
1011
|
callback: C = this._defaultCallbackByKey as C,
|
|
1069
1012
|
pattern: DFSOrderPattern = 'in',
|
|
1070
1013
|
beginRoot: N | null = this.root
|
|
1071
1014
|
): ReturnType<C>[] {
|
|
1072
1015
|
if (beginRoot === null) return [];
|
|
1073
|
-
const ans:
|
|
1016
|
+
const ans: ReturnType<OneParamCallback<N>>[] = [];
|
|
1074
1017
|
|
|
1075
1018
|
let cur: N | null | undefined = beginRoot;
|
|
1076
1019
|
const _reverseEdge = (node: N | null | undefined) => {
|
|
@@ -1149,6 +1092,49 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1149
1092
|
return ans;
|
|
1150
1093
|
}
|
|
1151
1094
|
|
|
1095
|
+
/**
|
|
1096
|
+
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1097
|
+
* either an iterative or recursive manner.
|
|
1098
|
+
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
1099
|
+
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
1100
|
+
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
1101
|
+
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1102
|
+
* binary tree nodes in a specific order.
|
|
1103
|
+
*/
|
|
1104
|
+
* [Symbol.iterator](node = this.root): Generator<BinaryTreeNodeKey, void, undefined> {
|
|
1105
|
+
if (!node) {
|
|
1106
|
+
return;
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
if (this.iterationType === IterationType.ITERATIVE) {
|
|
1110
|
+
const stack: (N | null | undefined)[] = [];
|
|
1111
|
+
let current: N | null | undefined = node;
|
|
1112
|
+
|
|
1113
|
+
while (current || stack.length > 0) {
|
|
1114
|
+
while (current) {
|
|
1115
|
+
stack.push(current);
|
|
1116
|
+
current = current.left;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
current = stack.pop();
|
|
1120
|
+
|
|
1121
|
+
if (current) yield current.key;
|
|
1122
|
+
if (current) current = current.right;
|
|
1123
|
+
}
|
|
1124
|
+
} else {
|
|
1125
|
+
|
|
1126
|
+
if (node.left) {
|
|
1127
|
+
yield* this[Symbol.iterator](node.left);
|
|
1128
|
+
}
|
|
1129
|
+
yield node.key;
|
|
1130
|
+
if (node.right) {
|
|
1131
|
+
yield* this[Symbol.iterator](node.right);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
|
|
1152
1138
|
/**
|
|
1153
1139
|
* Swap the data of two nodes in the binary tree.
|
|
1154
1140
|
* @param {N} srcNode - The source node to swap.
|
|
@@ -1177,7 +1163,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1177
1163
|
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
1178
1164
|
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
1179
1165
|
*/
|
|
1180
|
-
protected _defaultCallbackByKey:
|
|
1166
|
+
protected _defaultCallbackByKey: OneParamCallback<N, BinaryTreeNodeKey> = node => node.key;
|
|
1181
1167
|
|
|
1182
1168
|
/**
|
|
1183
1169
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
@@ -5,14 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
BinaryTreeNodeKey,
|
|
10
|
-
BSTComparator,
|
|
11
|
-
BSTNodeNested,
|
|
12
|
-
BSTOptions,
|
|
13
|
-
MapCallback,
|
|
14
|
-
MapCallbackReturn
|
|
15
|
-
} from '../../types';
|
|
8
|
+
import type {BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, OneParamCallback} from '../../types';
|
|
16
9
|
import {CP, IterationType} from '../../types';
|
|
17
10
|
import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
18
11
|
import {IBinaryTree} from '../../interfaces';
|
|
@@ -227,7 +220,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
227
220
|
* callback.
|
|
228
221
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
229
222
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
230
|
-
* value (`BinaryTreeNodeKey`) or a custom callback function (`
|
|
223
|
+
* value (`BinaryTreeNodeKey`) or a custom callback function (`OneParamCallback<N>`) that determines
|
|
231
224
|
* whether a node matches the desired property.
|
|
232
225
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
233
226
|
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
@@ -240,8 +233,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
240
233
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
241
234
|
* matching node is found.
|
|
242
235
|
*/
|
|
243
|
-
override get<C extends
|
|
244
|
-
identifier: ReturnType<C> |
|
|
236
|
+
override get<C extends OneParamCallback<N>>(
|
|
237
|
+
identifier: ReturnType<C> | null,
|
|
245
238
|
callback: C = this._defaultCallbackByKey as C,
|
|
246
239
|
beginRoot = this.root,
|
|
247
240
|
iterationType = this.iterationType
|
|
@@ -291,8 +284,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
291
284
|
* traverse the binary tree. It can have one of the following values:
|
|
292
285
|
* @returns an array of nodes (N[]).
|
|
293
286
|
*/
|
|
294
|
-
override getNodes<C extends
|
|
295
|
-
identifier: ReturnType<C> |
|
|
287
|
+
override getNodes<C extends OneParamCallback<N>>(
|
|
288
|
+
identifier: ReturnType<C> | null,
|
|
296
289
|
callback: C = this._defaultCallbackByKey as C,
|
|
297
290
|
onlyOne = false,
|
|
298
291
|
beginRoot: N | null = this.root,
|
|
@@ -363,16 +356,16 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
363
356
|
* (`BinaryTreeNodeKey`), or `null` to
|
|
364
357
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
365
358
|
* done recursively or iteratively. It can have two possible values:
|
|
366
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of `
|
|
359
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
|
|
367
360
|
*/
|
|
368
|
-
lesserOrGreaterTraverse<C extends
|
|
361
|
+
lesserOrGreaterTraverse<C extends OneParamCallback<N>>(
|
|
369
362
|
callback: C = this._defaultCallbackByKey as C,
|
|
370
363
|
lesserOrGreater: CP = CP.lt,
|
|
371
364
|
targetNode: BinaryTreeNodeKey | N | null = this.root,
|
|
372
365
|
iterationType = this.iterationType
|
|
373
366
|
): ReturnType<C>[] {
|
|
374
367
|
if (typeof targetNode === 'number') targetNode = this.get(targetNode);
|
|
375
|
-
const ans:
|
|
368
|
+
const ans: ReturnType<OneParamCallback<N>>[] = [];
|
|
376
369
|
if (!targetNode) return ans;
|
|
377
370
|
const targetKey = targetNode.key;
|
|
378
371
|
if (!this.root) return ans;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
|
|
9
|
-
import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType,
|
|
9
|
+
import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType, OneParamCallback} from '../../types';
|
|
10
10
|
import {IBinaryTree} from '../../interfaces';
|
|
11
11
|
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
12
12
|
|
|
@@ -274,7 +274,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
274
274
|
* decremented by 1 and
|
|
275
275
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
276
276
|
*/
|
|
277
|
-
override delete<C extends
|
|
277
|
+
override delete<C extends OneParamCallback<N>>(
|
|
278
278
|
identifier: ReturnType<C>,
|
|
279
279
|
callback: C = this._defaultCallbackByKey as C,
|
|
280
280
|
ignoreCount = false
|
|
@@ -196,7 +196,7 @@ export abstract class AbstractGraph<
|
|
|
196
196
|
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
197
197
|
* were removed.
|
|
198
198
|
*/
|
|
199
|
-
|
|
199
|
+
removeManyVertices(vertices: V[] | VertexKey[]): boolean {
|
|
200
200
|
const removed: boolean[] = [];
|
|
201
201
|
for (const v of vertices) {
|
|
202
202
|
removed.push(this.deleteVertex(v));
|
|
@@ -109,9 +109,9 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
|
|
|
109
109
|
*/
|
|
110
110
|
override createVertex(
|
|
111
111
|
key: VertexKey,
|
|
112
|
-
val?: V['val'],
|
|
113
112
|
lat: number = this.origin[0],
|
|
114
|
-
long: number = this.origin[1]
|
|
113
|
+
long: number = this.origin[1],
|
|
114
|
+
val?: V['val']
|
|
115
115
|
): V {
|
|
116
116
|
return new MapVertex(key, lat, long, val) as V;
|
|
117
117
|
}
|