data-structure-typed 1.50.1 → 1.50.2
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 +2 -1
- package/README.md +26 -26
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +153 -159
- package/dist/cjs/data-structures/base/iterable-base.d.ts +114 -9
- package/dist/cjs/data-structures/base/iterable-base.js +143 -7
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/cjs/data-structures/binary-tree/bst.js +30 -71
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +26 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/cjs/data-structures/graph/map-graph.js +14 -0
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/cjs/data-structures/graph/undirected-graph.js +25 -0
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/cjs/data-structures/hash/hash-map.js +160 -25
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +66 -6
- package/dist/cjs/data-structures/heap/heap.js +66 -6
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +70 -64
- 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 +128 -103
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/cjs/data-structures/matrix/matrix.js +50 -11
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +49 -19
- package/dist/cjs/data-structures/queue/deque.js +101 -47
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +39 -5
- package/dist/cjs/data-structures/queue/queue.js +47 -5
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +16 -0
- package/dist/cjs/data-structures/stack/stack.js +22 -0
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +38 -1
- package/dist/cjs/data-structures/trie/trie.js +41 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +114 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/mjs/data-structures/binary-tree/bst.js +30 -71
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +26 -0
- package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/mjs/data-structures/graph/map-graph.js +14 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/mjs/data-structures/graph/undirected-graph.js +25 -0
- package/dist/mjs/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/mjs/data-structures/hash/hash-map.js +160 -25
- package/dist/mjs/data-structures/heap/heap.d.ts +66 -6
- package/dist/mjs/data-structures/heap/heap.js +66 -6
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +70 -64
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +128 -103
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/mjs/data-structures/matrix/matrix.js +50 -11
- package/dist/mjs/data-structures/queue/deque.d.ts +49 -19
- package/dist/mjs/data-structures/queue/deque.js +101 -47
- package/dist/mjs/data-structures/queue/queue.d.ts +39 -5
- package/dist/mjs/data-structures/queue/queue.js +47 -5
- package/dist/mjs/data-structures/stack/stack.d.ts +16 -0
- package/dist/mjs/data-structures/stack/stack.js +22 -0
- package/dist/mjs/data-structures/trie/trie.d.ts +38 -1
- package/dist/mjs/data-structures/trie/trie.js +41 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +1730 -1042
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +8 -3
- package/src/data-structures/base/iterable-base.ts +172 -19
- package/src/data-structures/binary-tree/avl-tree.ts +97 -97
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +89 -131
- package/src/data-structures/binary-tree/rb-tree.ts +127 -155
- package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +28 -0
- package/src/data-structures/hash/hash-map.ts +175 -34
- package/src/data-structures/heap/heap.ts +66 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
- package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/queue/deque.ts +108 -49
- package/src/data-structures/queue/queue.ts +51 -5
- package/src/data-structures/stack/stack.ts +24 -0
- package/src/data-structures/trie/trie.ts +45 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -3
- package/test/performance/data-structures/queue/deque.test.ts +26 -25
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
- package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
- package/test/unit/data-structures/graph/abstract-graph.test.ts +8 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +249 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
- package/test/unit/data-structures/heap/heap.test.ts +18 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +65 -5
- package/test/unit/data-structures/queue/queue.test.ts +22 -5
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- package/test/unit/data-structures/trie/trie.test.ts +17 -0
|
@@ -9,29 +9,15 @@ import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntr
|
|
|
9
9
|
import { BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
|
-
export declare class BSTNode<K = any, V = any,
|
|
13
|
-
parent?:
|
|
12
|
+
export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
|
|
13
|
+
parent?: NODE;
|
|
14
14
|
constructor(key: K, value?: V);
|
|
15
|
-
protected _left?:
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
get
|
|
20
|
-
|
|
21
|
-
* Set the left child node.
|
|
22
|
-
* @param {N | undefined} v - The left child node.
|
|
23
|
-
*/
|
|
24
|
-
set left(v: N | undefined);
|
|
25
|
-
protected _right?: N;
|
|
26
|
-
/**
|
|
27
|
-
* Get the right child node.
|
|
28
|
-
*/
|
|
29
|
-
get right(): N | undefined;
|
|
30
|
-
/**
|
|
31
|
-
* Set the right child node.
|
|
32
|
-
* @param {N | undefined} v - The right child node.
|
|
33
|
-
*/
|
|
34
|
-
set right(v: N | undefined);
|
|
15
|
+
protected _left?: NODE;
|
|
16
|
+
get left(): NODE | undefined;
|
|
17
|
+
set left(v: NODE | undefined);
|
|
18
|
+
protected _right?: NODE;
|
|
19
|
+
get right(): NODE | undefined;
|
|
20
|
+
set right(v: NODE | undefined);
|
|
35
21
|
}
|
|
36
22
|
/**
|
|
37
23
|
* 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
|
|
@@ -42,7 +28,7 @@ export declare class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTN
|
|
|
42
28
|
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
43
29
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
44
30
|
*/
|
|
45
|
-
export declare class BST<K = any, V = any,
|
|
31
|
+
export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, NODE, TREE> = BST<K, V, NODE, BSTNested<K, V, NODE>>> extends BinaryTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
|
|
46
32
|
/**
|
|
47
33
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
48
34
|
* the tree with optional keysOrNodesOrEntries and options.
|
|
@@ -51,9 +37,9 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
51
37
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
52
38
|
* configuration options for the binary search tree. It can have the following properties:
|
|
53
39
|
*/
|
|
54
|
-
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V,
|
|
55
|
-
protected _root?:
|
|
56
|
-
get root():
|
|
40
|
+
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: BSTOptions<K>);
|
|
41
|
+
protected _root?: NODE;
|
|
42
|
+
get root(): NODE | undefined;
|
|
57
43
|
protected _variant: BSTVariant;
|
|
58
44
|
get variant(): BSTVariant;
|
|
59
45
|
/**
|
|
@@ -64,7 +50,7 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
64
50
|
* represents the value associated with the node in a binary search tree.
|
|
65
51
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
66
52
|
*/
|
|
67
|
-
createNode(key: K, value?: V):
|
|
53
|
+
createNode(key: K, value?: V): NODE;
|
|
68
54
|
/**
|
|
69
55
|
* The function creates a new binary search tree with the specified options.
|
|
70
56
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
@@ -74,18 +60,17 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
74
60
|
*/
|
|
75
61
|
createTree(options?: Partial<BSTOptions<K>>): TREE;
|
|
76
62
|
/**
|
|
77
|
-
* The function `
|
|
63
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
78
64
|
* otherwise it returns undefined.
|
|
79
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
65
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
80
66
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
81
|
-
* `
|
|
82
|
-
* @returns a node of type
|
|
67
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
68
|
+
* @returns a node of type NODE or undefined.
|
|
83
69
|
*/
|
|
84
|
-
|
|
70
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined;
|
|
85
71
|
/**
|
|
86
72
|
* Time Complexity: O(log n)
|
|
87
73
|
* Space Complexity: O(log n)
|
|
88
|
-
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
89
74
|
*/
|
|
90
75
|
/**
|
|
91
76
|
* Time Complexity: O(log n)
|
|
@@ -93,23 +78,22 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
93
78
|
*
|
|
94
79
|
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
95
80
|
* otherwise it returns the key itself.
|
|
96
|
-
* @param {K |
|
|
81
|
+
* @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
|
|
97
82
|
* `undefined`.
|
|
98
83
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
99
84
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
100
|
-
* @returns either a node object (
|
|
85
|
+
* @returns either a node object (NODE) or undefined.
|
|
101
86
|
*/
|
|
102
|
-
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
87
|
+
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE | undefined;
|
|
103
88
|
/**
|
|
104
89
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
105
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,
|
|
90
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
106
91
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
107
92
|
*/
|
|
108
|
-
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
93
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
|
|
109
94
|
/**
|
|
110
95
|
* Time Complexity: O(log n)
|
|
111
96
|
* Space Complexity: O(1)
|
|
112
|
-
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
113
97
|
*/
|
|
114
98
|
/**
|
|
115
99
|
* Time Complexity: O(log n)
|
|
@@ -123,15 +107,14 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
123
107
|
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
124
108
|
* node was not added.
|
|
125
109
|
*/
|
|
126
|
-
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
110
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
|
|
127
111
|
/**
|
|
128
112
|
* Time Complexity: O(k log n)
|
|
129
|
-
* Space Complexity: O(k)
|
|
130
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
113
|
+
* Space Complexity: O(k + log n)
|
|
131
114
|
*/
|
|
132
115
|
/**
|
|
133
116
|
* Time Complexity: O(k log n)
|
|
134
|
-
* Space Complexity: O(k)
|
|
117
|
+
* Space Complexity: O(k + log n)
|
|
135
118
|
*
|
|
136
119
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
137
120
|
* balancing the tree after each addition.
|
|
@@ -147,9 +130,9 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
147
130
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
148
131
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
149
132
|
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
150
|
-
* @returns The function `addMany` returns an array of nodes (`
|
|
133
|
+
* @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
|
|
151
134
|
*/
|
|
152
|
-
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V,
|
|
135
|
+
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
153
136
|
/**
|
|
154
137
|
* Time Complexity: O(log n)
|
|
155
138
|
* Space Complexity: O(1)
|
|
@@ -165,40 +148,39 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
165
148
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
166
149
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
167
150
|
* values:
|
|
168
|
-
* @returns The function `getNodeByKey` returns a node (`
|
|
151
|
+
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
169
152
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
170
153
|
*/
|
|
171
|
-
getNodeByKey(key: K, iterationType?: IterationType):
|
|
154
|
+
getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined;
|
|
172
155
|
/**
|
|
173
156
|
* Time Complexity: O(log n)
|
|
174
|
-
* Space Complexity: O(log n)
|
|
175
|
-
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
157
|
+
* Space Complexity: O(k + log n)
|
|
176
158
|
* /
|
|
177
159
|
|
|
178
160
|
/**
|
|
179
161
|
* Time Complexity: O(log n)
|
|
180
|
-
* Space Complexity: O(log n)
|
|
162
|
+
* Space Complexity: O(k + log n)
|
|
181
163
|
*
|
|
182
164
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
183
165
|
* recursive or iterative approach.
|
|
184
166
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
185
167
|
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
186
168
|
* callback function `C`.
|
|
187
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
169
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
|
|
188
170
|
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
189
|
-
* function type that extends the `BTNCallback<
|
|
171
|
+
* function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
|
|
190
172
|
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
191
173
|
* first node that matches the identifier. If set to true, the function will return an array
|
|
192
174
|
* containing only the first matching node. If set to false (default), the function will continue
|
|
193
175
|
* searching for all nodes that match the identifier and return an array containing
|
|
194
|
-
* @param {K |
|
|
176
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
195
177
|
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
196
178
|
* traversal will start from the root of the tree.
|
|
197
179
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
198
180
|
* performed on the binary tree. It can have two possible values:
|
|
199
|
-
* @returns The method returns an array of nodes (`
|
|
181
|
+
* @returns The method returns an array of nodes (`NODE[]`).
|
|
200
182
|
*/
|
|
201
|
-
getNodes<C extends BTNCallback<
|
|
183
|
+
getNodes<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE[];
|
|
202
184
|
/**
|
|
203
185
|
* Time complexity: O(n)
|
|
204
186
|
* Space complexity: O(n)
|
|
@@ -222,7 +204,7 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
222
204
|
* following values:
|
|
223
205
|
* @returns The method is returning an array of the return type of the callback function.
|
|
224
206
|
*/
|
|
225
|
-
dfs<C extends BTNCallback<
|
|
207
|
+
dfs<C extends BTNCallback<NODE>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): ReturnType<C>[];
|
|
226
208
|
/**
|
|
227
209
|
* Time complexity: O(n)
|
|
228
210
|
* Space complexity: O(n)
|
|
@@ -244,7 +226,7 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
244
226
|
* nodes are visited.
|
|
245
227
|
* @returns The method is returning an array of the return type of the callback function.
|
|
246
228
|
*/
|
|
247
|
-
bfs<C extends BTNCallback<
|
|
229
|
+
bfs<C extends BTNCallback<NODE>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): ReturnType<C>[];
|
|
248
230
|
/**
|
|
249
231
|
* Time complexity: O(n)
|
|
250
232
|
* Space complexity: O(n)
|
|
@@ -256,7 +238,7 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
256
238
|
* The function overrides the listLevels method and returns an array of arrays containing the return
|
|
257
239
|
* type of the callback function for each level of the tree.
|
|
258
240
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
259
|
-
* `BTNCallback<
|
|
241
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
|
|
260
242
|
* during the level listing process.
|
|
261
243
|
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
|
|
262
244
|
* levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
|
|
@@ -267,30 +249,28 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
267
249
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
268
250
|
* function.
|
|
269
251
|
*/
|
|
270
|
-
listLevels<C extends BTNCallback<
|
|
252
|
+
listLevels<C extends BTNCallback<NODE>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): ReturnType<C>[][];
|
|
271
253
|
/**
|
|
272
|
-
* Time Complexity: O(
|
|
273
|
-
* Space Complexity: O(
|
|
274
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
254
|
+
* Time Complexity: O(log n)
|
|
255
|
+
* Space Complexity: O(1)
|
|
275
256
|
*/
|
|
276
257
|
/**
|
|
277
|
-
* Time Complexity: O(
|
|
278
|
-
* Space Complexity: O(
|
|
258
|
+
* Time Complexity: O(log n)
|
|
259
|
+
* Space Complexity: O(1)
|
|
279
260
|
*
|
|
280
261
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
281
262
|
* leftmost node if the comparison result is greater than.
|
|
282
|
-
* @param {K |
|
|
283
|
-
* type `K`, `
|
|
263
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
264
|
+
* type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
|
|
284
265
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
285
266
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
286
267
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
287
268
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
288
269
|
*/
|
|
289
|
-
lastKey(beginRoot?: KeyOrNodeOrEntry<K, V,
|
|
270
|
+
lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, NODE>): K | undefined;
|
|
290
271
|
/**
|
|
291
272
|
* Time Complexity: O(log n)
|
|
292
273
|
* Space Complexity: O(log n)
|
|
293
|
-
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
294
274
|
*/
|
|
295
275
|
/**
|
|
296
276
|
* Time Complexity: O(log n)
|
|
@@ -300,12 +280,12 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
300
280
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
301
281
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
302
282
|
* that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
|
|
303
|
-
* parameter of type `
|
|
283
|
+
* parameter of type `NODE` (the node type) and returns a value of any type.
|
|
304
284
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
305
285
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
306
286
|
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
307
287
|
* `lesserOrGreater` are
|
|
308
|
-
* @param {K |
|
|
288
|
+
* @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
309
289
|
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
310
290
|
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
311
291
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -313,7 +293,7 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
313
293
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
314
294
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
315
295
|
*/
|
|
316
|
-
lesserOrGreaterTraverse<C extends BTNCallback<
|
|
296
|
+
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(callback?: C, lesserOrGreater?: CP, targetNode?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): ReturnType<C>[];
|
|
317
297
|
/**
|
|
318
298
|
* Time Complexity: O(log n)
|
|
319
299
|
* Space Complexity: O(log n)
|
|
@@ -340,8 +320,8 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
340
320
|
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
341
321
|
*/
|
|
342
322
|
/**
|
|
343
|
-
* Time Complexity: O(n)
|
|
344
|
-
* Space Complexity: O(n)
|
|
323
|
+
* Time Complexity: O(n)
|
|
324
|
+
* Space Complexity: O(log n)
|
|
345
325
|
*/
|
|
346
326
|
/**
|
|
347
327
|
* Time Complexity: O(n)
|
|
@@ -353,7 +333,7 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
353
333
|
* @returns a boolean value.
|
|
354
334
|
*/
|
|
355
335
|
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
356
|
-
protected _setRoot(v:
|
|
336
|
+
protected _setRoot(v: NODE | undefined): void;
|
|
357
337
|
/**
|
|
358
338
|
* The function compares two values using a comparator function and returns whether the first value
|
|
359
339
|
* is greater than, less than, or equal to the second value.
|
|
@@ -10,16 +10,9 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
10
10
|
this._right = undefined;
|
|
11
11
|
}
|
|
12
12
|
_left;
|
|
13
|
-
/**
|
|
14
|
-
* Get the left child node.
|
|
15
|
-
*/
|
|
16
13
|
get left() {
|
|
17
14
|
return this._left;
|
|
18
15
|
}
|
|
19
|
-
/**
|
|
20
|
-
* Set the left child node.
|
|
21
|
-
* @param {N | undefined} v - The left child node.
|
|
22
|
-
*/
|
|
23
16
|
set left(v) {
|
|
24
17
|
if (v) {
|
|
25
18
|
v.parent = this;
|
|
@@ -27,16 +20,9 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
27
20
|
this._left = v;
|
|
28
21
|
}
|
|
29
22
|
_right;
|
|
30
|
-
/**
|
|
31
|
-
* Get the right child node.
|
|
32
|
-
*/
|
|
33
23
|
get right() {
|
|
34
24
|
return this._right;
|
|
35
25
|
}
|
|
36
|
-
/**
|
|
37
|
-
* Set the right child node.
|
|
38
|
-
* @param {N | undefined} v - The right child node.
|
|
39
|
-
*/
|
|
40
26
|
set right(v) {
|
|
41
27
|
if (v) {
|
|
42
28
|
v.parent = this;
|
|
@@ -107,14 +93,14 @@ export class BST extends BinaryTree {
|
|
|
107
93
|
});
|
|
108
94
|
}
|
|
109
95
|
/**
|
|
110
|
-
* The function `
|
|
96
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
111
97
|
* otherwise it returns undefined.
|
|
112
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
98
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
113
99
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
114
|
-
* `
|
|
115
|
-
* @returns a node of type
|
|
100
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
101
|
+
* @returns a node of type NODE or undefined.
|
|
116
102
|
*/
|
|
117
|
-
|
|
103
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
118
104
|
let node;
|
|
119
105
|
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
120
106
|
return;
|
|
@@ -142,7 +128,6 @@ export class BST extends BinaryTree {
|
|
|
142
128
|
/**
|
|
143
129
|
* Time Complexity: O(log n)
|
|
144
130
|
* Space Complexity: O(log n)
|
|
145
|
-
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
146
131
|
*/
|
|
147
132
|
/**
|
|
148
133
|
* Time Complexity: O(log n)
|
|
@@ -150,11 +135,11 @@ export class BST extends BinaryTree {
|
|
|
150
135
|
*
|
|
151
136
|
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
152
137
|
* otherwise it returns the key itself.
|
|
153
|
-
* @param {K |
|
|
138
|
+
* @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
|
|
154
139
|
* `undefined`.
|
|
155
140
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
156
141
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
157
|
-
* @returns either a node object (
|
|
142
|
+
* @returns either a node object (NODE) or undefined.
|
|
158
143
|
*/
|
|
159
144
|
ensureNode(keyOrNodeOrEntry, iterationType = IterationType.ITERATIVE) {
|
|
160
145
|
let res;
|
|
@@ -173,7 +158,7 @@ export class BST extends BinaryTree {
|
|
|
173
158
|
}
|
|
174
159
|
/**
|
|
175
160
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
176
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,
|
|
161
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
177
162
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
178
163
|
*/
|
|
179
164
|
isNode(keyOrNodeOrEntry) {
|
|
@@ -182,7 +167,6 @@ export class BST extends BinaryTree {
|
|
|
182
167
|
/**
|
|
183
168
|
* Time Complexity: O(log n)
|
|
184
169
|
* Space Complexity: O(1)
|
|
185
|
-
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
186
170
|
*/
|
|
187
171
|
/**
|
|
188
172
|
* Time Complexity: O(log n)
|
|
@@ -197,7 +181,7 @@ export class BST extends BinaryTree {
|
|
|
197
181
|
* node was not added.
|
|
198
182
|
*/
|
|
199
183
|
add(keyOrNodeOrEntry, value) {
|
|
200
|
-
const newNode = this.
|
|
184
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
201
185
|
if (newNode === undefined)
|
|
202
186
|
return false;
|
|
203
187
|
if (this.root === undefined) {
|
|
@@ -221,7 +205,6 @@ export class BST extends BinaryTree {
|
|
|
221
205
|
else if (this._compare(current.key, newNode.key) === CP.gt) {
|
|
222
206
|
if (current.left === undefined) {
|
|
223
207
|
current.left = newNode;
|
|
224
|
-
newNode.parent = current;
|
|
225
208
|
this._size++;
|
|
226
209
|
return true;
|
|
227
210
|
}
|
|
@@ -230,7 +213,6 @@ export class BST extends BinaryTree {
|
|
|
230
213
|
else {
|
|
231
214
|
if (current.right === undefined) {
|
|
232
215
|
current.right = newNode;
|
|
233
|
-
newNode.parent = current;
|
|
234
216
|
this._size++;
|
|
235
217
|
return true;
|
|
236
218
|
}
|
|
@@ -241,12 +223,11 @@ export class BST extends BinaryTree {
|
|
|
241
223
|
}
|
|
242
224
|
/**
|
|
243
225
|
* Time Complexity: O(k log n)
|
|
244
|
-
* Space Complexity: O(k)
|
|
245
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
226
|
+
* Space Complexity: O(k + log n)
|
|
246
227
|
*/
|
|
247
228
|
/**
|
|
248
229
|
* Time Complexity: O(k log n)
|
|
249
|
-
* Space Complexity: O(k)
|
|
230
|
+
* Space Complexity: O(k + log n)
|
|
250
231
|
*
|
|
251
232
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
252
233
|
* balancing the tree after each addition.
|
|
@@ -262,7 +243,7 @@ export class BST extends BinaryTree {
|
|
|
262
243
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
263
244
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
264
245
|
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
265
|
-
* @returns The function `addMany` returns an array of nodes (`
|
|
246
|
+
* @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
|
|
266
247
|
*/
|
|
267
248
|
addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
268
249
|
const inserted = [];
|
|
@@ -353,7 +334,7 @@ export class BST extends BinaryTree {
|
|
|
353
334
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
354
335
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
355
336
|
* values:
|
|
356
|
-
* @returns The function `getNodeByKey` returns a node (`
|
|
337
|
+
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
357
338
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
358
339
|
*/
|
|
359
340
|
getNodeByKey(key, iterationType = IterationType.ITERATIVE) {
|
|
@@ -389,32 +370,31 @@ export class BST extends BinaryTree {
|
|
|
389
370
|
}
|
|
390
371
|
/**
|
|
391
372
|
* Time Complexity: O(log n)
|
|
392
|
-
* Space Complexity: O(log n)
|
|
393
|
-
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
373
|
+
* Space Complexity: O(k + log n)
|
|
394
374
|
* /
|
|
395
375
|
|
|
396
376
|
/**
|
|
397
377
|
* Time Complexity: O(log n)
|
|
398
|
-
* Space Complexity: O(log n)
|
|
378
|
+
* Space Complexity: O(k + log n)
|
|
399
379
|
*
|
|
400
380
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
401
381
|
* recursive or iterative approach.
|
|
402
382
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
403
383
|
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
404
384
|
* callback function `C`.
|
|
405
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
385
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
|
|
406
386
|
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
407
|
-
* function type that extends the `BTNCallback<
|
|
387
|
+
* function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
|
|
408
388
|
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
409
389
|
* first node that matches the identifier. If set to true, the function will return an array
|
|
410
390
|
* containing only the first matching node. If set to false (default), the function will continue
|
|
411
391
|
* searching for all nodes that match the identifier and return an array containing
|
|
412
|
-
* @param {K |
|
|
392
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
413
393
|
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
414
394
|
* traversal will start from the root of the tree.
|
|
415
395
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
416
396
|
* performed on the binary tree. It can have two possible values:
|
|
417
|
-
* @returns The method returns an array of nodes (`
|
|
397
|
+
* @returns The method returns an array of nodes (`NODE[]`).
|
|
418
398
|
*/
|
|
419
399
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
420
400
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -472,25 +452,6 @@ export class BST extends BinaryTree {
|
|
|
472
452
|
}
|
|
473
453
|
return ans;
|
|
474
454
|
}
|
|
475
|
-
// /**
|
|
476
|
-
// * The function overrides the subTreeTraverse method and returns the result of calling the super
|
|
477
|
-
// * method with the provided arguments.
|
|
478
|
-
// * @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
479
|
-
// * the subtree traversal. It should accept a single parameter of type `N`, which represents a node in
|
|
480
|
-
// * the tree. The return type of the callback function can be any type.
|
|
481
|
-
// * @param beginRoot - The `beginRoot` parameter is the starting point for traversing the subtree. It
|
|
482
|
-
// * can be either a key, a node, or an entry.
|
|
483
|
-
// * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
484
|
-
// * be performed during the traversal of the subtree. It can have one of the following values:
|
|
485
|
-
// * @returns The method is returning an array of the return type of the callback function.
|
|
486
|
-
// */
|
|
487
|
-
// override subTreeTraverse<C extends BTNCallback<N>>(
|
|
488
|
-
// callback: C = this._defaultOneParamCallback as C,
|
|
489
|
-
// beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
490
|
-
// iterationType = this.iterationType
|
|
491
|
-
// ): ReturnType<C>[] {
|
|
492
|
-
// return super.subTreeTraverse(callback, beginRoot, iterationType, false);
|
|
493
|
-
// }
|
|
494
455
|
/**
|
|
495
456
|
* Time complexity: O(n)
|
|
496
457
|
* Space complexity: O(n)
|
|
@@ -552,7 +513,7 @@ export class BST extends BinaryTree {
|
|
|
552
513
|
* The function overrides the listLevels method and returns an array of arrays containing the return
|
|
553
514
|
* type of the callback function for each level of the tree.
|
|
554
515
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
555
|
-
* `BTNCallback<
|
|
516
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
|
|
556
517
|
* during the level listing process.
|
|
557
518
|
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
|
|
558
519
|
* levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
|
|
@@ -567,18 +528,17 @@ export class BST extends BinaryTree {
|
|
|
567
528
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
568
529
|
}
|
|
569
530
|
/**
|
|
570
|
-
* Time Complexity: O(
|
|
571
|
-
* Space Complexity: O(
|
|
572
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
531
|
+
* Time Complexity: O(log n)
|
|
532
|
+
* Space Complexity: O(1)
|
|
573
533
|
*/
|
|
574
534
|
/**
|
|
575
|
-
* Time Complexity: O(
|
|
576
|
-
* Space Complexity: O(
|
|
535
|
+
* Time Complexity: O(log n)
|
|
536
|
+
* Space Complexity: O(1)
|
|
577
537
|
*
|
|
578
538
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
579
539
|
* leftmost node if the comparison result is greater than.
|
|
580
|
-
* @param {K |
|
|
581
|
-
* type `K`, `
|
|
540
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
541
|
+
* type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
|
|
582
542
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
583
543
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
584
544
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
@@ -605,7 +565,6 @@ export class BST extends BinaryTree {
|
|
|
605
565
|
/**
|
|
606
566
|
* Time Complexity: O(log n)
|
|
607
567
|
* Space Complexity: O(log n)
|
|
608
|
-
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
609
568
|
*/
|
|
610
569
|
/**
|
|
611
570
|
* Time Complexity: O(log n)
|
|
@@ -615,12 +574,12 @@ export class BST extends BinaryTree {
|
|
|
615
574
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
616
575
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
617
576
|
* that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
|
|
618
|
-
* parameter of type `
|
|
577
|
+
* parameter of type `NODE` (the node type) and returns a value of any type.
|
|
619
578
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
620
579
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
621
580
|
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
622
581
|
* `lesserOrGreater` are
|
|
623
|
-
* @param {K |
|
|
582
|
+
* @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
624
583
|
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
625
584
|
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
626
585
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -729,8 +688,8 @@ export class BST extends BinaryTree {
|
|
|
729
688
|
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
730
689
|
*/
|
|
731
690
|
/**
|
|
732
|
-
* Time Complexity: O(n)
|
|
733
|
-
* Space Complexity: O(n)
|
|
691
|
+
* Time Complexity: O(n)
|
|
692
|
+
* Space Complexity: O(log n)
|
|
734
693
|
*/
|
|
735
694
|
/**
|
|
736
695
|
* Time Complexity: O(n)
|