data-structure-typed 1.38.4 → 1.38.6
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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +9 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +9 -4
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +31 -79
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +50 -59
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +7 -7
- package/dist/cjs/data-structures/binary-tree/bst.js +13 -13
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +9 -5
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +9 -5
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +12 -3
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +17 -4
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +2 -0
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/types/helpers.d.ts +2 -0
- package/dist/cjs/types/helpers.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +9 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +9 -4
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +31 -79
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +50 -59
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +7 -7
- package/dist/mjs/data-structures/binary-tree/bst.js +13 -13
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +9 -5
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +9 -5
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +12 -3
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +17 -4
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +2 -0
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/types/helpers.d.ts +2 -0
- package/dist/umd/index.global.js +1 -1
- package/dist/umd/index.global.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +13 -4
- package/src/data-structures/binary-tree/binary-tree.ts +113 -69
- package/src/data-structures/binary-tree/bst.ts +17 -17
- package/src/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multiset.ts +14 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +2 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -7
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/helpers.ts +4 -0
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +14 -14
|
@@ -43,7 +43,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
43
43
|
/**
|
|
44
44
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
45
45
|
* maintaining balance.
|
|
46
|
-
* @param {[BinaryTreeNodeKey | N, N['val']][]}
|
|
46
|
+
* @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
47
47
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
48
48
|
* array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
|
|
49
49
|
* `null
|
|
@@ -57,7 +57,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
57
57
|
/**
|
|
58
58
|
* The function returns the first node in the binary tree that matches the given node property and
|
|
59
59
|
* callback.
|
|
60
|
-
* @param {
|
|
60
|
+
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
61
61
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
62
62
|
* value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
|
|
63
63
|
* whether a node matches the desired property.
|
|
@@ -72,7 +72,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
72
72
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
73
73
|
* matching node is found.
|
|
74
74
|
*/
|
|
75
|
-
get<C extends MapCallback<N
|
|
75
|
+
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
76
76
|
/**
|
|
77
77
|
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
78
78
|
* than, the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
@@ -92,7 +92,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
92
92
|
/**
|
|
93
93
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
94
94
|
* using either recursive or iterative traversal.
|
|
95
|
-
* @param {
|
|
95
|
+
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
|
|
96
96
|
* of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
|
|
97
97
|
* generic type `N`.
|
|
98
98
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
@@ -110,7 +110,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
110
110
|
* traverse the binary tree. It can have one of the following values:
|
|
111
111
|
* @returns an array of nodes (N[]).
|
|
112
112
|
*/
|
|
113
|
-
getNodes<C extends MapCallback<N
|
|
113
|
+
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
114
114
|
/**
|
|
115
115
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
116
116
|
* nodes that have a key value lesser or greater than a target key value.
|
|
@@ -120,7 +120,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
120
120
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
121
121
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
122
122
|
* of the following values:
|
|
123
|
-
* @param {
|
|
123
|
+
* @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the
|
|
124
124
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
125
125
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
126
126
|
* (`BinaryTreeNodeKey`), or `null` to
|
|
@@ -128,7 +128,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
128
128
|
* done recursively or iteratively. It can have two possible values:
|
|
129
129
|
* @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
|
|
130
130
|
*/
|
|
131
|
-
lesserOrGreaterTraverse<C extends MapCallback<N
|
|
131
|
+
lesserOrGreaterTraverse<C extends MapCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): ReturnType<C>[];
|
|
132
132
|
/**
|
|
133
133
|
* Balancing Adjustment:
|
|
134
134
|
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
@@ -125,7 +125,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
125
125
|
/**
|
|
126
126
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
127
127
|
* maintaining balance.
|
|
128
|
-
* @param {[BinaryTreeNodeKey | N, N['val']][]}
|
|
128
|
+
* @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
129
129
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
130
130
|
* array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
|
|
131
131
|
* `null
|
|
@@ -207,7 +207,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
207
207
|
/**
|
|
208
208
|
* The function returns the first node in the binary tree that matches the given node property and
|
|
209
209
|
* callback.
|
|
210
|
-
* @param {
|
|
210
|
+
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
211
211
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
212
212
|
* value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
|
|
213
213
|
* whether a node matches the desired property.
|
|
@@ -222,8 +222,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
222
222
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
223
223
|
* matching node is found.
|
|
224
224
|
*/
|
|
225
|
-
get(
|
|
226
|
-
return this.getNodes(
|
|
225
|
+
get(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
226
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
227
227
|
}
|
|
228
228
|
/**
|
|
229
229
|
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
@@ -251,7 +251,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
251
251
|
/**
|
|
252
252
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
253
253
|
* using either recursive or iterative traversal.
|
|
254
|
-
* @param {
|
|
254
|
+
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
|
|
255
255
|
* of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
|
|
256
256
|
* generic type `N`.
|
|
257
257
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
@@ -269,14 +269,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
269
269
|
* traverse the binary tree. It can have one of the following values:
|
|
270
270
|
* @returns an array of nodes (N[]).
|
|
271
271
|
*/
|
|
272
|
-
getNodes(
|
|
272
|
+
getNodes(identifier, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
273
273
|
if (!beginRoot)
|
|
274
274
|
return [];
|
|
275
275
|
const ans = [];
|
|
276
276
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
277
277
|
const _traverse = (cur) => {
|
|
278
278
|
const callbackResult = callback(cur);
|
|
279
|
-
if (callbackResult ===
|
|
279
|
+
if (callbackResult === identifier) {
|
|
280
280
|
ans.push(cur);
|
|
281
281
|
if (onlyOne)
|
|
282
282
|
return;
|
|
@@ -285,9 +285,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
285
285
|
return;
|
|
286
286
|
// TODO potential bug
|
|
287
287
|
if (callback === this._defaultCallbackByKey) {
|
|
288
|
-
if (this._compare(cur.key,
|
|
288
|
+
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
289
289
|
cur.left && _traverse(cur.left);
|
|
290
|
-
if (this._compare(cur.key,
|
|
290
|
+
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
291
291
|
cur.right && _traverse(cur.right);
|
|
292
292
|
}
|
|
293
293
|
else {
|
|
@@ -303,16 +303,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
303
303
|
const cur = queue.shift();
|
|
304
304
|
if (cur) {
|
|
305
305
|
const callbackResult = callback(cur);
|
|
306
|
-
if (callbackResult ===
|
|
306
|
+
if (callbackResult === identifier) {
|
|
307
307
|
ans.push(cur);
|
|
308
308
|
if (onlyOne)
|
|
309
309
|
return ans;
|
|
310
310
|
}
|
|
311
311
|
// TODO potential bug
|
|
312
312
|
if (callback === this._defaultCallbackByKey) {
|
|
313
|
-
if (this._compare(cur.key,
|
|
313
|
+
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
314
314
|
cur.left && queue.push(cur.left);
|
|
315
|
-
if (this._compare(cur.key,
|
|
315
|
+
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
316
316
|
cur.right && queue.push(cur.right);
|
|
317
317
|
}
|
|
318
318
|
else {
|
|
@@ -334,7 +334,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
334
334
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
335
335
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
336
336
|
* of the following values:
|
|
337
|
-
* @param {
|
|
337
|
+
* @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the
|
|
338
338
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
339
339
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
340
340
|
* (`BinaryTreeNodeKey`), or `null` to
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
-
import { BinaryTreeDeletedResult, IterationType } from '../../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, IterationType, MapCallback } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> {
|
|
@@ -92,16 +92,20 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
92
92
|
/**
|
|
93
93
|
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
94
94
|
* node along with the parent node that needs to be balanced.
|
|
95
|
-
* @param {
|
|
96
|
-
*
|
|
97
|
-
*
|
|
95
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
96
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
97
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
98
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
99
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
100
|
+
* included in the result. The `callback` parameter has a default value of
|
|
101
|
+
* `this._defaultCallbackByKey`
|
|
98
102
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
99
103
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
100
104
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
101
105
|
* decremented by 1 and
|
|
102
106
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
103
107
|
*/
|
|
104
|
-
delete(
|
|
108
|
+
delete<C extends MapCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
105
109
|
/**
|
|
106
110
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
107
111
|
*/
|
|
@@ -246,20 +246,24 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
246
246
|
/**
|
|
247
247
|
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
248
248
|
* node along with the parent node that needs to be balanced.
|
|
249
|
-
* @param {
|
|
250
|
-
*
|
|
251
|
-
*
|
|
249
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
250
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
251
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
252
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
253
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
254
|
+
* included in the result. The `callback` parameter has a default value of
|
|
255
|
+
* `this._defaultCallbackByKey`
|
|
252
256
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
253
257
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
254
258
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
255
259
|
* decremented by 1 and
|
|
256
260
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
257
261
|
*/
|
|
258
|
-
delete(
|
|
262
|
+
delete(identifier, callback = this._defaultCallbackByKey, ignoreCount = false) {
|
|
259
263
|
const bstDeletedResult = [];
|
|
260
264
|
if (!this.root)
|
|
261
265
|
return bstDeletedResult;
|
|
262
|
-
const curr = this.get(
|
|
266
|
+
const curr = this.get(identifier, callback);
|
|
263
267
|
if (!curr)
|
|
264
268
|
return bstDeletedResult;
|
|
265
269
|
const parent = curr?.parent ? curr.parent : null;
|
|
@@ -123,7 +123,7 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
123
123
|
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
|
|
124
124
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
125
125
|
*/
|
|
126
|
-
findNode(val: E): DoublyLinkedListNode<E> | null;
|
|
126
|
+
findNode(val: E | null): DoublyLinkedListNode<E> | null;
|
|
127
127
|
/**
|
|
128
128
|
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
129
129
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
@@ -229,6 +229,15 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
229
229
|
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U;
|
|
230
230
|
insertAfter(existingValueOrNode: E, newValue: E): boolean;
|
|
231
231
|
insertAfter(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean;
|
|
232
|
-
|
|
233
|
-
|
|
232
|
+
/**
|
|
233
|
+
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
234
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
235
|
+
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
236
|
+
* itself.
|
|
237
|
+
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
238
|
+
* list.
|
|
239
|
+
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
240
|
+
* insertion fails.
|
|
241
|
+
*/
|
|
242
|
+
insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
|
|
234
243
|
}
|
|
@@ -88,8 +88,14 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
88
88
|
* bounds.
|
|
89
89
|
*/
|
|
90
90
|
deleteAt(index: number): E | undefined;
|
|
91
|
-
|
|
92
|
-
|
|
91
|
+
/**
|
|
92
|
+
* The delete function removes a node with a specific value from a singly linked list.
|
|
93
|
+
* @param {E | SinglyLinkedListNode<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
|
|
94
|
+
* or a `SinglyLinkedListNode<E>` object.
|
|
95
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
96
|
+
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
97
|
+
*/
|
|
98
|
+
delete(valueOrNode: E | SinglyLinkedListNode<E> | null | undefined): boolean;
|
|
93
99
|
/**
|
|
94
100
|
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
95
101
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
@@ -143,8 +149,15 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
143
149
|
* the specified value is found, the function returns `null`.
|
|
144
150
|
*/
|
|
145
151
|
findNode(value: E): SinglyLinkedListNode<E> | null;
|
|
146
|
-
|
|
147
|
-
|
|
152
|
+
/**
|
|
153
|
+
* The `insertBefore` function inserts a new value before an existing value in a singly linked list.
|
|
154
|
+
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
155
|
+
* new value before. It can be either the value itself or a node containing the value in the linked list.
|
|
156
|
+
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
|
|
157
|
+
* @returns The method `insertBefore` returns a boolean value. It returns `true` if the new value was successfully
|
|
158
|
+
* inserted before the existing value, and `false` otherwise.
|
|
159
|
+
*/
|
|
160
|
+
insertBefore(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
148
161
|
insertAfter(existingValueOrNode: E, newValue: E): boolean;
|
|
149
162
|
insertAfter(existingValueOrNode: SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
150
163
|
/**
|
|
@@ -207,6 +207,8 @@ class SinglyLinkedList {
|
|
|
207
207
|
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
208
208
|
*/
|
|
209
209
|
delete(valueOrNode) {
|
|
210
|
+
if (!valueOrNode)
|
|
211
|
+
return false;
|
|
210
212
|
let value;
|
|
211
213
|
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
212
214
|
value = valueOrNode.val;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../types';
|
|
2
|
+
import { BinaryTreeDeletedResult, BinaryTreeNodeKey, MapCallback } from '../types';
|
|
3
3
|
export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> {
|
|
4
4
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
5
5
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
6
|
-
delete(
|
|
6
|
+
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
|
|
7
7
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { BinaryTreeNodeKey } from './data-structures';
|
|
1
2
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
2
3
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
3
4
|
export type MapCallback<N, D = any> = (node: N) => D;
|
|
5
|
+
export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
|
|
4
6
|
export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
|
|
5
7
|
export declare enum CP {
|
|
6
8
|
lt = "lt",
|