min-heap-typed 1.39.1 → 1.39.3
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 -10
- package/dist/data-structures/binary-tree/avl-tree.js +4 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +58 -107
- package/dist/data-structures/binary-tree/binary-tree.js +65 -27
- package/dist/data-structures/binary-tree/bst.d.ts +22 -22
- package/dist/data-structures/binary-tree/bst.js +12 -12
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +14 -14
- package/dist/data-structures/binary-tree/tree-multiset.js +7 -7
- 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/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 +4 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/types/helpers.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +10 -10
- package/src/data-structures/binary-tree/binary-tree.ts +165 -53
- package/src/data-structures/binary-tree/bst.ts +29 -31
- package/src/data-structures/binary-tree/rb-tree.ts +5 -5
- package/src/data-structures/binary-tree/tree-multiset.ts +14 -14
- 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/queue/deque.ts +22 -22
- package/src/data-structures/queue/queue.ts +3 -3
- package/src/interfaces/binary-tree.ts +4 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -2
- package/src/types/helpers.ts +1 -1
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type {BTNKey, BSTComparator, BSTNodeNested, BSTOptions, BTNCallback} from '../../types';
|
|
9
9
|
import {CP, IterationType} from '../../types';
|
|
10
10
|
import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
11
11
|
import {IBinaryTree} from '../../interfaces';
|
|
12
12
|
import {Queue} from '../queue';
|
|
13
13
|
|
|
14
14
|
export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
15
|
-
constructor(key:
|
|
15
|
+
constructor(key: BTNKey, val?: V) {
|
|
16
16
|
super(key, val);
|
|
17
17
|
}
|
|
18
18
|
}
|
|
@@ -39,27 +39,27 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
41
|
* The function creates a new binary search tree node with the given key and value.
|
|
42
|
-
* @param {
|
|
42
|
+
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
43
43
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
44
44
|
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It
|
|
45
45
|
* represents the value associated with the node in a binary search tree.
|
|
46
46
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
47
47
|
*/
|
|
48
|
-
override createNode(key:
|
|
48
|
+
override createNode(key: BTNKey, val?: V): N {
|
|
49
49
|
return new BSTNode<V, N>(key, val) as N;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
54
54
|
* into the tree.
|
|
55
|
-
* @param {
|
|
56
|
-
* `
|
|
55
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
56
|
+
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
57
57
|
* @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
|
|
58
58
|
* binary search tree.
|
|
59
59
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
60
60
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
61
61
|
*/
|
|
62
|
-
override add(keyOrNode:
|
|
62
|
+
override add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
|
|
63
63
|
// TODO support node as a parameter
|
|
64
64
|
let inserted: N | null = null;
|
|
65
65
|
let newNode: N | null = null;
|
|
@@ -128,9 +128,9 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
128
128
|
/**
|
|
129
129
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
130
130
|
* maintaining balance.
|
|
131
|
-
* @param {[
|
|
131
|
+
* @param {[BTNKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
132
132
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
133
|
-
* array of `
|
|
133
|
+
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
134
134
|
* `null
|
|
135
135
|
* @param {V[]} data - The values of tree nodes
|
|
136
136
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
@@ -140,13 +140,13 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
140
140
|
*/
|
|
141
141
|
|
|
142
142
|
override addMany(
|
|
143
|
-
keysOrNodes: (
|
|
143
|
+
keysOrNodes: (BTNKey | null)[] | (N | null)[],
|
|
144
144
|
data?: V[],
|
|
145
145
|
isBalanceAdd = true,
|
|
146
146
|
iterationType = this.iterationType
|
|
147
147
|
): (N | null | undefined)[] {
|
|
148
148
|
// TODO this addMany function is inefficient, it should be optimized
|
|
149
|
-
function hasNoNull(arr: (
|
|
149
|
+
function hasNoNull(arr: (BTNKey | null)[] | (N | null)[]): arr is BTNKey[] | N[] {
|
|
150
150
|
return arr.indexOf(null) === -1;
|
|
151
151
|
}
|
|
152
152
|
|
|
@@ -154,17 +154,15 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
154
154
|
return super.addMany(keysOrNodes, data);
|
|
155
155
|
}
|
|
156
156
|
const inserted: (N | null | undefined)[] = [];
|
|
157
|
-
const combinedArr: [
|
|
157
|
+
const combinedArr: [BTNKey | N, N['val']][] = keysOrNodes.map((value, index) => [value, data?.[index]]);
|
|
158
158
|
let sorted = [];
|
|
159
159
|
|
|
160
|
-
function isNodeOrNullTuple(arr: [
|
|
160
|
+
function isNodeOrNullTuple(arr: [BTNKey | N, N['val']][]): arr is [N, N['val']][] {
|
|
161
161
|
for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
|
|
162
162
|
return false;
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
function isBinaryTreeKeyOrNullTuple(
|
|
166
|
-
arr: [BinaryTreeNodeKey | N, N['val']][]
|
|
167
|
-
): arr is [BinaryTreeNodeKey, N['val']][] {
|
|
165
|
+
function isBinaryTreeKeyOrNullTuple(arr: [BTNKey | N, N['val']][]): arr is [BTNKey, N['val']][] {
|
|
168
166
|
for (const [keyOrNode] of arr) if (typeof keyOrNode === 'number') return true;
|
|
169
167
|
return false;
|
|
170
168
|
}
|
|
@@ -181,7 +179,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
181
179
|
}
|
|
182
180
|
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
183
181
|
sortedData = sorted.map(([, val]) => val);
|
|
184
|
-
const recursive = (arr: (
|
|
182
|
+
const recursive = (arr: (BTNKey | null | N)[], data?: (V | undefined)[]) => {
|
|
185
183
|
if (arr.length === 0) return;
|
|
186
184
|
|
|
187
185
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
@@ -221,7 +219,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
221
219
|
* callback.
|
|
222
220
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
223
221
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
224
|
-
* value (`
|
|
222
|
+
* value (`BTNKey`) or a custom callback function (`BTNCallback<N>`) that determines
|
|
225
223
|
* whether a node matches the desired property.
|
|
226
224
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
227
225
|
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
@@ -234,7 +232,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
234
232
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
235
233
|
* matching node is found.
|
|
236
234
|
*/
|
|
237
|
-
override get<C extends
|
|
235
|
+
override get<C extends BTNCallback<N>>(
|
|
238
236
|
identifier: ReturnType<C> | null,
|
|
239
237
|
callback: C = this._defaultCallbackByKey as C,
|
|
240
238
|
beginRoot = this.root,
|
|
@@ -258,7 +256,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
258
256
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
259
257
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
260
258
|
*/
|
|
261
|
-
lastKey(beginRoot: N | null = this.root, iterationType = this.iterationType):
|
|
259
|
+
lastKey(beginRoot: N | null = this.root, iterationType = this.iterationType): BTNKey {
|
|
262
260
|
if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
263
261
|
else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
|
|
264
262
|
else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
@@ -268,7 +266,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
268
266
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
269
267
|
* using either recursive or iterative traversal.
|
|
270
268
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
|
|
271
|
-
* of the binary tree node that you want to search for. It can be either a `
|
|
269
|
+
* of the binary tree node that you want to search for. It can be either a `BTNKey` or a
|
|
272
270
|
* generic type `N`.
|
|
273
271
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
274
272
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
@@ -285,7 +283,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
285
283
|
* traverse the binary tree. It can have one of the following values:
|
|
286
284
|
* @returns an array of nodes (N[]).
|
|
287
285
|
*/
|
|
288
|
-
override getNodes<C extends
|
|
286
|
+
override getNodes<C extends BTNCallback<N>>(
|
|
289
287
|
identifier: ReturnType<C> | null,
|
|
290
288
|
callback: C = this._defaultCallbackByKey as C,
|
|
291
289
|
onlyOne = false,
|
|
@@ -351,22 +349,22 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
351
349
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
352
350
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
353
351
|
* of the following values:
|
|
354
|
-
* @param {
|
|
352
|
+
* @param {BTNKey | N | null} targetNode - The `targetNode` parameter in the
|
|
355
353
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
356
354
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
357
|
-
* (`
|
|
355
|
+
* (`BTNKey`), or `null` to
|
|
358
356
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
359
357
|
* done recursively or iteratively. It can have two possible values:
|
|
360
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<
|
|
358
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
361
359
|
*/
|
|
362
|
-
lesserOrGreaterTraverse<C extends
|
|
360
|
+
lesserOrGreaterTraverse<C extends BTNCallback<N>>(
|
|
363
361
|
callback: C = this._defaultCallbackByKey as C,
|
|
364
362
|
lesserOrGreater: CP = CP.lt,
|
|
365
|
-
targetNode:
|
|
363
|
+
targetNode: BTNKey | N | null = this.root,
|
|
366
364
|
iterationType = this.iterationType
|
|
367
365
|
): ReturnType<C>[] {
|
|
368
366
|
if (typeof targetNode === 'number') targetNode = this.get(targetNode);
|
|
369
|
-
const ans: ReturnType<
|
|
367
|
+
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
370
368
|
if (!targetNode) return ans;
|
|
371
369
|
const targetKey = targetNode.key;
|
|
372
370
|
if (!this.root) return ans;
|
|
@@ -509,12 +507,12 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
509
507
|
/**
|
|
510
508
|
* The function compares two values using a comparator function and returns whether the first value
|
|
511
509
|
* is greater than, less than, or equal to the second value.
|
|
512
|
-
* @param {
|
|
513
|
-
* @param {
|
|
510
|
+
* @param {BTNKey} a - The parameter "a" is of type BTNKey.
|
|
511
|
+
* @param {BTNKey} b - The parameter "b" in the above code represents a BTNKey.
|
|
514
512
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
515
513
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
516
514
|
*/
|
|
517
|
-
protected _compare(a:
|
|
515
|
+
protected _compare(a: BTNKey, b: BTNKey): CP {
|
|
518
516
|
const compared = this._comparator(a, b);
|
|
519
517
|
if (compared > 0) return CP.gt;
|
|
520
518
|
else if (compared < 0) return CP.lt;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {BTNKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types';
|
|
2
2
|
import {IBinaryTree} from '../../interfaces';
|
|
3
3
|
import {BST, BSTNode} from './bst';
|
|
4
4
|
|
|
5
5
|
export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
6
|
-
constructor(key:
|
|
6
|
+
constructor(key: BTNKey, val?: V) {
|
|
7
7
|
super(key, val);
|
|
8
8
|
this._color = RBColor.RED;
|
|
9
9
|
}
|
|
@@ -27,11 +27,11 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
27
27
|
super(options);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
override createNode(key:
|
|
30
|
+
override createNode(key: BTNKey, val?: V): N {
|
|
31
31
|
return new RBTreeNode(key, val) as N;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
// override add(keyOrNode:
|
|
34
|
+
// override add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
|
|
35
35
|
// const inserted = super.add(keyOrNode, val);
|
|
36
36
|
// if (inserted) this._fixInsertViolation(inserted);
|
|
37
37
|
// return inserted;
|
|
@@ -205,7 +205,7 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
205
205
|
// node.right = null;
|
|
206
206
|
// }
|
|
207
207
|
//
|
|
208
|
-
// override delete(keyOrNode:
|
|
208
|
+
// override delete(keyOrNode: BTNKey | N): BinaryTreeDeletedResult<N>[] {
|
|
209
209
|
// const node = this.get(keyOrNode);
|
|
210
210
|
// const result: BinaryTreeDeletedResult<N>[] = [{deleted: undefined, needBalanced: null}];
|
|
211
211
|
// if (!node) return result; // Node does not exist
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType,
|
|
8
|
+
import type {BTNKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
|
|
9
|
+
import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType, BTNCallback} from '../../types';
|
|
10
10
|
import {IBinaryTree} from '../../interfaces';
|
|
11
11
|
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
12
12
|
|
|
@@ -18,7 +18,7 @@ export class TreeMultisetNode<
|
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
21
|
-
* @param {
|
|
21
|
+
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
22
22
|
* of the binary tree node.
|
|
23
23
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
24
24
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -26,7 +26,7 @@ export class TreeMultisetNode<
|
|
|
26
26
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
27
27
|
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
28
28
|
*/
|
|
29
|
-
constructor(key:
|
|
29
|
+
constructor(key: BTNKey, val?: V, count = 1) {
|
|
30
30
|
super(key, val);
|
|
31
31
|
this.count = count;
|
|
32
32
|
}
|
|
@@ -57,22 +57,22 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
57
57
|
|
|
58
58
|
/**
|
|
59
59
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
60
|
-
* @param {
|
|
60
|
+
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
61
61
|
* distinguish one node from another in the tree.
|
|
62
62
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
63
63
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
64
64
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
65
65
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
66
66
|
*/
|
|
67
|
-
override createNode(key:
|
|
67
|
+
override createNode(key: BTNKey, val?: V, count?: number): N {
|
|
68
68
|
return new TreeMultisetNode(key, val, count) as N;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
72
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
73
73
|
* exists, and balancing the tree if necessary.
|
|
74
|
-
* @param {
|
|
75
|
-
* `
|
|
74
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
75
|
+
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
76
76
|
* node to be added), or `null` (which represents a null node).
|
|
77
77
|
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
|
78
78
|
* added to the binary tree.
|
|
@@ -81,7 +81,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
81
81
|
* count is specified, the default count will be 1.
|
|
82
82
|
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
83
83
|
*/
|
|
84
|
-
override add(keyOrNode:
|
|
84
|
+
override add(keyOrNode: BTNKey | N | null, val?: V, count = 1): N | null | undefined {
|
|
85
85
|
let inserted: N | null | undefined = undefined,
|
|
86
86
|
newNode: N | null;
|
|
87
87
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
@@ -185,14 +185,14 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
185
185
|
/**
|
|
186
186
|
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
187
187
|
* inserted nodes.
|
|
188
|
-
* @param {(
|
|
189
|
-
* added to the multiset. Each element can be either a
|
|
188
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
189
|
+
* added to the multiset. Each element can be either a BTNKey or a TreeMultisetNode.
|
|
190
190
|
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
|
|
191
191
|
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
192
192
|
* each key or node.
|
|
193
193
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
194
194
|
*/
|
|
195
|
-
override addMany(keysOrNodes: (
|
|
195
|
+
override addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[] {
|
|
196
196
|
const inserted: (N | null | undefined)[] = [];
|
|
197
197
|
|
|
198
198
|
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
@@ -263,7 +263,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
263
263
|
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
264
264
|
* node along with the parent node that needs to be balanced.
|
|
265
265
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
266
|
-
* `
|
|
266
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
267
267
|
* searching for. It can be a specific key value or any other property of the node.
|
|
268
268
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
269
269
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
@@ -275,7 +275,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
275
275
|
* decremented by 1 and
|
|
276
276
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
277
277
|
*/
|
|
278
|
-
override delete<C extends
|
|
278
|
+
override delete<C extends BTNCallback<N>>(
|
|
279
279
|
identifier: ReturnType<C>,
|
|
280
280
|
callback: C = this._defaultCallbackByKey as C,
|
|
281
281
|
ignoreCount = false
|
|
@@ -147,11 +147,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
/**
|
|
150
|
-
* The `
|
|
150
|
+
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
151
151
|
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
|
152
152
|
* list is empty, it returns null.
|
|
153
153
|
*/
|
|
154
|
-
|
|
154
|
+
popLast(): E | undefined {
|
|
155
155
|
return this.pop();
|
|
156
156
|
}
|
|
157
157
|
|
|
@@ -175,11 +175,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
/**
|
|
178
|
-
* The `
|
|
178
|
+
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
179
179
|
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
180
180
|
* list.
|
|
181
181
|
*/
|
|
182
|
-
|
|
182
|
+
popFirst(): E | undefined {
|
|
183
183
|
return this.shift();
|
|
184
184
|
}
|
|
185
185
|
|
|
@@ -211,18 +211,18 @@ export class DoublyLinkedList<E = any> {
|
|
|
211
211
|
}
|
|
212
212
|
|
|
213
213
|
/**
|
|
214
|
-
* The `
|
|
215
|
-
* @returns The method `
|
|
214
|
+
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
|
|
215
|
+
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
|
216
216
|
*/
|
|
217
|
-
|
|
217
|
+
getFirst(): E | undefined {
|
|
218
218
|
return this.head?.val;
|
|
219
219
|
}
|
|
220
220
|
|
|
221
221
|
/**
|
|
222
|
-
* The `
|
|
223
|
-
* @returns The method `
|
|
222
|
+
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
|
|
223
|
+
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
|
|
224
224
|
*/
|
|
225
|
-
|
|
225
|
+
getLast(): E | undefined {
|
|
226
226
|
return this.tail?.val;
|
|
227
227
|
}
|
|
228
228
|
|
|
@@ -266,7 +266,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
266
266
|
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
|
|
267
267
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
268
268
|
*/
|
|
269
|
-
|
|
269
|
+
getNode(val: E | null): DoublyLinkedListNode<E> | null {
|
|
270
270
|
let current = this.head;
|
|
271
271
|
|
|
272
272
|
while (current) {
|
|
@@ -310,6 +310,43 @@ export class DoublyLinkedList<E = any> {
|
|
|
310
310
|
return true;
|
|
311
311
|
}
|
|
312
312
|
|
|
313
|
+
/**
|
|
314
|
+
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
315
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
316
|
+
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
317
|
+
* itself.
|
|
318
|
+
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
319
|
+
* list.
|
|
320
|
+
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
321
|
+
* insertion fails.
|
|
322
|
+
*/
|
|
323
|
+
insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
|
|
324
|
+
let existingNode;
|
|
325
|
+
|
|
326
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
327
|
+
existingNode = existingValueOrNode;
|
|
328
|
+
} else {
|
|
329
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (existingNode) {
|
|
333
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
334
|
+
newNode.prev = existingNode.prev;
|
|
335
|
+
if (existingNode.prev) {
|
|
336
|
+
existingNode.prev.next = newNode;
|
|
337
|
+
}
|
|
338
|
+
newNode.next = existingNode;
|
|
339
|
+
existingNode.prev = newNode;
|
|
340
|
+
if (existingNode === this.head) {
|
|
341
|
+
this.head = newNode;
|
|
342
|
+
}
|
|
343
|
+
this._length++;
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
|
|
313
350
|
/**
|
|
314
351
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
315
352
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
@@ -331,9 +368,6 @@ export class DoublyLinkedList<E = any> {
|
|
|
331
368
|
return removedNode!.val;
|
|
332
369
|
}
|
|
333
370
|
|
|
334
|
-
delete(valOrNode: E): boolean;
|
|
335
|
-
delete(valOrNode: DoublyLinkedListNode<E>): boolean;
|
|
336
|
-
|
|
337
371
|
/**
|
|
338
372
|
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
|
|
339
373
|
* @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
|
|
@@ -347,7 +381,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
347
381
|
if (valOrNode instanceof DoublyLinkedListNode) {
|
|
348
382
|
node = valOrNode;
|
|
349
383
|
} else {
|
|
350
|
-
node = this.
|
|
384
|
+
node = this.getNode(valOrNode);
|
|
351
385
|
}
|
|
352
386
|
|
|
353
387
|
if (node) {
|
|
@@ -437,14 +471,14 @@ export class DoublyLinkedList<E = any> {
|
|
|
437
471
|
}
|
|
438
472
|
|
|
439
473
|
/**
|
|
440
|
-
* The `
|
|
474
|
+
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
441
475
|
* value that satisfies the given callback function, or null if no value satisfies the callback.
|
|
442
476
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
443
477
|
* function is used to determine whether a given value satisfies a certain condition.
|
|
444
|
-
* @returns The method `
|
|
478
|
+
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
445
479
|
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
446
480
|
*/
|
|
447
|
-
|
|
481
|
+
findBackward(callback: (val: E) => boolean): E | null {
|
|
448
482
|
let current = this.tail;
|
|
449
483
|
while (current) {
|
|
450
484
|
if (callback(current.val)) {
|
|
@@ -456,10 +490,10 @@ export class DoublyLinkedList<E = any> {
|
|
|
456
490
|
}
|
|
457
491
|
|
|
458
492
|
/**
|
|
459
|
-
* The `
|
|
460
|
-
* @returns The `
|
|
493
|
+
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
|
|
494
|
+
* @returns The `toArrayBackward()` function returns an array of type `E[]`.
|
|
461
495
|
*/
|
|
462
|
-
|
|
496
|
+
toArrayBackward(): E[] {
|
|
463
497
|
const array: E[] = [];
|
|
464
498
|
let current = this.tail;
|
|
465
499
|
while (current) {
|
|
@@ -555,9 +589,6 @@ export class DoublyLinkedList<E = any> {
|
|
|
555
589
|
return accumulator;
|
|
556
590
|
}
|
|
557
591
|
|
|
558
|
-
insertAfter(existingValueOrNode: E, newValue: E): boolean;
|
|
559
|
-
insertAfter(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean;
|
|
560
|
-
|
|
561
592
|
/**
|
|
562
593
|
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
563
594
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
@@ -573,7 +604,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
573
604
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
574
605
|
existingNode = existingValueOrNode;
|
|
575
606
|
} else {
|
|
576
|
-
existingNode = this.
|
|
607
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
577
608
|
}
|
|
578
609
|
|
|
579
610
|
if (existingNode) {
|
|
@@ -595,39 +626,14 @@ export class DoublyLinkedList<E = any> {
|
|
|
595
626
|
}
|
|
596
627
|
|
|
597
628
|
/**
|
|
598
|
-
* The
|
|
599
|
-
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
600
|
-
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
601
|
-
* itself.
|
|
602
|
-
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
603
|
-
* list.
|
|
604
|
-
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
605
|
-
* insertion fails.
|
|
629
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
606
630
|
*/
|
|
607
|
-
|
|
608
|
-
let
|
|
609
|
-
|
|
610
|
-
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
611
|
-
existingNode = existingValueOrNode;
|
|
612
|
-
} else {
|
|
613
|
-
existingNode = this.findNode(existingValueOrNode);
|
|
614
|
-
}
|
|
631
|
+
*[Symbol.iterator]() {
|
|
632
|
+
let current = this.head;
|
|
615
633
|
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
if (existingNode.prev) {
|
|
620
|
-
existingNode.prev.next = newNode;
|
|
621
|
-
}
|
|
622
|
-
newNode.next = existingNode;
|
|
623
|
-
existingNode.prev = newNode;
|
|
624
|
-
if (existingNode === this.head) {
|
|
625
|
-
this.head = newNode;
|
|
626
|
-
}
|
|
627
|
-
this._length++;
|
|
628
|
-
return true;
|
|
634
|
+
while (current) {
|
|
635
|
+
yield current.val;
|
|
636
|
+
current = current.next;
|
|
629
637
|
}
|
|
630
|
-
|
|
631
|
-
return false;
|
|
632
638
|
}
|
|
633
639
|
}
|