data-structure-typed 1.51.9 → 1.52.1
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/README.md +141 -54
- package/benchmark/report.html +37 -1
- package/benchmark/report.json +385 -13
- package/dist/cjs/data-structures/base/index.d.ts +2 -1
- package/dist/cjs/data-structures/base/index.js +2 -1
- package/dist/cjs/data-structures/base/index.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/cjs/data-structures/base/iterable-element-base.js +226 -0
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -0
- package/dist/cjs/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/cjs/data-structures/base/{iterable-base.js → iterable-entry-base.js} +13 -190
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +13 -13
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +13 -13
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +99 -99
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +54 -52
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +37 -45
- package/dist/cjs/data-structures/binary-tree/bst.js +17 -25
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +10 -10
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +6 -6
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +5 -5
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js +2 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +43 -114
- package/dist/cjs/data-structures/heap/heap.js +59 -127
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/cjs/data-structures/heap/max-heap.js +76 -10
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/cjs/data-structures/heap/min-heap.js +68 -11
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +26 -28
- 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 +22 -25
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +27 -18
- package/dist/cjs/data-structures/queue/deque.js +43 -21
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +8 -29
- package/dist/cjs/data-structures/queue/queue.js +15 -32
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +17 -22
- package/dist/cjs/data-structures/stack/stack.js +25 -24
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +18 -13
- package/dist/cjs/data-structures/trie/trie.js +26 -15
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +4 -4
- package/dist/cjs/types/common.d.ts +1 -22
- package/dist/cjs/types/data-structures/base/base.d.ts +5 -2
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +20 -4
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +5 -3
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -3
- package/dist/cjs/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/cjs/types/data-structures/queue/deque.d.ts +4 -2
- package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/cjs/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/cjs/utils/number.js +1 -2
- package/dist/cjs/utils/number.js.map +1 -1
- package/dist/cjs/utils/utils.js +2 -2
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/base/index.d.ts +2 -1
- package/dist/mjs/data-structures/base/index.js +2 -1
- package/dist/mjs/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/mjs/data-structures/base/iterable-element-base.js +222 -0
- package/dist/mjs/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/mjs/data-structures/base/{iterable-base.js → iterable-entry-base.js} +10 -186
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +13 -13
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +13 -13
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +99 -99
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +54 -52
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +37 -45
- package/dist/mjs/data-structures/binary-tree/bst.js +17 -25
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +10 -10
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +6 -6
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +5 -5
- package/dist/mjs/data-structures/graph/directed-graph.js +2 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/mjs/data-structures/heap/heap.d.ts +43 -114
- package/dist/mjs/data-structures/heap/heap.js +60 -128
- package/dist/mjs/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/mjs/data-structures/heap/max-heap.js +79 -10
- package/dist/mjs/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/mjs/data-structures/heap/min-heap.js +68 -11
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +82 -10
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +27 -18
- package/dist/mjs/data-structures/queue/deque.js +43 -21
- package/dist/mjs/data-structures/queue/queue.d.ts +8 -29
- package/dist/mjs/data-structures/queue/queue.js +15 -32
- package/dist/mjs/data-structures/stack/stack.d.ts +17 -22
- package/dist/mjs/data-structures/stack/stack.js +25 -24
- package/dist/mjs/data-structures/trie/trie.d.ts +18 -13
- package/dist/mjs/data-structures/trie/trie.js +26 -15
- package/dist/mjs/interfaces/binary-tree.d.ts +4 -4
- package/dist/mjs/types/common.d.ts +1 -22
- package/dist/mjs/types/data-structures/base/base.d.ts +5 -2
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +20 -4
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +5 -3
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -3
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -3
- package/dist/mjs/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/mjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/mjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/mjs/types/data-structures/queue/deque.d.ts +4 -2
- package/dist/mjs/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/mjs/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/umd/data-structure-typed.js +732 -409
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +14 -15
- package/src/data-structures/binary-tree/avl-tree.ts +13 -14
- package/src/data-structures/binary-tree/binary-tree.ts +156 -152
- package/src/data-structures/binary-tree/bst.ts +52 -60
- package/src/data-structures/binary-tree/rb-tree.ts +12 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +12 -13
- package/src/data-structures/graph/directed-graph.ts +2 -1
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +50 -25
- package/src/data-structures/queue/queue.ts +23 -37
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +33 -18
- package/src/interfaces/binary-tree.ts +4 -5
- package/src/types/common.ts +2 -24
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +24 -5
- package/src/types/data-structures/binary-tree/bst.ts +9 -3
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +6 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +1 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +28 -0
- package/test/unit/data-structures/heap/heap.test.ts +100 -0
- package/test/unit/data-structures/heap/max-heap.test.ts +44 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +18 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +42 -0
- package/test/unit/data-structures/queue/deque.test.ts +49 -0
- package/test/unit/data-structures/queue/queue.test.ts +56 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +0 -1
|
@@ -40,7 +40,7 @@ export class BinaryTreeNode {
|
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* The function sets the left child of a node and updates its parent reference.
|
|
43
|
-
* @param {NODE
|
|
43
|
+
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
44
44
|
* `undefined`.
|
|
45
45
|
*/
|
|
46
46
|
set left(v) {
|
|
@@ -60,7 +60,7 @@ export class BinaryTreeNode {
|
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
62
|
* The function sets the right child of a node and updates its parent.
|
|
63
|
-
* @param {NODE
|
|
63
|
+
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
64
64
|
* `undefined`.
|
|
65
65
|
*/
|
|
66
66
|
set right(v) {
|
|
@@ -98,7 +98,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
98
98
|
iterationType = 'ITERATIVE';
|
|
99
99
|
/**
|
|
100
100
|
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
|
|
101
|
-
* @param [keysOrNodesOrEntriesOrRawElements] -
|
|
101
|
+
* @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of BTNKeyOrNodeOrEntry objects. These objects represent the
|
|
102
102
|
* nodes to be added to the binary tree.
|
|
103
103
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
104
104
|
* configuration options for the binary tree. In this case, it is of type
|
|
@@ -113,6 +113,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
113
113
|
this.iterationType = iterationType;
|
|
114
114
|
if (typeof toEntryFn === 'function')
|
|
115
115
|
this._toEntryFn = toEntryFn;
|
|
116
|
+
else if (toEntryFn)
|
|
117
|
+
throw TypeError('toEntryFn must be a function type');
|
|
116
118
|
}
|
|
117
119
|
if (keysOrNodesOrEntriesOrRawElements)
|
|
118
120
|
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
@@ -172,8 +174,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
172
174
|
/**
|
|
173
175
|
* The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
|
|
174
176
|
* into a node object.
|
|
175
|
-
* @param {R |
|
|
176
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
177
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
178
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
177
179
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
178
180
|
* `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
|
|
179
181
|
* key-value pair. If provided, it will be used to create a node with the specified key and value.
|
|
@@ -217,8 +219,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
217
219
|
*
|
|
218
220
|
* The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
|
|
219
221
|
* node if it is a key or entry.
|
|
220
|
-
* @param {R |
|
|
221
|
-
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `
|
|
222
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
223
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
|
|
222
224
|
* a raw element.
|
|
223
225
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
224
226
|
* parameter that specifies the type of iteration to be used when searching for a node. It has a
|
|
@@ -253,8 +255,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
253
255
|
}
|
|
254
256
|
/**
|
|
255
257
|
* The function checks if the input is an instance of the BinaryTreeNode class.
|
|
256
|
-
* @param {R |
|
|
257
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
258
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
259
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
258
260
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
259
261
|
* an instance of the `BinaryTreeNode` class.
|
|
260
262
|
*/
|
|
@@ -263,8 +265,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
263
265
|
}
|
|
264
266
|
/**
|
|
265
267
|
* The function checks if a given node is a valid node in a binary search tree.
|
|
266
|
-
* @param {R |
|
|
267
|
-
* `
|
|
268
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
269
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
268
270
|
* @returns a boolean value.
|
|
269
271
|
*/
|
|
270
272
|
isRealNode(node) {
|
|
@@ -274,8 +276,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
274
276
|
}
|
|
275
277
|
/**
|
|
276
278
|
* The function checks if a given node is a real node or null.
|
|
277
|
-
* @param {R |
|
|
278
|
-
* `
|
|
279
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
280
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
279
281
|
* @returns a boolean value.
|
|
280
282
|
*/
|
|
281
283
|
isNodeOrNull(node) {
|
|
@@ -283,8 +285,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
283
285
|
}
|
|
284
286
|
/**
|
|
285
287
|
* The function checks if a given node is equal to the NIL value.
|
|
286
|
-
* @param {R |
|
|
287
|
-
* `
|
|
288
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
289
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
288
290
|
* @returns a boolean value.
|
|
289
291
|
*/
|
|
290
292
|
isNIL(node) {
|
|
@@ -293,8 +295,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
293
295
|
/**
|
|
294
296
|
* The function checks if the input is an array with two elements, indicating it is a binary tree
|
|
295
297
|
* node entry.
|
|
296
|
-
* @param {R |
|
|
297
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
298
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
299
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
298
300
|
* @returns a boolean value.
|
|
299
301
|
*/
|
|
300
302
|
isEntry(keyOrNodeOrEntryOrRawElement) {
|
|
@@ -342,10 +344,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
342
344
|
*
|
|
343
345
|
* The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
|
|
344
346
|
* and finding the appropriate insertion position.
|
|
345
|
-
* @param {R |
|
|
347
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
346
348
|
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
|
|
347
349
|
* node, entry, or raw element to be added to the tree. It can also accept a value of type
|
|
348
|
-
* `
|
|
350
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
349
351
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
350
352
|
* key being added to the tree. It represents the value that will be stored in the tree for the given
|
|
351
353
|
* key.
|
|
@@ -447,7 +449,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
447
449
|
*
|
|
448
450
|
* The `refill` function clears the current data and adds new data to the collection.
|
|
449
451
|
* @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
|
|
450
|
-
* elements. These can be of any type (R) or a specific type (
|
|
452
|
+
* elements. These can be of any type (R) or a specific type (BTNKeyOrNodeOrEntry<K, V, NODE>).
|
|
451
453
|
* @param [values] - The `values` parameter is an optional iterable of values that will be associated
|
|
452
454
|
* with the keys or nodes being added. If provided, the values will be assigned to the corresponding
|
|
453
455
|
* keys or nodes. If not provided, the values will be set to `undefined`.
|
|
@@ -542,7 +544,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
542
544
|
* the identifier or all nodes that match the identifier. If set to true, only the first matching
|
|
543
545
|
* node will be returned. If set to false, all matching nodes will be returned. The default value is
|
|
544
546
|
* false.
|
|
545
|
-
* @param {R |
|
|
547
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
546
548
|
* point for the search. It can be either a node object, a key-value pair, or a key. If it is not
|
|
547
549
|
* provided, the `root` of the data structure is used as the starting point.
|
|
548
550
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
@@ -601,7 +603,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
601
603
|
* the `C` callback function, or it can be `null` or `undefined`.
|
|
602
604
|
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
603
605
|
* node matches the desired criteria. It should return a value that can be used to identify the node.
|
|
604
|
-
* @param {R |
|
|
606
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
605
607
|
* point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
|
|
606
608
|
* a node entry. If not provided, the search will start from the root of the tree.
|
|
607
609
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -645,7 +647,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
645
647
|
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
646
648
|
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
647
649
|
* node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
648
|
-
* @param {R |
|
|
650
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
649
651
|
* point for the search in the binary tree. It can be either a root node of the tree or a key, node,
|
|
650
652
|
* or entry object that exists in the tree. If no specific starting point is provided, the search
|
|
651
653
|
* will begin from the root of the
|
|
@@ -675,7 +677,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
675
677
|
* @param {C} callback - The `callback` parameter is a function that will be used to determine
|
|
676
678
|
* whether a node should be included in the result or not. It is of type `C`, which extends the
|
|
677
679
|
* `BTNCallback<NODE>` type.
|
|
678
|
-
* @param {R |
|
|
680
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
679
681
|
* point for the iteration in the data structure. It can be either a root node, a key-value pair, or
|
|
680
682
|
* a node entry. If not specified, it defaults to the root of the data structure.
|
|
681
683
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -724,10 +726,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
724
726
|
*
|
|
725
727
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
726
728
|
* height of the tree.
|
|
727
|
-
* @param {R |
|
|
729
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
|
|
728
730
|
* has a default value of `this.root`. It represents the starting point for checking if the tree is
|
|
729
731
|
* perfectly balanced. It can be either a root node (`R`), a key or node or entry
|
|
730
|
-
* (`
|
|
732
|
+
* (`BTNKeyOrNodeOrEntry<K, V, NODE
|
|
731
733
|
* @returns a boolean value.
|
|
732
734
|
*/
|
|
733
735
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
@@ -742,7 +744,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
742
744
|
* Space Complexity: O(1)
|
|
743
745
|
*
|
|
744
746
|
* The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
|
|
745
|
-
* @param {R |
|
|
747
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
746
748
|
* starting point for checking if a binary search tree (BST) is valid. It can be either a root node
|
|
747
749
|
* of the BST, a key value of a node in the BST, or an entry object containing both the key and value
|
|
748
750
|
* of a node in the BST
|
|
@@ -802,10 +804,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
802
804
|
* Space Complexity: O(1)
|
|
803
805
|
*
|
|
804
806
|
* The function calculates the depth of a given node or key in a tree-like data structure.
|
|
805
|
-
* @param {R |
|
|
806
|
-
* (representing a root node), or a `
|
|
807
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
|
|
808
|
+
* (representing a root node), or a `BTNKeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
|
|
807
809
|
* entry).
|
|
808
|
-
* @param {R |
|
|
810
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
|
|
809
811
|
* represents the starting point from which to calculate the depth. It can be either a reference to a
|
|
810
812
|
* node in the tree or a key-value pair or an entry object. If not provided, the default value is
|
|
811
813
|
* `this.root`, which refers to the root node
|
|
@@ -834,9 +836,9 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
834
836
|
*
|
|
835
837
|
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
836
838
|
* or iterative approach.
|
|
837
|
-
* @param {R |
|
|
839
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
838
840
|
* starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
|
|
839
|
-
* node or entry (`
|
|
841
|
+
* node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
|
|
840
842
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
841
843
|
* iteration used to calculate the height of the tree. It can have two possible values:
|
|
842
844
|
* @returns the maximum height of the binary tree.
|
|
@@ -879,9 +881,9 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
879
881
|
*
|
|
880
882
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
881
883
|
* recursive or iterative approach.
|
|
882
|
-
* @param {R |
|
|
884
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
883
885
|
* starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
|
|
884
|
-
* key or node or entry (`
|
|
886
|
+
* key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
|
|
885
887
|
* tree.
|
|
886
888
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
887
889
|
* iteration to be used when calculating the minimum height of the tree. It can have two possible
|
|
@@ -943,8 +945,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
943
945
|
*
|
|
944
946
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
945
947
|
* up to the root node, with an option to reverse the order of the nodes.
|
|
946
|
-
* @param {R |
|
|
947
|
-
* type `R` or `
|
|
948
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
|
|
949
|
+
* type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
948
950
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
949
951
|
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
950
952
|
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
@@ -973,9 +975,9 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
973
975
|
*
|
|
974
976
|
* The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
|
|
975
977
|
* iterative traversal.
|
|
976
|
-
* @param {R |
|
|
978
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
977
979
|
* starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
|
|
978
|
-
* a key or node or entry (`
|
|
980
|
+
* a key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
979
981
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
980
982
|
* of iteration to be performed. It can have two possible values:
|
|
981
983
|
* @returns The function `getLeftMost` returns the leftmost node in a binary tree.
|
|
@@ -1014,9 +1016,9 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1014
1016
|
*
|
|
1015
1017
|
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
|
|
1016
1018
|
* iteratively.
|
|
1017
|
-
* @param {R |
|
|
1019
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1018
1020
|
* starting point for finding the rightmost node in a binary tree. It can be either a root node
|
|
1019
|
-
* (`R`), a key or node or entry (`
|
|
1021
|
+
* (`R`), a key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
1020
1022
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
1021
1023
|
* of iteration to be performed when finding the rightmost node in a binary tree. It can have two
|
|
1022
1024
|
* possible values:
|
|
@@ -1116,7 +1118,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1116
1118
|
* return type of the callback function is determined by the generic type `C`.
|
|
1117
1119
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
|
|
1118
1120
|
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
1119
|
-
* @param {R |
|
|
1121
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1120
1122
|
* point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
|
|
1121
1123
|
* is a key or key-value pair, the method will find the corresponding node in the tree and start the
|
|
1122
1124
|
* search from there.
|
|
@@ -1247,7 +1249,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1247
1249
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1248
1250
|
* the breadth-first search traversal. It takes a single argument, which is the current node being
|
|
1249
1251
|
* visited, and returns a value of any type.
|
|
1250
|
-
* @param {R |
|
|
1252
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1251
1253
|
* starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
|
|
1252
1254
|
* or entry object. If no value is provided, the `root` property of the class is used as the default
|
|
1253
1255
|
* starting point.
|
|
@@ -1325,7 +1327,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1325
1327
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1326
1328
|
* the tree. It takes a node as an argument and returns a value. The return type of the callback
|
|
1327
1329
|
* function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
|
|
1328
|
-
* @param {R |
|
|
1330
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1329
1331
|
* starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
|
|
1330
1332
|
* entry. If no value is provided, the `root` property of the class is used as the default starting
|
|
1331
1333
|
* point.
|
|
@@ -1403,7 +1405,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1403
1405
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
|
|
1404
1406
|
* to specify the order in which the nodes of a binary tree are traversed. It can take one of the
|
|
1405
1407
|
* following values:
|
|
1406
|
-
* @param {R |
|
|
1408
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1407
1409
|
* point for the traversal. It can be either a node object, a key, or an entry object. If no value is
|
|
1408
1410
|
* provided, the `root` of the tree is used as the starting point.
|
|
1409
1411
|
* @returns The function `morris` returns an array of values that are the return values of the
|
|
@@ -1589,7 +1591,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1589
1591
|
* Space Complexity: O(n)
|
|
1590
1592
|
*
|
|
1591
1593
|
* The `print` function in TypeScript prints the binary tree structure with customizable options.
|
|
1592
|
-
* @param {R |
|
|
1594
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1593
1595
|
* point for printing the binary tree. It can be either a node of the binary tree or a key or entry
|
|
1594
1596
|
* that exists in the binary tree. If no value is provided, the root of the binary tree will be used
|
|
1595
1597
|
* as the starting point.
|
|
@@ -1672,7 +1674,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1672
1674
|
*
|
|
1673
1675
|
* The `_displayAux` function is responsible for generating the display layout of a binary tree node,
|
|
1674
1676
|
* taking into account various options such as whether to show null, undefined, or NaN nodes.
|
|
1675
|
-
* @param {NODE
|
|
1677
|
+
* @param {OptBTNOrNull<NODE>} node - The `node` parameter represents a node in a binary tree.
|
|
1676
1678
|
* It can be of type `NODE`, `null`, or `undefined`.
|
|
1677
1679
|
* @param {BinaryTreePrintOptions} options - The `options` parameter is an object that contains the
|
|
1678
1680
|
* following properties:
|
|
@@ -1698,12 +1700,12 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1698
1700
|
}
|
|
1699
1701
|
else if (node !== null && node !== undefined) {
|
|
1700
1702
|
// Display logic of normal nodes
|
|
1701
|
-
const key = node.key, line = this.isNIL(node) ? 'S' : key
|
|
1703
|
+
const key = node.key, line = this.isNIL(node) ? 'S' : String(key), width = line.length;
|
|
1702
1704
|
return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
|
|
1703
1705
|
}
|
|
1704
1706
|
else {
|
|
1705
1707
|
// For cases where none of the conditions are met, null, undefined, and NaN nodes are not displayed
|
|
1706
|
-
const line = node === undefined ? 'U' : '
|
|
1708
|
+
const line = node === undefined ? 'U' : 'N', width = line.length;
|
|
1707
1709
|
return _buildNodeDisplay(line, width, [[''], 1, 0, 0], [[''], 1, 0, 0]);
|
|
1708
1710
|
}
|
|
1709
1711
|
function _buildNodeDisplay(line, width, left, right) {
|
|
@@ -1745,10 +1747,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1745
1747
|
* Space Complexity: O(1)
|
|
1746
1748
|
*
|
|
1747
1749
|
* The function `_swapProperties` swaps the key-value properties between two nodes.
|
|
1748
|
-
* @param {R |
|
|
1750
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
|
|
1749
1751
|
* destination node. It can be either an instance of the class `R`, or an object of type
|
|
1750
|
-
* `
|
|
1751
|
-
* @param {R |
|
|
1752
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
1753
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
|
|
1752
1754
|
* the properties will be swapped with the `srcNode`.
|
|
1753
1755
|
* @returns either the `destNode` object with its properties swapped with the `srcNode` object's
|
|
1754
1756
|
* properties, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
@@ -1812,7 +1814,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1812
1814
|
*
|
|
1813
1815
|
* The function sets the root property of an object to the provided value, and also updates the
|
|
1814
1816
|
* parent property of the new root.
|
|
1815
|
-
* @param {NODE
|
|
1817
|
+
* @param {OptBTNOrNull<NODE>} v - The parameter `v` is of type `OptBTNOrNull<NODE>`. This
|
|
1816
1818
|
* means that it can accept a value of type `NODE`, `null`, or `undefined`.
|
|
1817
1819
|
*/
|
|
1818
1820
|
_setRoot(v) {
|
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BSTNested, BSTNKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback,
|
|
8
|
+
import type { BSTNested, BSTNKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNKeyOrNodeOrEntry, Comparator, CP, DFSOrderPattern, IterationType, OptBSTN } from '../../types';
|
|
9
9
|
import { BTNEntry } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
|
-
export declare class BSTNode<K
|
|
12
|
+
export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
|
|
13
13
|
parent?: NODE;
|
|
14
14
|
constructor(key: K, value?: V);
|
|
15
15
|
protected _left?: NODE;
|
|
@@ -17,26 +17,26 @@ export declare class BSTNode<K extends Comparable, V = any, NODE extends BSTNode
|
|
|
17
17
|
* The function returns the value of the `_left` property.
|
|
18
18
|
* @returns The `_left` property of the current object is being returned.
|
|
19
19
|
*/
|
|
20
|
-
get left(): NODE
|
|
20
|
+
get left(): OptBSTN<NODE>;
|
|
21
21
|
/**
|
|
22
22
|
* The function sets the left child of a node and updates the parent reference of the child.
|
|
23
|
-
* @param {NODE
|
|
23
|
+
* @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be an
|
|
24
24
|
* instance of the `NODE` class or `undefined`.
|
|
25
25
|
*/
|
|
26
|
-
set left(v: NODE
|
|
26
|
+
set left(v: OptBSTN<NODE>);
|
|
27
27
|
protected _right?: NODE;
|
|
28
28
|
/**
|
|
29
29
|
* The function returns the right node of a binary tree or undefined if there is no right node.
|
|
30
30
|
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
|
|
31
31
|
* `undefined`.
|
|
32
32
|
*/
|
|
33
|
-
get right(): NODE
|
|
33
|
+
get right(): OptBSTN<NODE>;
|
|
34
34
|
/**
|
|
35
35
|
* The function sets the right child of a node and updates the parent reference of the child.
|
|
36
|
-
* @param {NODE
|
|
36
|
+
* @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be a
|
|
37
37
|
* `NODE` object or `undefined`.
|
|
38
38
|
*/
|
|
39
|
-
set right(v: NODE
|
|
39
|
+
set right(v: OptBSTN<NODE>);
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
|
|
@@ -47,7 +47,7 @@ export declare class BSTNode<K extends Comparable, V = any, NODE extends BSTNode
|
|
|
47
47
|
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
48
48
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
49
49
|
*/
|
|
50
|
-
export declare class BST<K
|
|
50
|
+
export declare class BST<K = any, V = any, R = BTNEntry<K, V>, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>> extends BinaryTree<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
51
51
|
/**
|
|
52
52
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
53
53
|
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
@@ -56,13 +56,13 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
56
56
|
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
57
57
|
* It can include a comparator function that defines the order of the elements in the tree.
|
|
58
58
|
*/
|
|
59
|
-
constructor(keysOrNodesOrEntriesOrRawElements?: Iterable<R |
|
|
59
|
+
constructor(keysOrNodesOrEntriesOrRawElements?: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>, options?: BSTOptions<K, V, R>);
|
|
60
60
|
protected _root?: NODE;
|
|
61
61
|
/**
|
|
62
62
|
* The function returns the root node of a tree structure.
|
|
63
63
|
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
64
64
|
*/
|
|
65
|
-
get root(): NODE
|
|
65
|
+
get root(): OptBSTN<NODE>;
|
|
66
66
|
/**
|
|
67
67
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
68
68
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -82,21 +82,21 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
82
82
|
createTree(options?: Partial<BSTOptions<K, V, R>>): TREE;
|
|
83
83
|
/**
|
|
84
84
|
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
85
|
-
* @param {R |
|
|
86
|
-
* type R or
|
|
85
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
|
|
86
|
+
* type R or BTNKeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
87
87
|
* element.
|
|
88
88
|
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
89
89
|
* value associated with a key in a key-value pair.
|
|
90
90
|
* @returns either a NODE object or undefined.
|
|
91
91
|
*/
|
|
92
|
-
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R |
|
|
92
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): OptBSTN<NODE>;
|
|
93
93
|
/**
|
|
94
94
|
* Time Complexity: O(log n)
|
|
95
95
|
* Space Complexity: O(log n)
|
|
96
96
|
*
|
|
97
97
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
98
98
|
* it doesn't exist.
|
|
99
|
-
* @param {R |
|
|
99
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
100
100
|
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
|
|
101
101
|
* entry, or raw element that needs to be ensured in the tree.
|
|
102
102
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
@@ -105,27 +105,27 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
105
105
|
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
106
106
|
* not be ensured.
|
|
107
107
|
*/
|
|
108
|
-
ensureNode(keyOrNodeOrEntryOrRawElement: R |
|
|
108
|
+
ensureNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): OptBSTN<NODE>;
|
|
109
109
|
/**
|
|
110
110
|
* The function checks if the input is an instance of the BSTNode class.
|
|
111
|
-
* @param {R |
|
|
112
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
111
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
112
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
113
113
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
114
114
|
* an instance of the `BSTNode` class.
|
|
115
115
|
*/
|
|
116
|
-
isNode(keyOrNodeOrEntryOrRawElement: R |
|
|
116
|
+
isNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE;
|
|
117
117
|
/**
|
|
118
118
|
* Time Complexity: O(log n)
|
|
119
119
|
* Space Complexity: O(1)
|
|
120
120
|
*
|
|
121
121
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
122
|
-
* @param {R |
|
|
123
|
-
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `
|
|
122
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
123
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
124
124
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
125
125
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
126
126
|
* @returns a boolean value.
|
|
127
127
|
*/
|
|
128
|
-
add(keyOrNodeOrEntryOrRawElement: R |
|
|
128
|
+
add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
|
|
129
129
|
/**
|
|
130
130
|
* Time Complexity: O(log n)
|
|
131
131
|
* Space Complexity: O(log n)
|
|
@@ -151,7 +151,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
151
151
|
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
152
152
|
* successfully inserted into the data structure.
|
|
153
153
|
*/
|
|
154
|
-
addMany(keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
154
|
+
addMany(keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
155
155
|
/**
|
|
156
156
|
* Time Complexity: O(log n)
|
|
157
157
|
* Space Complexity: O(k + log n)
|
|
@@ -167,14 +167,14 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
167
167
|
* @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
|
|
168
168
|
* or all matching nodes. If set to true, only the first matching node will be returned. If set to
|
|
169
169
|
* false, all matching nodes will be returned. The default value is false.
|
|
170
|
-
* @param {R |
|
|
170
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
171
171
|
* point for the search in the binary tree. It can be either a node object, a key-value pair, or an
|
|
172
172
|
* entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
|
|
173
173
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
174
174
|
* iteration to be performed. It can have two possible values:
|
|
175
175
|
* @returns The method `getNodes` returns an array of `NODE` objects.
|
|
176
176
|
*/
|
|
177
|
-
getNodes<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: R |
|
|
177
|
+
getNodes<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE[];
|
|
178
178
|
/**
|
|
179
179
|
* Time Complexity: O(log n)
|
|
180
180
|
* Space Complexity: O(1)
|
|
@@ -200,7 +200,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
200
200
|
* of the following values:
|
|
201
201
|
* @returns The method is returning a NODE object or undefined.
|
|
202
202
|
*/
|
|
203
|
-
getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: R | BSTNKeyOrNode<K, NODE>, iterationType?: IterationType): NODE
|
|
203
|
+
getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: R | BSTNKeyOrNode<K, NODE>, iterationType?: IterationType): OptBSTN<NODE>;
|
|
204
204
|
/**
|
|
205
205
|
* Time Complexity: O(k log n)
|
|
206
206
|
* Space Complexity: O(k + log n)
|
|
@@ -218,7 +218,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
218
218
|
* It has a default value of `'ITERATIVE'`.
|
|
219
219
|
* @returns The method is returning a NODE object or undefined.
|
|
220
220
|
*/
|
|
221
|
-
getNodeByKey(key: K, iterationType?: IterationType): NODE
|
|
221
|
+
getNodeByKey(key: K, iterationType?: IterationType): OptBSTN<NODE>;
|
|
222
222
|
/**
|
|
223
223
|
* Time Complexity: O(log n)
|
|
224
224
|
* Space Complexity: O(k + log n)
|
|
@@ -235,7 +235,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
235
235
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
236
236
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
237
237
|
* take one of the following values:
|
|
238
|
-
* @param {R |
|
|
238
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
239
239
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
240
240
|
* node entry. If not specified, the default value is the root of the tree.
|
|
241
241
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -243,7 +243,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
243
243
|
* following values:
|
|
244
244
|
* @returns The method is returning an array of the return type of the callback function.
|
|
245
245
|
*/
|
|
246
|
-
dfs<C extends BTNCallback<NODE>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: R |
|
|
246
|
+
dfs<C extends BTNCallback<NODE>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): ReturnType<C>[];
|
|
247
247
|
/**
|
|
248
248
|
* Time Complexity: O(log n)
|
|
249
249
|
* Space Complexity: O(1)
|
|
@@ -257,7 +257,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
257
257
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
258
258
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
259
259
|
* node being visited, and it can return a value of any type.
|
|
260
|
-
* @param {R |
|
|
260
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
261
261
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
262
262
|
* object. If no value is provided, the default value is the root of the tree.
|
|
263
263
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -265,7 +265,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
265
265
|
* the following values:
|
|
266
266
|
* @returns an array of the return type of the callback function.
|
|
267
267
|
*/
|
|
268
|
-
bfs<C extends BTNCallback<NODE>>(callback?: C, beginRoot?: R |
|
|
268
|
+
bfs<C extends BTNCallback<NODE>>(callback?: C, beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): ReturnType<C>[];
|
|
269
269
|
/**
|
|
270
270
|
* Time Complexity: O(log n)
|
|
271
271
|
* Space Complexity: O(1)
|
|
@@ -279,7 +279,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
279
279
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
280
280
|
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
281
281
|
* tree during the iteration process.
|
|
282
|
-
* @param {R |
|
|
282
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
283
283
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
284
284
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
285
285
|
* value is provided, the root of
|
|
@@ -288,7 +288,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
288
288
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
289
289
|
* function.
|
|
290
290
|
*/
|
|
291
|
-
listLevels<C extends BTNCallback<NODE>>(callback?: C, beginRoot?: R |
|
|
291
|
+
listLevels<C extends BTNCallback<NODE>>(callback?: C, beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): ReturnType<C>[][];
|
|
292
292
|
/**
|
|
293
293
|
* Time complexity: O(n)
|
|
294
294
|
* Space complexity: O(n)
|
|
@@ -305,7 +305,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
305
305
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
306
306
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
307
307
|
* 0, or 1, where:
|
|
308
|
-
* @param {R |
|
|
308
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
|
|
309
309
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
310
310
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
311
311
|
* `targetNode` is provided,
|
|
@@ -314,7 +314,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
314
314
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
315
315
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
316
316
|
*/
|
|
317
|
-
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(callback?: C, lesserOrGreater?: CP, targetNode?: R |
|
|
317
|
+
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(callback?: C, lesserOrGreater?: CP, targetNode?: R | BTNKeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): ReturnType<C>[];
|
|
318
318
|
/**
|
|
319
319
|
* Time complexity: O(n)
|
|
320
320
|
* Space complexity: O(n)
|
|
@@ -349,15 +349,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
349
349
|
* @returns a boolean value.
|
|
350
350
|
*/
|
|
351
351
|
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
352
|
-
/**
|
|
353
|
-
* Time complexity: O(n)
|
|
354
|
-
* Space complexity: O(n)
|
|
355
|
-
*/
|
|
356
352
|
protected _DEFAULT_COMPARATOR: (a: K, b: K) => number;
|
|
357
|
-
/**
|
|
358
|
-
* Time complexity: O(n)
|
|
359
|
-
* Space complexity: O(n)
|
|
360
|
-
*/
|
|
361
353
|
protected _comparator: Comparator<K>;
|
|
362
354
|
/**
|
|
363
355
|
* Time Complexity: O(n)
|
|
@@ -371,7 +363,7 @@ export declare class BST<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE
|
|
|
371
363
|
/**
|
|
372
364
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
373
365
|
* root.
|
|
374
|
-
* @param {NODE
|
|
366
|
+
* @param {OptBSTN<NODE>} v - v is a parameter of type NODE or undefined.
|
|
375
367
|
*/
|
|
376
|
-
protected _setRoot(v: NODE
|
|
368
|
+
protected _setRoot(v: OptBSTN<NODE>): void;
|
|
377
369
|
}
|