min-heap-typed 1.39.2 → 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 +49 -108
- package/dist/data-structures/binary-tree/binary-tree.js +27 -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/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 +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +13 -12
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +130 -60
- package/src/data-structures/binary-tree/bst.ts +31 -32
- package/src/data-structures/binary-tree/rb-tree.ts +7 -6
- package/src/data-structures/binary-tree/tree-multiset.ts +16 -15
- package/src/data-structures/graph/abstract-graph.ts +11 -10
- package/src/data-structures/graph/directed-graph.ts +2 -1
- package/src/data-structures/graph/undirected-graph.ts +5 -4
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +1 -2
- package/src/data-structures/hash/tree-set.ts +1 -2
- package/src/data-structures/heap/heap.ts +2 -2
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +1 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +4 -5
- package/src/data-structures/queue/queue.ts +1 -1
- 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/data-structures/matrix/navigator.ts +1 -1
- package/src/types/helpers.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -5,12 +5,12 @@
|
|
|
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
|
export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
13
|
-
constructor(key:
|
|
13
|
+
constructor(key: BTNKey, val?: V);
|
|
14
14
|
}
|
|
15
15
|
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
|
16
16
|
/**
|
|
@@ -22,30 +22,30 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
22
22
|
constructor(options?: BSTOptions);
|
|
23
23
|
/**
|
|
24
24
|
* The function creates a new binary search tree node with the given key and value.
|
|
25
|
-
* @param {
|
|
25
|
+
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
26
26
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
27
27
|
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It
|
|
28
28
|
* represents the value associated with the node in a binary search tree.
|
|
29
29
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
30
30
|
*/
|
|
31
|
-
createNode(key:
|
|
31
|
+
createNode(key: BTNKey, val?: V): N;
|
|
32
32
|
/**
|
|
33
33
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
34
34
|
* into the tree.
|
|
35
|
-
* @param {
|
|
36
|
-
* `
|
|
35
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
36
|
+
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
37
37
|
* @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
|
|
38
38
|
* binary search tree.
|
|
39
39
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
40
40
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
41
41
|
*/
|
|
42
|
-
add(keyOrNode:
|
|
42
|
+
add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined;
|
|
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 {[
|
|
46
|
+
* @param {[BTNKey | 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
|
-
* array of `
|
|
48
|
+
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
49
49
|
* `null
|
|
50
50
|
* @param {V[]} data - The values of tree nodes
|
|
51
51
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
@@ -53,13 +53,13 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
53
53
|
* It can have two possible values:
|
|
54
54
|
* @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
|
|
55
55
|
*/
|
|
56
|
-
addMany(keysOrNodes: (
|
|
56
|
+
addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: V[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[];
|
|
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
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
|
-
* value (`
|
|
62
|
+
* value (`BTNKey`) or a custom callback function (`BTNCallback<N>`) that determines
|
|
63
63
|
* whether a node matches the desired property.
|
|
64
64
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
65
65
|
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
@@ -72,7 +72,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
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
|
|
75
|
+
get<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, 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
|
|
@@ -88,12 +88,12 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
88
88
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
89
89
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
90
90
|
*/
|
|
91
|
-
lastKey(beginRoot?: N | null, iterationType?: IterationType):
|
|
91
|
+
lastKey(beginRoot?: N | null, iterationType?: IterationType): BTNKey;
|
|
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
95
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
|
|
96
|
-
* of the binary tree node that you want to search for. It can be either a `
|
|
96
|
+
* of the binary tree node that you want to search for. It can be either a `BTNKey` 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
|
|
99
99
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
@@ -110,7 +110,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
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
|
|
113
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, 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,15 +120,15 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
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 {BTNKey | 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
|
+
* (`BTNKey`), or `null` to
|
|
127
127
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
128
128
|
* done recursively or iteratively. It can have two possible values:
|
|
129
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<
|
|
129
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
130
130
|
*/
|
|
131
|
-
lesserOrGreaterTraverse<C extends
|
|
131
|
+
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | 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.
|
|
@@ -158,10 +158,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
158
158
|
/**
|
|
159
159
|
* The function compares two values using a comparator function and returns whether the first value
|
|
160
160
|
* is greater than, less than, or equal to the second value.
|
|
161
|
-
* @param {
|
|
162
|
-
* @param {
|
|
161
|
+
* @param {BTNKey} a - The parameter "a" is of type BTNKey.
|
|
162
|
+
* @param {BTNKey} b - The parameter "b" in the above code represents a BTNKey.
|
|
163
163
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
164
164
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
165
165
|
*/
|
|
166
|
-
protected _compare(a:
|
|
166
|
+
protected _compare(a: BTNKey, b: BTNKey): CP;
|
|
167
167
|
}
|
|
@@ -29,7 +29,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* The function creates a new binary search tree node with the given key and value.
|
|
32
|
-
* @param {
|
|
32
|
+
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
33
33
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
34
34
|
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It
|
|
35
35
|
* represents the value associated with the node in a binary search tree.
|
|
@@ -41,8 +41,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
41
41
|
/**
|
|
42
42
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
43
43
|
* into the tree.
|
|
44
|
-
* @param {
|
|
45
|
-
* `
|
|
44
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
45
|
+
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
46
46
|
* @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
|
|
47
47
|
* binary search tree.
|
|
48
48
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
@@ -126,9 +126,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
126
126
|
/**
|
|
127
127
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
128
128
|
* maintaining balance.
|
|
129
|
-
* @param {[
|
|
129
|
+
* @param {[BTNKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
130
130
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
131
|
-
* array of `
|
|
131
|
+
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
132
132
|
* `null
|
|
133
133
|
* @param {V[]} data - The values of tree nodes
|
|
134
134
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
@@ -210,7 +210,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
210
210
|
* callback.
|
|
211
211
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
212
212
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
213
|
-
* value (`
|
|
213
|
+
* value (`BTNKey`) or a custom callback function (`BTNCallback<N>`) that determines
|
|
214
214
|
* whether a node matches the desired property.
|
|
215
215
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
216
216
|
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
@@ -255,7 +255,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
255
255
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
256
256
|
* using either recursive or iterative traversal.
|
|
257
257
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
|
|
258
|
-
* of the binary tree node that you want to search for. It can be either a `
|
|
258
|
+
* of the binary tree node that you want to search for. It can be either a `BTNKey` or a
|
|
259
259
|
* generic type `N`.
|
|
260
260
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
261
261
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
@@ -337,13 +337,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
337
337
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
338
338
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
339
339
|
* of the following values:
|
|
340
|
-
* @param {
|
|
340
|
+
* @param {BTNKey | N | null} targetNode - The `targetNode` parameter in the
|
|
341
341
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
342
342
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
343
|
-
* (`
|
|
343
|
+
* (`BTNKey`), or `null` to
|
|
344
344
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
345
345
|
* done recursively or iteratively. It can have two possible values:
|
|
346
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<
|
|
346
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
347
347
|
*/
|
|
348
348
|
lesserOrGreaterTraverse(callback = this._defaultCallbackByKey, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
349
349
|
if (typeof targetNode === 'number')
|
|
@@ -494,8 +494,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
494
494
|
/**
|
|
495
495
|
* The function compares two values using a comparator function and returns whether the first value
|
|
496
496
|
* is greater than, less than, or equal to the second value.
|
|
497
|
-
* @param {
|
|
498
|
-
* @param {
|
|
497
|
+
* @param {BTNKey} a - The parameter "a" is of type BTNKey.
|
|
498
|
+
* @param {BTNKey} b - The parameter "b" in the above code represents a BTNKey.
|
|
499
499
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
500
500
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
501
501
|
*/
|
|
@@ -1,13 +1,13 @@
|
|
|
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
|
export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
5
|
-
constructor(key:
|
|
5
|
+
constructor(key: BTNKey, val?: V);
|
|
6
6
|
private _color;
|
|
7
7
|
get color(): RBColor;
|
|
8
8
|
set color(value: RBColor);
|
|
9
9
|
}
|
|
10
10
|
export declare class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
11
11
|
constructor(options?: RBTreeOptions);
|
|
12
|
-
createNode(key:
|
|
12
|
+
createNode(key: BTNKey, val?: V): N;
|
|
13
13
|
}
|
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
import { BinaryTreeDeletedResult, IterationType,
|
|
8
|
+
import type { BTNKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, IterationType, BTNCallback } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
13
13
|
count: number;
|
|
14
14
|
/**
|
|
15
15
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
16
|
-
* @param {
|
|
16
|
+
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
17
17
|
* of the binary tree node.
|
|
18
18
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
19
19
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -21,7 +21,7 @@ export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N>
|
|
|
21
21
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
22
22
|
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
23
23
|
*/
|
|
24
|
-
constructor(key:
|
|
24
|
+
constructor(key: BTNKey, val?: V, count?: number);
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
@@ -38,19 +38,19 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
38
38
|
get count(): number;
|
|
39
39
|
/**
|
|
40
40
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
41
|
-
* @param {
|
|
41
|
+
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
42
42
|
* distinguish one node from another in the tree.
|
|
43
43
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
44
44
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
45
45
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
46
46
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
47
47
|
*/
|
|
48
|
-
createNode(key:
|
|
48
|
+
createNode(key: BTNKey, val?: V, count?: number): N;
|
|
49
49
|
/**
|
|
50
50
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
51
51
|
* exists, and balancing the tree if necessary.
|
|
52
|
-
* @param {
|
|
53
|
-
* `
|
|
52
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
53
|
+
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
54
54
|
* node to be added), or `null` (which represents a null node).
|
|
55
55
|
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
|
56
56
|
* added to the binary tree.
|
|
@@ -59,7 +59,7 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
59
59
|
* count is specified, the default count will be 1.
|
|
60
60
|
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
61
61
|
*/
|
|
62
|
-
add(keyOrNode:
|
|
62
|
+
add(keyOrNode: BTNKey | N | null, val?: V, count?: number): N | null | undefined;
|
|
63
63
|
/**
|
|
64
64
|
* The function adds a new node to a binary tree if there is an available slot in the parent node.
|
|
65
65
|
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
|
|
@@ -72,14 +72,14 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
72
72
|
/**
|
|
73
73
|
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
74
74
|
* inserted nodes.
|
|
75
|
-
* @param {(
|
|
76
|
-
* added to the multiset. Each element can be either a
|
|
75
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
76
|
+
* added to the multiset. Each element can be either a BTNKey or a TreeMultisetNode.
|
|
77
77
|
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
|
|
78
78
|
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
79
79
|
* each key or node.
|
|
80
80
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
81
81
|
*/
|
|
82
|
-
addMany(keysOrNodes: (
|
|
82
|
+
addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[];
|
|
83
83
|
/**
|
|
84
84
|
* The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
|
|
85
85
|
* binary search tree using either a recursive or iterative approach.
|
|
@@ -93,7 +93,7 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
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
95
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
96
|
-
* `
|
|
96
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
97
97
|
* searching for. It can be a specific key value or any other property of the node.
|
|
98
98
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
99
99
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
@@ -105,7 +105,7 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
105
105
|
* decremented by 1 and
|
|
106
106
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
107
107
|
*/
|
|
108
|
-
delete<C extends
|
|
108
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
109
109
|
/**
|
|
110
110
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
111
111
|
*/
|
|
@@ -6,7 +6,7 @@ const avl_tree_1 = require("./avl-tree");
|
|
|
6
6
|
class TreeMultisetNode extends avl_tree_1.AVLTreeNode {
|
|
7
7
|
/**
|
|
8
8
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
9
|
-
* @param {
|
|
9
|
+
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
10
10
|
* of the binary tree node.
|
|
11
11
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
12
12
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -39,7 +39,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
41
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
42
|
-
* @param {
|
|
42
|
+
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
43
43
|
* distinguish one node from another in the tree.
|
|
44
44
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
45
45
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
@@ -52,8 +52,8 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
52
52
|
/**
|
|
53
53
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
54
54
|
* exists, and balancing the tree if necessary.
|
|
55
|
-
* @param {
|
|
56
|
-
* `
|
|
55
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
56
|
+
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
57
57
|
* node to be added), or `null` (which represents a null node).
|
|
58
58
|
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
|
59
59
|
* added to the binary tree.
|
|
@@ -175,8 +175,8 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
175
175
|
/**
|
|
176
176
|
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
177
177
|
* inserted nodes.
|
|
178
|
-
* @param {(
|
|
179
|
-
* added to the multiset. Each element can be either a
|
|
178
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
179
|
+
* added to the multiset. Each element can be either a BTNKey or a TreeMultisetNode.
|
|
180
180
|
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
|
|
181
181
|
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
182
182
|
* each key or node.
|
|
@@ -246,7 +246,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
246
246
|
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
247
247
|
* node along with the parent node that needs to be balanced.
|
|
248
248
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
249
|
-
* `
|
|
249
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
250
250
|
* searching for. It can be a specific key value or any other property of the node.
|
|
251
251
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
252
252
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeletedResult,
|
|
2
|
+
import { BinaryTreeDeletedResult, BTNKey, BinaryTreeNodeNested, BTNCallback } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
4
|
-
createNode(key:
|
|
5
|
-
add(keyOrNode:
|
|
6
|
-
delete<C extends
|
|
4
|
+
createNode(key: BTNKey, val?: N['val']): N;
|
|
5
|
+
add(keyOrNode: BTNKey | N | null, val?: N['val']): N | null | undefined;
|
|
6
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
7
7
|
}
|
|
@@ -18,7 +18,7 @@ export declare enum FamilyPosition {
|
|
|
18
18
|
ISOLATED = "ISOLATED",
|
|
19
19
|
MAL_NODE = "MAL_NODE"
|
|
20
20
|
}
|
|
21
|
-
export type
|
|
21
|
+
export type BTNKey = number;
|
|
22
22
|
export type BinaryTreeDeletedResult<N> = {
|
|
23
23
|
deleted: N | null | undefined;
|
|
24
24
|
needBalanced: N | null;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BSTNode } from '../../../data-structures';
|
|
2
|
-
import type {
|
|
3
|
-
export type BSTComparator = (a:
|
|
2
|
+
import type { BTNKey, BinaryTreeOptions } from './binary-tree';
|
|
3
|
+
export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
|
|
4
4
|
export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
5
|
export type BSTOptions = BinaryTreeOptions & {
|
|
6
6
|
comparator?: BSTComparator;
|
package/dist/types/helpers.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
2
2
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
3
|
-
export type
|
|
3
|
+
export type BTNCallback<N, D = any> = (node: N) => D;
|
|
4
4
|
export declare enum CP {
|
|
5
5
|
lt = "lt",
|
|
6
6
|
eq = "eq",
|
package/package.json
CHANGED
|
@@ -6,14 +6,14 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import {BST, BSTNode} from './bst';
|
|
9
|
-
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult,
|
|
10
|
-
import {
|
|
9
|
+
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BTNKey} from '../../types';
|
|
10
|
+
import {BTNCallback} 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> {
|
|
14
14
|
height: number;
|
|
15
15
|
|
|
16
|
-
constructor(key:
|
|
16
|
+
constructor(key: BTNKey, val?: V) {
|
|
17
17
|
super(key, val);
|
|
18
18
|
this.height = 0;
|
|
19
19
|
}
|
|
@@ -21,7 +21,8 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
|
|
|
21
21
|
|
|
22
22
|
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
|
|
23
23
|
extends BST<V, N>
|
|
24
|
-
implements IBinaryTree<V, N>
|
|
24
|
+
implements IBinaryTree<V, N>
|
|
25
|
+
{
|
|
25
26
|
/**
|
|
26
27
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
27
28
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -34,27 +35,27 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
34
35
|
|
|
35
36
|
/**
|
|
36
37
|
* The function creates a new AVL tree node with the specified key and value.
|
|
37
|
-
* @param {
|
|
38
|
+
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
38
39
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
39
40
|
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
|
|
40
41
|
* type `V`, which means it can be any value that is assignable to the `val` property of the
|
|
41
42
|
* node type `N`.
|
|
42
43
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
43
44
|
*/
|
|
44
|
-
override createNode(key:
|
|
45
|
+
override createNode(key: BTNKey, val?: V): N {
|
|
45
46
|
return new AVLTreeNode<V, N>(key, val) as N;
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
/**
|
|
49
50
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
50
51
|
* a new node.
|
|
51
|
-
* @param {
|
|
52
|
-
* `
|
|
52
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
|
|
53
|
+
* `BTNKey` or a `N` (which represents a node in the binary tree) or `null`.
|
|
53
54
|
* @param [val] - The `val` parameter is the value that you want to assign to the new node that you
|
|
54
55
|
* are adding to the binary search tree.
|
|
55
56
|
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
56
57
|
*/
|
|
57
|
-
override add(keyOrNode:
|
|
58
|
+
override add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
|
|
58
59
|
// TODO support node as a param
|
|
59
60
|
const inserted = super.add(keyOrNode, val);
|
|
60
61
|
if (inserted) this._balancePath(inserted);
|
|
@@ -65,7 +66,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
65
66
|
* The function overrides the delete method of a binary tree and balances the tree after deleting a
|
|
66
67
|
* node if necessary.
|
|
67
68
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
68
|
-
* `
|
|
69
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
69
70
|
* searching for. It can be a specific key value or any other property of the node.
|
|
70
71
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
71
72
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
@@ -73,7 +74,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
73
74
|
* `this._defaultCallbackByKey`
|
|
74
75
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
75
76
|
*/
|
|
76
|
-
override delete<C extends
|
|
77
|
+
override delete<C extends BTNCallback<N>>(
|
|
77
78
|
identifier: ReturnType<C>,
|
|
78
79
|
callback: C = this._defaultCallbackByKey as C
|
|
79
80
|
): BinaryTreeDeletedResult<N>[] {
|
|
@@ -160,7 +161,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
160
161
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
161
162
|
switch (
|
|
162
163
|
this._balanceFactor(A) // second O(1)
|
|
163
|
-
|
|
164
|
+
) {
|
|
164
165
|
case -2:
|
|
165
166
|
if (A && A.left) {
|
|
166
167
|
if (this._balanceFactor(A.left) <= 0) {
|
|
@@ -17,7 +17,7 @@ export class BinaryIndexedTree {
|
|
|
17
17
|
* @param - - `frequency`: The default frequency value. It is optional and has a default
|
|
18
18
|
* value of 0.
|
|
19
19
|
*/
|
|
20
|
-
constructor({frequency = 0, max}: {
|
|
20
|
+
constructor({frequency = 0, max}: {frequency?: number; max: number}) {
|
|
21
21
|
this._freq = frequency;
|
|
22
22
|
this._max = max;
|
|
23
23
|
this._freqMap = {0: 0};
|