data-structure-typed 1.50.1 → 1.50.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -1
- package/README.md +30 -28
- package/README_zh-CN.md +1 -1
- package/SPECIFICATION.md +56 -0
- package/SPONSOR-zh-CN.md +90 -0
- package/SPONSOR.md +54 -0
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +149 -161
- package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -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 +72 -47
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-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 +92 -79
- package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -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 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +131 -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 +76 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/cjs/data-structures/hash/hash-map.js +347 -78
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/cjs/data-structures/heap/heap.js +95 -26
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
- 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 +154 -106
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
- 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/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- 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 +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- 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 +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/cjs/data-structures/queue/deque.js +200 -62
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/cjs/data-structures/queue/queue.js +111 -49
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/cjs/data-structures/stack/stack.js +58 -22
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/cjs/data-structures/tree/tree.js +77 -11
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/cjs/data-structures/trie/trie.js +153 -33
- 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 +120 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- 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 +92 -79
- package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +131 -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 +76 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/mjs/data-structures/hash/hash-map.js +353 -84
- package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/mjs/data-structures/heap/heap.js +95 -26
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
- 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/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/mjs/data-structures/queue/deque.js +205 -67
- package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/mjs/data-structures/queue/queue.js +111 -49
- package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/mjs/data-structures/stack/stack.js +58 -22
- package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/mjs/data-structures/tree/tree.js +80 -14
- package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/mjs/data-structures/trie/trie.js +156 -36
- 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 +3121 -1583
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +61 -55
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- 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/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- 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 +4 -4
- 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 +11 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- 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 +24 -5
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- package/test/unit/data-structures/trie/trie.test.ts +17 -0
|
@@ -11,14 +11,16 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
11
11
|
}
|
|
12
12
|
_left;
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
14
|
+
* The function returns the value of the `_left` property.
|
|
15
|
+
* @returns The `_left` property of the current object is being returned.
|
|
15
16
|
*/
|
|
16
17
|
get left() {
|
|
17
18
|
return this._left;
|
|
18
19
|
}
|
|
19
20
|
/**
|
|
20
|
-
*
|
|
21
|
-
* @param {
|
|
21
|
+
* The function sets the left child of a node and updates the parent reference of the child.
|
|
22
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an
|
|
23
|
+
* instance of the `NODE` class or `undefined`.
|
|
22
24
|
*/
|
|
23
25
|
set left(v) {
|
|
24
26
|
if (v) {
|
|
@@ -28,14 +30,17 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
28
30
|
}
|
|
29
31
|
_right;
|
|
30
32
|
/**
|
|
31
|
-
*
|
|
33
|
+
* The function returns the right node of a binary tree or undefined if there is no right node.
|
|
34
|
+
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
|
|
35
|
+
* `undefined`.
|
|
32
36
|
*/
|
|
33
37
|
get right() {
|
|
34
38
|
return this._right;
|
|
35
39
|
}
|
|
36
40
|
/**
|
|
37
|
-
*
|
|
38
|
-
* @param {
|
|
41
|
+
* The function sets the right child of a node and updates the parent reference of the child.
|
|
42
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a
|
|
43
|
+
* `NODE` object or `undefined`.
|
|
39
44
|
*/
|
|
40
45
|
set right(v) {
|
|
41
46
|
if (v) {
|
|
@@ -55,10 +60,10 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
55
60
|
*/
|
|
56
61
|
export class BST extends BinaryTree {
|
|
57
62
|
/**
|
|
58
|
-
* This is the constructor function for a
|
|
59
|
-
*
|
|
60
|
-
* @param
|
|
61
|
-
* binary search tree.
|
|
63
|
+
* This is the constructor function for a TypeScript class that initializes a binary search tree with
|
|
64
|
+
* optional keys or nodes or entries and options.
|
|
65
|
+
* @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used
|
|
66
|
+
* to initialize the binary search tree with the provided keys, nodes, or entries.
|
|
62
67
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
63
68
|
* configuration options for the binary search tree. It can have the following properties:
|
|
64
69
|
*/
|
|
@@ -74,20 +79,28 @@ export class BST extends BinaryTree {
|
|
|
74
79
|
this.addMany(keysOrNodesOrEntries);
|
|
75
80
|
}
|
|
76
81
|
_root;
|
|
82
|
+
/**
|
|
83
|
+
* The function returns the root node of a tree structure.
|
|
84
|
+
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
85
|
+
*/
|
|
77
86
|
get root() {
|
|
78
87
|
return this._root;
|
|
79
88
|
}
|
|
80
89
|
_variant = BSTVariant.STANDARD;
|
|
90
|
+
/**
|
|
91
|
+
* The function returns the value of the _variant property.
|
|
92
|
+
* @returns The value of the `_variant` property.
|
|
93
|
+
*/
|
|
81
94
|
get variant() {
|
|
82
95
|
return this._variant;
|
|
83
96
|
}
|
|
84
97
|
/**
|
|
85
|
-
* The function creates a new
|
|
86
|
-
* @param {K} key - The key parameter is the
|
|
87
|
-
*
|
|
88
|
-
* @param [value] - The parameter
|
|
89
|
-
*
|
|
90
|
-
* @returns a new instance of the BSTNode class
|
|
98
|
+
* The function creates a new BSTNode with the given key and value and returns it.
|
|
99
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
100
|
+
* being created.
|
|
101
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
102
|
+
* value associated with the key in the node being created.
|
|
103
|
+
* @returns The method is returning a new instance of the BSTNode class, casted as the NODE type.
|
|
91
104
|
*/
|
|
92
105
|
createNode(key, value) {
|
|
93
106
|
return new BSTNode(key, value);
|
|
@@ -95,9 +108,10 @@ export class BST extends BinaryTree {
|
|
|
95
108
|
/**
|
|
96
109
|
* The function creates a new binary search tree with the specified options.
|
|
97
110
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
98
|
-
* behavior of the `createTree` method. It
|
|
99
|
-
*
|
|
100
|
-
* @returns a new instance of the BST class with the
|
|
111
|
+
* behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a
|
|
112
|
+
* partial object of type `BSTOptions<K>`.
|
|
113
|
+
* @returns a new instance of the BST class, with the provided options merged with the default
|
|
114
|
+
* options. The returned value is casted as TREE.
|
|
101
115
|
*/
|
|
102
116
|
createTree(options) {
|
|
103
117
|
return new BST([], {
|
|
@@ -107,14 +121,14 @@ export class BST extends BinaryTree {
|
|
|
107
121
|
});
|
|
108
122
|
}
|
|
109
123
|
/**
|
|
110
|
-
* The function `
|
|
124
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
111
125
|
* otherwise it returns undefined.
|
|
112
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
126
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
113
127
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
114
|
-
* `
|
|
115
|
-
* @returns a node of type
|
|
128
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
129
|
+
* @returns a node of type NODE or undefined.
|
|
116
130
|
*/
|
|
117
|
-
|
|
131
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
118
132
|
let node;
|
|
119
133
|
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
120
134
|
return;
|
|
@@ -142,7 +156,6 @@ export class BST extends BinaryTree {
|
|
|
142
156
|
/**
|
|
143
157
|
* Time Complexity: O(log n)
|
|
144
158
|
* Space Complexity: O(log n)
|
|
145
|
-
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
146
159
|
*/
|
|
147
160
|
/**
|
|
148
161
|
* Time Complexity: O(log n)
|
|
@@ -150,11 +163,11 @@ export class BST extends BinaryTree {
|
|
|
150
163
|
*
|
|
151
164
|
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
152
165
|
* otherwise it returns the key itself.
|
|
153
|
-
* @param {K |
|
|
166
|
+
* @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
|
|
154
167
|
* `undefined`.
|
|
155
168
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
156
169
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
157
|
-
* @returns either a node object (
|
|
170
|
+
* @returns either a node object (NODE) or undefined.
|
|
158
171
|
*/
|
|
159
172
|
ensureNode(keyOrNodeOrEntry, iterationType = IterationType.ITERATIVE) {
|
|
160
173
|
let res;
|
|
@@ -173,7 +186,7 @@ export class BST extends BinaryTree {
|
|
|
173
186
|
}
|
|
174
187
|
/**
|
|
175
188
|
* 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,
|
|
189
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
177
190
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
178
191
|
*/
|
|
179
192
|
isNode(keyOrNodeOrEntry) {
|
|
@@ -182,7 +195,6 @@ export class BST extends BinaryTree {
|
|
|
182
195
|
/**
|
|
183
196
|
* Time Complexity: O(log n)
|
|
184
197
|
* Space Complexity: O(1)
|
|
185
|
-
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
186
198
|
*/
|
|
187
199
|
/**
|
|
188
200
|
* Time Complexity: O(log n)
|
|
@@ -197,7 +209,7 @@ export class BST extends BinaryTree {
|
|
|
197
209
|
* node was not added.
|
|
198
210
|
*/
|
|
199
211
|
add(keyOrNodeOrEntry, value) {
|
|
200
|
-
const newNode = this.
|
|
212
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
201
213
|
if (newNode === undefined)
|
|
202
214
|
return false;
|
|
203
215
|
if (this.root === undefined) {
|
|
@@ -221,7 +233,6 @@ export class BST extends BinaryTree {
|
|
|
221
233
|
else if (this._compare(current.key, newNode.key) === CP.gt) {
|
|
222
234
|
if (current.left === undefined) {
|
|
223
235
|
current.left = newNode;
|
|
224
|
-
newNode.parent = current;
|
|
225
236
|
this._size++;
|
|
226
237
|
return true;
|
|
227
238
|
}
|
|
@@ -230,7 +241,6 @@ export class BST extends BinaryTree {
|
|
|
230
241
|
else {
|
|
231
242
|
if (current.right === undefined) {
|
|
232
243
|
current.right = newNode;
|
|
233
|
-
newNode.parent = current;
|
|
234
244
|
this._size++;
|
|
235
245
|
return true;
|
|
236
246
|
}
|
|
@@ -241,12 +251,11 @@ export class BST extends BinaryTree {
|
|
|
241
251
|
}
|
|
242
252
|
/**
|
|
243
253
|
* 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.
|
|
254
|
+
* Space Complexity: O(k + log n)
|
|
246
255
|
*/
|
|
247
256
|
/**
|
|
248
257
|
* Time Complexity: O(k log n)
|
|
249
|
-
* Space Complexity: O(k)
|
|
258
|
+
* Space Complexity: O(k + log n)
|
|
250
259
|
*
|
|
251
260
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
252
261
|
* balancing the tree after each addition.
|
|
@@ -262,7 +271,7 @@ export class BST extends BinaryTree {
|
|
|
262
271
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
263
272
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
264
273
|
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
265
|
-
* @returns The function `addMany` returns an array of nodes (`
|
|
274
|
+
* @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
|
|
266
275
|
*/
|
|
267
276
|
addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
268
277
|
const inserted = [];
|
|
@@ -353,7 +362,7 @@ export class BST extends BinaryTree {
|
|
|
353
362
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
354
363
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
355
364
|
* values:
|
|
356
|
-
* @returns The function `getNodeByKey` returns a node (`
|
|
365
|
+
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
357
366
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
358
367
|
*/
|
|
359
368
|
getNodeByKey(key, iterationType = IterationType.ITERATIVE) {
|
|
@@ -389,32 +398,31 @@ export class BST extends BinaryTree {
|
|
|
389
398
|
}
|
|
390
399
|
/**
|
|
391
400
|
* 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.
|
|
401
|
+
* Space Complexity: O(k + log n)
|
|
394
402
|
* /
|
|
395
403
|
|
|
396
404
|
/**
|
|
397
405
|
* Time Complexity: O(log n)
|
|
398
|
-
* Space Complexity: O(log n)
|
|
406
|
+
* Space Complexity: O(k + log n)
|
|
399
407
|
*
|
|
400
408
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
401
409
|
* recursive or iterative approach.
|
|
402
410
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
403
411
|
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
404
412
|
* callback function `C`.
|
|
405
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
413
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
|
|
406
414
|
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
407
|
-
* function type that extends the `BTNCallback<
|
|
415
|
+
* function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
|
|
408
416
|
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
409
417
|
* first node that matches the identifier. If set to true, the function will return an array
|
|
410
418
|
* containing only the first matching node. If set to false (default), the function will continue
|
|
411
419
|
* searching for all nodes that match the identifier and return an array containing
|
|
412
|
-
* @param {K |
|
|
420
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
413
421
|
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
414
422
|
* traversal will start from the root of the tree.
|
|
415
423
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
416
424
|
* performed on the binary tree. It can have two possible values:
|
|
417
|
-
* @returns The method returns an array of nodes (`
|
|
425
|
+
* @returns The method returns an array of nodes (`NODE[]`).
|
|
418
426
|
*/
|
|
419
427
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
420
428
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -472,25 +480,6 @@ export class BST extends BinaryTree {
|
|
|
472
480
|
}
|
|
473
481
|
return ans;
|
|
474
482
|
}
|
|
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
483
|
/**
|
|
495
484
|
* Time complexity: O(n)
|
|
496
485
|
* Space complexity: O(n)
|
|
@@ -552,7 +541,7 @@ export class BST extends BinaryTree {
|
|
|
552
541
|
* The function overrides the listLevels method and returns an array of arrays containing the return
|
|
553
542
|
* type of the callback function for each level of the tree.
|
|
554
543
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
555
|
-
* `BTNCallback<
|
|
544
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
|
|
556
545
|
* during the level listing process.
|
|
557
546
|
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
|
|
558
547
|
* levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
|
|
@@ -567,18 +556,17 @@ export class BST extends BinaryTree {
|
|
|
567
556
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
568
557
|
}
|
|
569
558
|
/**
|
|
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.
|
|
559
|
+
* Time Complexity: O(log n)
|
|
560
|
+
* Space Complexity: O(1)
|
|
573
561
|
*/
|
|
574
562
|
/**
|
|
575
|
-
* Time Complexity: O(
|
|
576
|
-
* Space Complexity: O(
|
|
563
|
+
* Time Complexity: O(log n)
|
|
564
|
+
* Space Complexity: O(1)
|
|
577
565
|
*
|
|
578
566
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
579
567
|
* leftmost node if the comparison result is greater than.
|
|
580
|
-
* @param {K |
|
|
581
|
-
* type `K`, `
|
|
568
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
569
|
+
* type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
|
|
582
570
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
583
571
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
584
572
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
@@ -605,7 +593,6 @@ export class BST extends BinaryTree {
|
|
|
605
593
|
/**
|
|
606
594
|
* Time Complexity: O(log n)
|
|
607
595
|
* 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
596
|
*/
|
|
610
597
|
/**
|
|
611
598
|
* Time Complexity: O(log n)
|
|
@@ -615,12 +602,12 @@ export class BST extends BinaryTree {
|
|
|
615
602
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
616
603
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
617
604
|
* that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
|
|
618
|
-
* parameter of type `
|
|
605
|
+
* parameter of type `NODE` (the node type) and returns a value of any type.
|
|
619
606
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
620
607
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
621
608
|
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
622
609
|
* `lesserOrGreater` are
|
|
623
|
-
* @param {K |
|
|
610
|
+
* @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
624
611
|
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
625
612
|
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
626
613
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -729,8 +716,8 @@ export class BST extends BinaryTree {
|
|
|
729
716
|
* 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
717
|
*/
|
|
731
718
|
/**
|
|
732
|
-
* Time Complexity: O(n)
|
|
733
|
-
* Space Complexity: O(n)
|
|
719
|
+
* Time Complexity: O(n)
|
|
720
|
+
* Space Complexity: O(log n)
|
|
734
721
|
*/
|
|
735
722
|
/**
|
|
736
723
|
* Time Complexity: O(n)
|
|
@@ -786,6 +773,11 @@ export class BST extends BinaryTree {
|
|
|
786
773
|
}
|
|
787
774
|
return balanced;
|
|
788
775
|
}
|
|
776
|
+
/**
|
|
777
|
+
* The function sets the root property of an object and updates the parent property of the new root.
|
|
778
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
|
|
779
|
+
* can either be an object of type `NODE` or it can be `undefined`.
|
|
780
|
+
*/
|
|
789
781
|
_setRoot(v) {
|
|
790
782
|
if (v) {
|
|
791
783
|
v.parent = undefined;
|