data-structure-typed 1.47.6 → 1.47.7
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/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +103 -28
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/cjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +6 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +133 -128
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/mjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +59 -138
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +6 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/umd/data-structure-typed.js +422 -466
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/hash/hash-map.ts +1 -2
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +87 -0
- package/test/performance/data-structures/comparison/comparison.test.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -51
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/unrestricted-interconversion.test.ts +61 -5
- package/tsconfig-cjs.json +1 -1
|
@@ -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 { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, BTNKey, Comparator } from '../../types';
|
|
9
|
-
import { CP,
|
|
8
|
+
import type { BSTNested, BSTNodeKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNKey, BTNodeExemplar, Comparator } from '../../types';
|
|
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> {
|
|
@@ -33,17 +33,26 @@ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>
|
|
|
33
33
|
*/
|
|
34
34
|
set right(v: N | undefined);
|
|
35
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
|
|
38
|
+
* 2. Unique Keys: No duplicate keys in a standard BST.
|
|
39
|
+
* 3. Efficient Search: Enables quick search, minimum, and maximum operations.
|
|
40
|
+
* 4. Inorder Traversal: Yields nodes in ascending order.
|
|
41
|
+
* 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
|
|
42
|
+
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
43
|
+
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
44
|
+
*/
|
|
36
45
|
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>, TREE extends BST<V, N, TREE> = BST<V, N, BSTNested<V, N>>> extends BinaryTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
37
46
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
47
|
+
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
48
|
+
* the tree with optional elements and options.
|
|
49
|
+
* @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
|
|
50
|
+
* binary search tree.
|
|
51
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
52
|
+
* configuration options for the binary search tree. It can have the following properties:
|
|
41
53
|
*/
|
|
42
|
-
constructor(elements?:
|
|
54
|
+
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<BSTOptions>);
|
|
43
55
|
protected _root?: N;
|
|
44
|
-
/**
|
|
45
|
-
* Get the root node of the binary tree.
|
|
46
|
-
*/
|
|
47
56
|
get root(): N | undefined;
|
|
48
57
|
comparator: Comparator<BTNKey>;
|
|
49
58
|
/**
|
|
@@ -55,45 +64,50 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
55
64
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
56
65
|
*/
|
|
57
66
|
createNode(key: BTNKey, value?: V): N;
|
|
67
|
+
/**
|
|
68
|
+
* The function creates a new binary search tree with the specified options.
|
|
69
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
70
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
|
|
71
|
+
* that defines various options for creating a binary search tree.
|
|
72
|
+
* @returns a new instance of the BST class with the specified options.
|
|
73
|
+
*/
|
|
58
74
|
createTree(options?: Partial<BSTOptions>): TREE;
|
|
59
75
|
/**
|
|
60
76
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
61
77
|
* Space Complexity: O(1) - Constant space is used.
|
|
62
|
-
*
|
|
63
|
-
* The `add` function adds a new node to a binary search tree based on the provided key and value.
|
|
64
|
-
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
65
|
-
* following types:
|
|
66
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
67
|
-
* key or node being added to the binary search tree.
|
|
68
|
-
* @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
|
|
69
|
-
* no node was inserted, it returns `undefined`.
|
|
70
78
|
*/
|
|
71
|
-
add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
|
|
72
79
|
/**
|
|
73
80
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
74
81
|
* Space Complexity: O(1) - Constant space is used.
|
|
82
|
+
*
|
|
83
|
+
* The `add` function adds a new node to a binary search tree, either by key or by providing a node
|
|
84
|
+
* object.
|
|
85
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
86
|
+
* @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
|
|
87
|
+
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
75
88
|
*/
|
|
89
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
|
|
76
90
|
/**
|
|
77
|
-
* Time Complexity: O(
|
|
78
|
-
* Space Complexity: O(
|
|
91
|
+
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
92
|
+
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
93
|
+
*/
|
|
94
|
+
/**
|
|
95
|
+
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
96
|
+
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
79
97
|
*
|
|
80
|
-
* The `addMany` function
|
|
81
|
-
*
|
|
82
|
-
* @param
|
|
83
|
-
* binary
|
|
84
|
-
* node), or `undefined`.
|
|
85
|
-
* @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
|
|
86
|
-
* nodes being added. If provided, the length of the `data` array must be the same as the length of
|
|
87
|
-
* the `keysOrNodes` array.
|
|
98
|
+
* The `addMany` function in TypeScript adds multiple nodes to a binary tree, either in a balanced or
|
|
99
|
+
* unbalanced manner, and returns an array of the inserted nodes.
|
|
100
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
|
|
101
|
+
* binary tree.
|
|
88
102
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
89
|
-
* adding the nodes. The default value is
|
|
103
|
+
* adding the nodes. The default value is true.
|
|
90
104
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
91
|
-
* type of iteration to use when adding multiple keys or nodes to the binary
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* @returns The
|
|
105
|
+
* type of iteration to use when adding multiple keys or nodes to the binary tree. It has a default
|
|
106
|
+
* value of `this.iterationType`, which means it will use the iteration type specified by the binary
|
|
107
|
+
* tree instance.
|
|
108
|
+
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
95
109
|
*/
|
|
96
|
-
addMany(
|
|
110
|
+
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
97
111
|
/**
|
|
98
112
|
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
99
113
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
@@ -113,7 +127,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
113
127
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
114
128
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
115
129
|
*/
|
|
116
|
-
lastKey(beginRoot?:
|
|
130
|
+
lastKey(beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): BTNKey;
|
|
117
131
|
/**
|
|
118
132
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
119
133
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -138,7 +152,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
138
152
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
139
153
|
*/
|
|
140
154
|
/**
|
|
141
|
-
* The function `
|
|
155
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
142
156
|
* otherwise it returns the key itself.
|
|
143
157
|
* @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
|
|
144
158
|
* `undefined`.
|
|
@@ -146,7 +160,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
146
160
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
147
161
|
* @returns either a node object (N) or undefined.
|
|
148
162
|
*/
|
|
149
|
-
|
|
163
|
+
ensureNode(key: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N | undefined;
|
|
150
164
|
/**
|
|
151
165
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
152
166
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -170,7 +184,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
170
184
|
* performed on the binary tree. It can have two possible values:
|
|
171
185
|
* @returns The method returns an array of nodes (`N[]`).
|
|
172
186
|
*/
|
|
173
|
-
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?:
|
|
187
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N[];
|
|
174
188
|
/**
|
|
175
189
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
176
190
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -196,7 +210,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
196
210
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
197
211
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
198
212
|
*/
|
|
199
|
-
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?:
|
|
213
|
+
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): ReturnType<C>[];
|
|
200
214
|
/**
|
|
201
215
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
202
216
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -236,11 +250,6 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
236
250
|
* @returns a boolean value.
|
|
237
251
|
*/
|
|
238
252
|
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
239
|
-
/**
|
|
240
|
-
* Time Complexity: O(n) - Visiting each node once.
|
|
241
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
242
|
-
*/
|
|
243
|
-
init(elements: IterableEntriesOrKeys<V>): void;
|
|
244
253
|
protected _setRoot(v: N | undefined): void;
|
|
245
254
|
/**
|
|
246
255
|
* The function compares two values using a comparator function and returns whether the first value
|
|
@@ -44,11 +44,23 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
44
44
|
this._right = v;
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
|
|
49
|
+
* 2. Unique Keys: No duplicate keys in a standard BST.
|
|
50
|
+
* 3. Efficient Search: Enables quick search, minimum, and maximum operations.
|
|
51
|
+
* 4. Inorder Traversal: Yields nodes in ascending order.
|
|
52
|
+
* 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
|
|
53
|
+
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
54
|
+
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
55
|
+
*/
|
|
47
56
|
export class BST extends BinaryTree {
|
|
48
57
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
58
|
+
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
59
|
+
* the tree with optional elements and options.
|
|
60
|
+
* @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
|
|
61
|
+
* binary search tree.
|
|
62
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
63
|
+
* configuration options for the binary search tree. It can have the following properties:
|
|
52
64
|
*/
|
|
53
65
|
constructor(elements, options) {
|
|
54
66
|
super([], options);
|
|
@@ -60,12 +72,9 @@ export class BST extends BinaryTree {
|
|
|
60
72
|
}
|
|
61
73
|
this._root = undefined;
|
|
62
74
|
if (elements)
|
|
63
|
-
this.
|
|
75
|
+
this.addMany(elements);
|
|
64
76
|
}
|
|
65
77
|
_root;
|
|
66
|
-
/**
|
|
67
|
-
* Get the root node of the binary tree.
|
|
68
|
-
*/
|
|
69
78
|
get root() {
|
|
70
79
|
return this._root;
|
|
71
80
|
}
|
|
@@ -81,168 +90,159 @@ export class BST extends BinaryTree {
|
|
|
81
90
|
createNode(key, value) {
|
|
82
91
|
return new BSTNode(key, value);
|
|
83
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* The function creates a new binary search tree with the specified options.
|
|
95
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
96
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
|
|
97
|
+
* that defines various options for creating a binary search tree.
|
|
98
|
+
* @returns a new instance of the BST class with the specified options.
|
|
99
|
+
*/
|
|
84
100
|
createTree(options) {
|
|
85
101
|
return new BST([], {
|
|
86
102
|
iterationType: this.iterationType,
|
|
87
103
|
comparator: this.comparator, ...options
|
|
88
104
|
});
|
|
89
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
108
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
109
|
+
*/
|
|
90
110
|
/**
|
|
91
111
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
92
112
|
* Space Complexity: O(1) - Constant space is used.
|
|
93
113
|
*
|
|
94
|
-
* The `add` function adds a new node to a binary search tree
|
|
95
|
-
*
|
|
96
|
-
* following
|
|
97
|
-
* @
|
|
98
|
-
*
|
|
99
|
-
* @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
|
|
100
|
-
* no node was inserted, it returns `undefined`.
|
|
114
|
+
* The `add` function adds a new node to a binary search tree, either by key or by providing a node
|
|
115
|
+
* object.
|
|
116
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
117
|
+
* @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
|
|
118
|
+
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
101
119
|
*/
|
|
102
|
-
add(
|
|
103
|
-
if (
|
|
120
|
+
add(keyOrNodeOrEntry) {
|
|
121
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
104
122
|
return undefined;
|
|
105
|
-
|
|
106
|
-
let inserted;
|
|
123
|
+
}
|
|
107
124
|
let newNode;
|
|
108
|
-
if (
|
|
109
|
-
newNode =
|
|
125
|
+
if (keyOrNodeOrEntry instanceof BSTNode) {
|
|
126
|
+
newNode = keyOrNodeOrEntry;
|
|
110
127
|
}
|
|
111
|
-
else if (this.isNodeKey(
|
|
112
|
-
newNode = this.createNode(
|
|
128
|
+
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
129
|
+
newNode = this.createNode(keyOrNodeOrEntry);
|
|
130
|
+
}
|
|
131
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
132
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
133
|
+
if (key === undefined || key === null) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
newNode = this.createNode(key, value);
|
|
138
|
+
}
|
|
113
139
|
}
|
|
114
140
|
else {
|
|
115
|
-
|
|
141
|
+
return;
|
|
116
142
|
}
|
|
117
143
|
if (this.root === undefined) {
|
|
118
144
|
this._setRoot(newNode);
|
|
119
|
-
this._size
|
|
120
|
-
|
|
145
|
+
this._size++;
|
|
146
|
+
return this.root;
|
|
121
147
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
//Add to the left of the current node
|
|
142
|
-
cur.left = newNode;
|
|
143
|
-
this._size = this.size + 1;
|
|
144
|
-
traversing = false;
|
|
145
|
-
inserted = cur.left;
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
//Traverse the left of the current node
|
|
149
|
-
if (cur.left)
|
|
150
|
-
cur = cur.left;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
154
|
-
// Traverse right of the node
|
|
155
|
-
if (cur.right === undefined) {
|
|
156
|
-
if (newNode) {
|
|
157
|
-
newNode.parent = cur;
|
|
158
|
-
}
|
|
159
|
-
//Add to the right of the current node
|
|
160
|
-
cur.right = newNode;
|
|
161
|
-
this._size = this.size + 1;
|
|
162
|
-
traversing = false;
|
|
163
|
-
inserted = cur.right;
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
//Traverse the left of the current node
|
|
167
|
-
if (cur.right)
|
|
168
|
-
cur = cur.right;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
148
|
+
let current = this.root;
|
|
149
|
+
while (current !== undefined) {
|
|
150
|
+
if (this._compare(current.key, newNode.key) === CP.eq) {
|
|
151
|
+
// if (current !== newNode) {
|
|
152
|
+
// The key value is the same but the reference is different, update the value of the existing node
|
|
153
|
+
this._replaceNode(current, newNode);
|
|
154
|
+
return newNode;
|
|
155
|
+
// } else {
|
|
156
|
+
// The key value is the same and the reference is the same, replace the entire node
|
|
157
|
+
// this._replaceNode(current, newNode);
|
|
158
|
+
// return;
|
|
159
|
+
// }
|
|
160
|
+
}
|
|
161
|
+
else if (this._compare(current.key, newNode.key) === CP.gt) {
|
|
162
|
+
if (current.left === undefined) {
|
|
163
|
+
current.left = newNode;
|
|
164
|
+
newNode.parent = current;
|
|
165
|
+
this._size++;
|
|
166
|
+
return newNode;
|
|
171
167
|
}
|
|
172
|
-
|
|
173
|
-
|
|
168
|
+
current = current.left;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
if (current.right === undefined) {
|
|
172
|
+
current.right = newNode;
|
|
173
|
+
newNode.parent = current;
|
|
174
|
+
this._size++;
|
|
175
|
+
return newNode;
|
|
174
176
|
}
|
|
177
|
+
current = current.right;
|
|
175
178
|
}
|
|
176
179
|
}
|
|
177
|
-
return
|
|
180
|
+
return undefined;
|
|
178
181
|
}
|
|
179
182
|
/**
|
|
180
|
-
* Time Complexity: O(log n) -
|
|
181
|
-
* Space Complexity: O(
|
|
183
|
+
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
184
|
+
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
182
185
|
*/
|
|
183
186
|
/**
|
|
184
|
-
* Time Complexity: O(
|
|
185
|
-
* Space Complexity: O(
|
|
187
|
+
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
188
|
+
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
186
189
|
*
|
|
187
|
-
* The `addMany` function
|
|
188
|
-
*
|
|
189
|
-
* @param
|
|
190
|
-
* binary
|
|
191
|
-
* node), or `undefined`.
|
|
192
|
-
* @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
|
|
193
|
-
* nodes being added. If provided, the length of the `data` array must be the same as the length of
|
|
194
|
-
* the `keysOrNodes` array.
|
|
190
|
+
* The `addMany` function in TypeScript adds multiple nodes to a binary tree, either in a balanced or
|
|
191
|
+
* unbalanced manner, and returns an array of the inserted nodes.
|
|
192
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
|
|
193
|
+
* binary tree.
|
|
195
194
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
196
|
-
* adding the nodes. The default value is
|
|
195
|
+
* adding the nodes. The default value is true.
|
|
197
196
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
198
|
-
* type of iteration to use when adding multiple keys or nodes to the binary
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
* @returns The
|
|
197
|
+
* type of iteration to use when adding multiple keys or nodes to the binary tree. It has a default
|
|
198
|
+
* value of `this.iterationType`, which means it will use the iteration type specified by the binary
|
|
199
|
+
* tree instance.
|
|
200
|
+
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
202
201
|
*/
|
|
203
|
-
addMany(
|
|
204
|
-
// TODO this addMany function is inefficient, it should be optimized
|
|
205
|
-
function hasNoUndefined(arr) {
|
|
206
|
-
return arr.indexOf(undefined) === -1;
|
|
207
|
-
}
|
|
208
|
-
if (!isBalanceAdd || !hasNoUndefined(keysOrNodes)) {
|
|
209
|
-
return super.addMany(keysOrNodes, data).map(n => n ?? undefined);
|
|
210
|
-
}
|
|
202
|
+
addMany(keysOrNodesOrEntries, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
211
203
|
const inserted = [];
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
return false;
|
|
204
|
+
if (!isBalanceAdd) {
|
|
205
|
+
for (const kve of keysOrNodesOrEntries) {
|
|
206
|
+
const nn = this.add(kve);
|
|
207
|
+
inserted.push(nn);
|
|
208
|
+
}
|
|
209
|
+
return inserted;
|
|
219
210
|
}
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return
|
|
211
|
+
const realBTNExemplars = [];
|
|
212
|
+
const isRealBTNExemplar = (kve) => {
|
|
213
|
+
if (kve === undefined || kve === null)
|
|
214
|
+
return false;
|
|
215
|
+
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
225
216
|
};
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
|
|
229
|
-
}
|
|
230
|
-
else if (_isBinaryTreeKeyOrNullTuple(combinedArr)) {
|
|
231
|
-
sorted = combinedArr.sort((a, b) => a[0] - b[0]);
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
throw new Error('Invalid input keysOrNodes');
|
|
217
|
+
for (const kve of keysOrNodesOrEntries) {
|
|
218
|
+
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
235
219
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
220
|
+
// TODO this addMany function is inefficient, it should be optimized
|
|
221
|
+
let sorted = [];
|
|
222
|
+
sorted = realBTNExemplars.sort((a, b) => {
|
|
223
|
+
let aR, bR;
|
|
224
|
+
if (this.isEntry(a))
|
|
225
|
+
aR = a[0];
|
|
226
|
+
else if (this.isRealNode(a))
|
|
227
|
+
aR = a.key;
|
|
228
|
+
else
|
|
229
|
+
aR = a;
|
|
230
|
+
if (this.isEntry(b))
|
|
231
|
+
bR = b[0];
|
|
232
|
+
else if (this.isRealNode(b))
|
|
233
|
+
bR = b.key;
|
|
234
|
+
else
|
|
235
|
+
bR = b;
|
|
236
|
+
return aR - bR;
|
|
237
|
+
});
|
|
238
|
+
const _dfs = (arr) => {
|
|
239
239
|
if (arr.length === 0)
|
|
240
240
|
return;
|
|
241
241
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
242
|
-
const newNode = this.add(arr[mid]
|
|
242
|
+
const newNode = this.add(arr[mid]);
|
|
243
243
|
inserted.push(newNode);
|
|
244
|
-
_dfs(arr.slice(0, mid)
|
|
245
|
-
_dfs(arr.slice(mid + 1)
|
|
244
|
+
_dfs(arr.slice(0, mid));
|
|
245
|
+
_dfs(arr.slice(mid + 1));
|
|
246
246
|
};
|
|
247
247
|
const _iterate = () => {
|
|
248
248
|
const n = sorted.length;
|
|
@@ -253,7 +253,7 @@ export class BST extends BinaryTree {
|
|
|
253
253
|
const [l, r] = popped;
|
|
254
254
|
if (l <= r) {
|
|
255
255
|
const m = l + Math.floor((r - l) / 2);
|
|
256
|
-
const newNode = this.add(
|
|
256
|
+
const newNode = this.add(sorted[m]);
|
|
257
257
|
inserted.push(newNode);
|
|
258
258
|
stack.push([m + 1, r]);
|
|
259
259
|
stack.push([l, m - 1]);
|
|
@@ -262,7 +262,7 @@ export class BST extends BinaryTree {
|
|
|
262
262
|
}
|
|
263
263
|
};
|
|
264
264
|
if (iterationType === IterationType.RECURSIVE) {
|
|
265
|
-
_dfs(
|
|
265
|
+
_dfs(sorted);
|
|
266
266
|
}
|
|
267
267
|
else {
|
|
268
268
|
_iterate();
|
|
@@ -350,7 +350,7 @@ export class BST extends BinaryTree {
|
|
|
350
350
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
351
351
|
*/
|
|
352
352
|
/**
|
|
353
|
-
* The function `
|
|
353
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
354
354
|
* otherwise it returns the key itself.
|
|
355
355
|
* @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
|
|
356
356
|
* `undefined`.
|
|
@@ -358,7 +358,7 @@ export class BST extends BinaryTree {
|
|
|
358
358
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
359
359
|
* @returns either a node object (N) or undefined.
|
|
360
360
|
*/
|
|
361
|
-
|
|
361
|
+
ensureNode(key, iterationType = IterationType.ITERATIVE) {
|
|
362
362
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
363
363
|
}
|
|
364
364
|
/**
|
|
@@ -385,7 +385,7 @@ export class BST extends BinaryTree {
|
|
|
385
385
|
* @returns The method returns an array of nodes (`N[]`).
|
|
386
386
|
*/
|
|
387
387
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
388
|
-
beginRoot = this.
|
|
388
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
389
389
|
if (!beginRoot)
|
|
390
390
|
return [];
|
|
391
391
|
const ans = [];
|
|
@@ -466,7 +466,7 @@ export class BST extends BinaryTree {
|
|
|
466
466
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
467
467
|
*/
|
|
468
468
|
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
469
|
-
targetNode = this.
|
|
469
|
+
targetNode = this.ensureNode(targetNode);
|
|
470
470
|
const ans = [];
|
|
471
471
|
if (!targetNode)
|
|
472
472
|
return ans;
|
|
@@ -531,7 +531,7 @@ export class BST extends BinaryTree {
|
|
|
531
531
|
return;
|
|
532
532
|
const m = l + Math.floor((r - l) / 2);
|
|
533
533
|
const midNode = sorted[m];
|
|
534
|
-
this.add(midNode.key, midNode.value);
|
|
534
|
+
this.add([midNode.key, midNode.value]);
|
|
535
535
|
buildBalanceBST(l, m - 1);
|
|
536
536
|
buildBalanceBST(m + 1, r);
|
|
537
537
|
};
|
|
@@ -548,7 +548,7 @@ export class BST extends BinaryTree {
|
|
|
548
548
|
const m = l + Math.floor((r - l) / 2);
|
|
549
549
|
const midNode = sorted[m];
|
|
550
550
|
debugger;
|
|
551
|
-
this.add(midNode.key, midNode.value);
|
|
551
|
+
this.add([midNode.key, midNode.value]);
|
|
552
552
|
stack.push([m + 1, r]);
|
|
553
553
|
stack.push([l, m - 1]);
|
|
554
554
|
}
|
|
@@ -624,23 +624,6 @@ export class BST extends BinaryTree {
|
|
|
624
624
|
}
|
|
625
625
|
return balanced;
|
|
626
626
|
}
|
|
627
|
-
/**
|
|
628
|
-
* Time Complexity: O(n) - Visiting each node once.
|
|
629
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
630
|
-
*/
|
|
631
|
-
init(elements) {
|
|
632
|
-
if (elements) {
|
|
633
|
-
for (const entryOrKey of elements) {
|
|
634
|
-
if (Array.isArray(entryOrKey)) {
|
|
635
|
-
const [key, value] = entryOrKey;
|
|
636
|
-
this.add(key, value);
|
|
637
|
-
}
|
|
638
|
-
else {
|
|
639
|
-
this.add(entryOrKey);
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
627
|
_setRoot(v) {
|
|
645
628
|
if (v) {
|
|
646
629
|
v.parent = undefined;
|