min-heap-typed 1.38.5 → 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/dist/data-structures/binary-tree/avl-tree.d.ts +9 -3
- package/dist/data-structures/binary-tree/avl-tree.js +9 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -79
- package/dist/data-structures/binary-tree/binary-tree.js +50 -59
- package/dist/data-structures/binary-tree/bst.d.ts +7 -7
- package/dist/data-structures/binary-tree/bst.js +13 -13
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +9 -5
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/helpers.d.ts +2 -0
- package/package.json +2 -2
- 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 +0 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/helpers.ts +4 -0
|
@@ -132,7 +132,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
132
132
|
/**
|
|
133
133
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
134
134
|
* maintaining balance.
|
|
135
|
-
* @param {[BinaryTreeNodeKey | N, N['val']][]}
|
|
135
|
+
* @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
136
136
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
137
137
|
* array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
|
|
138
138
|
* `null
|
|
@@ -223,7 +223,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
223
223
|
/**
|
|
224
224
|
* The function returns the first node in the binary tree that matches the given node property and
|
|
225
225
|
* callback.
|
|
226
|
-
* @param {
|
|
226
|
+
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
227
227
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
228
228
|
* value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
|
|
229
229
|
* whether a node matches the desired property.
|
|
@@ -238,13 +238,13 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
238
238
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
239
239
|
* matching node is found.
|
|
240
240
|
*/
|
|
241
|
-
override get<C extends MapCallback<N
|
|
242
|
-
|
|
241
|
+
override get<C extends MapCallback<N>>(
|
|
242
|
+
identifier: ReturnType<C> | N,
|
|
243
243
|
callback: C = this._defaultCallbackByKey as C,
|
|
244
244
|
beginRoot = this.root,
|
|
245
245
|
iterationType = this.iterationType
|
|
246
246
|
): N | null {
|
|
247
|
-
return this.getNodes(
|
|
247
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
248
248
|
}
|
|
249
249
|
|
|
250
250
|
/**
|
|
@@ -271,7 +271,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
271
271
|
/**
|
|
272
272
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
273
273
|
* using either recursive or iterative traversal.
|
|
274
|
-
* @param {
|
|
274
|
+
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
|
|
275
275
|
* of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
|
|
276
276
|
* generic type `N`.
|
|
277
277
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
@@ -289,8 +289,8 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
289
289
|
* traverse the binary tree. It can have one of the following values:
|
|
290
290
|
* @returns an array of nodes (N[]).
|
|
291
291
|
*/
|
|
292
|
-
override getNodes<C extends MapCallback<N
|
|
293
|
-
|
|
292
|
+
override getNodes<C extends MapCallback<N>>(
|
|
293
|
+
identifier: ReturnType<C> | N,
|
|
294
294
|
callback: C = this._defaultCallbackByKey as C,
|
|
295
295
|
onlyOne = false,
|
|
296
296
|
beginRoot: N | null = this.root,
|
|
@@ -302,7 +302,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
302
302
|
if (iterationType === IterationType.RECURSIVE) {
|
|
303
303
|
const _traverse = (cur: N) => {
|
|
304
304
|
const callbackResult = callback(cur);
|
|
305
|
-
if (callbackResult ===
|
|
305
|
+
if (callbackResult === identifier) {
|
|
306
306
|
ans.push(cur);
|
|
307
307
|
if (onlyOne) return;
|
|
308
308
|
}
|
|
@@ -310,8 +310,8 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
310
310
|
if (!cur.left && !cur.right) return;
|
|
311
311
|
// TODO potential bug
|
|
312
312
|
if (callback === this._defaultCallbackByKey) {
|
|
313
|
-
if (this._compare(cur.key,
|
|
314
|
-
if (this._compare(cur.key,
|
|
313
|
+
if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && _traverse(cur.left);
|
|
314
|
+
if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && _traverse(cur.right);
|
|
315
315
|
} else {
|
|
316
316
|
cur.left && _traverse(cur.left);
|
|
317
317
|
cur.right && _traverse(cur.right);
|
|
@@ -325,14 +325,14 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
325
325
|
const cur = queue.shift();
|
|
326
326
|
if (cur) {
|
|
327
327
|
const callbackResult = callback(cur);
|
|
328
|
-
if (callbackResult ===
|
|
328
|
+
if (callbackResult === identifier) {
|
|
329
329
|
ans.push(cur);
|
|
330
330
|
if (onlyOne) return ans;
|
|
331
331
|
}
|
|
332
332
|
// TODO potential bug
|
|
333
333
|
if (callback === this._defaultCallbackByKey) {
|
|
334
|
-
if (this._compare(cur.key,
|
|
335
|
-
if (this._compare(cur.key,
|
|
334
|
+
if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && queue.push(cur.left);
|
|
335
|
+
if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && queue.push(cur.right);
|
|
336
336
|
} else {
|
|
337
337
|
cur.left && queue.push(cur.left);
|
|
338
338
|
cur.right && queue.push(cur.right);
|
|
@@ -355,7 +355,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
355
355
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
356
356
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
357
357
|
* of the following values:
|
|
358
|
-
* @param {
|
|
358
|
+
* @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the
|
|
359
359
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
360
360
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
361
361
|
* (`BinaryTreeNodeKey`), or `null` to
|
|
@@ -363,10 +363,10 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
363
363
|
* done recursively or iteratively. It can have two possible values:
|
|
364
364
|
* @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
|
|
365
365
|
*/
|
|
366
|
-
lesserOrGreaterTraverse<C extends MapCallback<N
|
|
366
|
+
lesserOrGreaterTraverse<C extends MapCallback<N>>(
|
|
367
367
|
callback: C = this._defaultCallbackByKey as C,
|
|
368
368
|
lesserOrGreater: CP = CP.lt,
|
|
369
|
-
targetNode:
|
|
369
|
+
targetNode: BinaryTreeNodeKey | N | null = this.root,
|
|
370
370
|
iterationType = this.iterationType
|
|
371
371
|
): ReturnType<C>[] {
|
|
372
372
|
if (typeof targetNode === 'number') targetNode = this.get(targetNode);
|
|
@@ -205,8 +205,8 @@ export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<
|
|
|
205
205
|
// node.right = null;
|
|
206
206
|
// }
|
|
207
207
|
//
|
|
208
|
-
// override delete(
|
|
209
|
-
// const node = this.get(
|
|
208
|
+
// override delete(keyOrNode: BinaryTreeNodeKey | N): BinaryTreeDeletedResult<N>[] {
|
|
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
|
|
212
212
|
//
|
|
@@ -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} from '../../types';
|
|
9
|
+
import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType, MapCallback} from '../../types';
|
|
10
10
|
import {IBinaryTree} from '../../interfaces';
|
|
11
11
|
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
12
12
|
|
|
@@ -265,20 +265,28 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
265
265
|
/**
|
|
266
266
|
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
267
267
|
* node along with the parent node that needs to be balanced.
|
|
268
|
-
* @param {
|
|
269
|
-
*
|
|
270
|
-
*
|
|
268
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
269
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
270
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
271
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
272
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
273
|
+
* included in the result. The `callback` parameter has a default value of
|
|
274
|
+
* `this._defaultCallbackByKey`
|
|
271
275
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
272
276
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
273
277
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
274
278
|
* decremented by 1 and
|
|
275
279
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
276
280
|
*/
|
|
277
|
-
override delete
|
|
281
|
+
override delete<C extends MapCallback<N>>(
|
|
282
|
+
identifier: ReturnType<C>,
|
|
283
|
+
callback: C = this._defaultCallbackByKey as C,
|
|
284
|
+
ignoreCount = false
|
|
285
|
+
): BinaryTreeDeletedResult<N>[] {
|
|
278
286
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
279
287
|
if (!this.root) return bstDeletedResult;
|
|
280
288
|
|
|
281
|
-
const curr: N | null = this.get(
|
|
289
|
+
const curr: N | null = this.get(identifier, callback);
|
|
282
290
|
if (!curr) return bstDeletedResult;
|
|
283
291
|
|
|
284
292
|
const parent: N | null = curr?.parent ? curr.parent : null;
|
|
@@ -594,7 +594,6 @@ export class DoublyLinkedList<E = any> {
|
|
|
594
594
|
return false;
|
|
595
595
|
}
|
|
596
596
|
|
|
597
|
-
|
|
598
597
|
/**
|
|
599
598
|
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
600
599
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {BinaryTreeNode} from '../data-structures';
|
|
2
|
-
import {BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../types';
|
|
2
|
+
import {BinaryTreeDeletedResult, BinaryTreeNodeKey, MapCallback} from '../types';
|
|
3
3
|
|
|
4
4
|
export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> {
|
|
5
5
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
6
6
|
|
|
7
7
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
8
8
|
|
|
9
|
-
delete(
|
|
9
|
+
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
|
|
10
10
|
}
|
package/src/types/helpers.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import {BinaryTreeNodeKey} from './data-structures';
|
|
2
|
+
|
|
1
3
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
2
4
|
|
|
3
5
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
4
6
|
|
|
5
7
|
export type MapCallback<N, D = any> = (node: N) => D;
|
|
6
8
|
|
|
9
|
+
export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
|
|
10
|
+
|
|
7
11
|
export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
|
|
8
12
|
|
|
9
13
|
export enum CP {
|