red-black-tree-typed 1.53.7 → 1.54.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/LICENSE +2 -2
- package/README.md +52 -0
- package/dist/common/index.js +5 -0
- package/dist/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -328
- package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
- package/dist/data-structures/binary-tree/avl-tree.js +130 -70
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +268 -202
- package/dist/data-structures/binary-tree/binary-tree.js +311 -263
- package/dist/data-structures/binary-tree/bst.d.ts +193 -139
- package/dist/data-structures/binary-tree/bst.js +248 -164
- package/dist/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/data-structures/binary-tree/index.js +3 -1
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +286 -0
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +176 -107
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
- package/dist/data-structures/binary-tree/tree-multi-map.js +145 -367
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/hash/hash-map.d.ts +31 -1
- package/dist/data-structures/hash/hash-map.js +35 -5
- package/dist/data-structures/heap/heap.d.ts +20 -3
- package/dist/data-structures/heap/heap.js +31 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
- package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +47 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +73 -26
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +8 -7
- package/dist/data-structures/trie/trie.js +8 -7
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/interfaces/binary-tree.d.ts +8 -8
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/types/data-structures/binary-tree/index.js +3 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
- package/package.json +3 -3
- package/src/common/index.ts +7 -1
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +151 -370
- package/src/data-structures/binary-tree/avl-tree.ts +162 -105
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +488 -416
- package/src/data-structures/binary-tree/bst.ts +326 -251
- package/src/data-structures/binary-tree/index.ts +3 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +219 -145
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/hash/hash-map.ts +37 -7
- package/src/data-structures/heap/heap.ts +33 -10
- package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
- package/src/data-structures/linked-list/singly-linked-list.ts +80 -27
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +8 -7
- package/src/index.ts +4 -4
- package/src/interfaces/binary-tree.ts +10 -21
- package/src/types/data-structures/base/base.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/index.ts +3 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -209
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
- /package/dist/types/data-structures/binary-tree/{rb-tree.js → avl-tree-counter.js} +0 -0
|
@@ -15,10 +15,24 @@ const common_1 = require("../../common");
|
|
|
15
15
|
/**
|
|
16
16
|
* Represents a node in a binary tree.
|
|
17
17
|
* @template V - The type of data stored in the node.
|
|
18
|
-
* @template
|
|
18
|
+
* @template BinaryTreeNode<K, V> - The type of the family relationship in the binary tree.
|
|
19
19
|
*/
|
|
20
20
|
class BinaryTreeNode {
|
|
21
|
+
/**
|
|
22
|
+
* The constructor function initializes an object with a key and an optional value in TypeScript.
|
|
23
|
+
* @param {K} key - The `key` parameter in the constructor function is used to store the key value
|
|
24
|
+
* for the key-value pair.
|
|
25
|
+
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
|
|
26
|
+
* have to be provided when creating an instance of the class. If a `value` is not provided, it will
|
|
27
|
+
* default to `undefined`.
|
|
28
|
+
*/
|
|
21
29
|
constructor(key, value) {
|
|
30
|
+
this.parent = undefined;
|
|
31
|
+
this._left = undefined;
|
|
32
|
+
this._right = undefined;
|
|
33
|
+
this._height = 0;
|
|
34
|
+
this._color = 'BLACK';
|
|
35
|
+
this._count = 1;
|
|
22
36
|
this.key = key;
|
|
23
37
|
this.value = value;
|
|
24
38
|
}
|
|
@@ -40,15 +54,32 @@ class BinaryTreeNode {
|
|
|
40
54
|
}
|
|
41
55
|
this._right = v;
|
|
42
56
|
}
|
|
57
|
+
get height() {
|
|
58
|
+
return this._height;
|
|
59
|
+
}
|
|
60
|
+
set height(value) {
|
|
61
|
+
this._height = value;
|
|
62
|
+
}
|
|
63
|
+
get color() {
|
|
64
|
+
return this._color;
|
|
65
|
+
}
|
|
66
|
+
set color(value) {
|
|
67
|
+
this._color = value;
|
|
68
|
+
}
|
|
69
|
+
get count() {
|
|
70
|
+
return this._count;
|
|
71
|
+
}
|
|
72
|
+
set count(value) {
|
|
73
|
+
this._count = value;
|
|
74
|
+
}
|
|
43
75
|
get familyPosition() {
|
|
44
|
-
const that = this;
|
|
45
76
|
if (!this.parent) {
|
|
46
77
|
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
47
78
|
}
|
|
48
|
-
if (this.parent.left ===
|
|
79
|
+
if (this.parent.left === this) {
|
|
49
80
|
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
50
81
|
}
|
|
51
|
-
else if (this.parent.right ===
|
|
82
|
+
else if (this.parent.right === this) {
|
|
52
83
|
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
53
84
|
}
|
|
54
85
|
return 'MAL_NODE';
|
|
@@ -64,13 +95,13 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
64
95
|
*/
|
|
65
96
|
class BinaryTree extends base_1.IterableEntryBase {
|
|
66
97
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* @param [options] - The `options` parameter in the constructor is an object that can
|
|
73
|
-
* following properties:
|
|
98
|
+
* This TypeScript constructor function initializes a binary tree with optional options and adds
|
|
99
|
+
* elements based on the provided input.
|
|
100
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
101
|
+
* iterable that can contain either objects of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It
|
|
102
|
+
* is used to initialize the binary tree with keys, nodes, entries, or raw data.
|
|
103
|
+
* @param [options] - The `options` parameter in the constructor is an optional object that can
|
|
104
|
+
* contain the following properties:
|
|
74
105
|
*/
|
|
75
106
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
76
107
|
super();
|
|
@@ -113,18 +144,24 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
113
144
|
return this._toEntryFn;
|
|
114
145
|
}
|
|
115
146
|
/**
|
|
147
|
+
* Time Complexity: O(1)
|
|
148
|
+
* Space Complexity: O(1)
|
|
149
|
+
*
|
|
116
150
|
* The function creates a new binary tree node with a specified key and optional value.
|
|
117
151
|
* @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
|
|
118
152
|
* @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
|
|
119
153
|
* not required to be provided when calling the function. If a `value` is provided, it should be of
|
|
120
154
|
* type `V`, which is the type of the value associated with the node.
|
|
121
155
|
* @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted
|
|
122
|
-
* as
|
|
156
|
+
* as BinaryTreeNode<K, V>.
|
|
123
157
|
*/
|
|
124
158
|
createNode(key, value) {
|
|
125
159
|
return new BinaryTreeNode(key, this._isMapMode ? undefined : value);
|
|
126
160
|
}
|
|
127
161
|
/**
|
|
162
|
+
* Time Complexity: O(1)
|
|
163
|
+
* Space Complexity: O(1)
|
|
164
|
+
*
|
|
128
165
|
* The function creates a binary tree with the specified options.
|
|
129
166
|
* @param [options] - The `options` parameter in the `createTree` function is an optional parameter
|
|
130
167
|
* that allows you to provide partial configuration options for creating a binary tree. It is of type
|
|
@@ -135,55 +172,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
135
172
|
createTree(options) {
|
|
136
173
|
return new BinaryTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, toEntryFn: this._toEntryFn }, options));
|
|
137
174
|
}
|
|
138
|
-
/**
|
|
139
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
140
|
-
* or returns null.
|
|
141
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
142
|
-
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
143
|
-
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
144
|
-
* node, an entry
|
|
145
|
-
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
146
|
-
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
147
|
-
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
148
|
-
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
149
|
-
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
150
|
-
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
151
|
-
* value.
|
|
152
|
-
*/
|
|
153
|
-
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
154
|
-
if (keyNodeEntryOrRaw === undefined)
|
|
155
|
-
return [undefined, undefined];
|
|
156
|
-
if (keyNodeEntryOrRaw === null)
|
|
157
|
-
return [null, undefined];
|
|
158
|
-
if (this.isNode(keyNodeEntryOrRaw))
|
|
159
|
-
return [keyNodeEntryOrRaw, value];
|
|
160
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
161
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
162
|
-
if (key === undefined)
|
|
163
|
-
return [undefined, undefined];
|
|
164
|
-
else if (key === null)
|
|
165
|
-
return [null, undefined];
|
|
166
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
167
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
168
|
-
}
|
|
169
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
170
|
-
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
171
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
172
|
-
if (this.isKey(key))
|
|
173
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
174
|
-
}
|
|
175
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
176
|
-
return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
177
|
-
return [undefined, undefined];
|
|
178
|
-
}
|
|
179
175
|
/**
|
|
180
176
|
* Time Complexity: O(n)
|
|
181
177
|
* Space Complexity: O(log n)
|
|
182
178
|
*
|
|
183
179
|
* The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
|
|
184
180
|
* value and returns the corresponding node or null.
|
|
185
|
-
* @param {BTNRep<K, V,
|
|
186
|
-
* parameter in the `ensureNode` function can be of type `BTNRep<K, V,
|
|
181
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
182
|
+
* parameter in the `ensureNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It
|
|
187
183
|
* is used to determine whether the input is a key, node, entry, or raw data. The
|
|
188
184
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
|
|
189
185
|
* is used to specify the type of iteration to be performed. It has a default value of
|
|
@@ -191,49 +187,48 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
191
187
|
* @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
|
|
192
188
|
* conditions specified in the code snippet.
|
|
193
189
|
*/
|
|
194
|
-
ensureNode(
|
|
195
|
-
if (
|
|
190
|
+
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
191
|
+
if (keyNodeOrEntry === null)
|
|
196
192
|
return null;
|
|
197
|
-
if (
|
|
193
|
+
if (keyNodeOrEntry === undefined)
|
|
198
194
|
return;
|
|
199
|
-
if (
|
|
195
|
+
if (keyNodeOrEntry === this._NIL)
|
|
200
196
|
return;
|
|
201
|
-
if (this.isNode(
|
|
202
|
-
return
|
|
203
|
-
if (this.isEntry(
|
|
204
|
-
const key =
|
|
197
|
+
if (this.isNode(keyNodeOrEntry))
|
|
198
|
+
return keyNodeOrEntry;
|
|
199
|
+
if (this.isEntry(keyNodeOrEntry)) {
|
|
200
|
+
const key = keyNodeOrEntry[0];
|
|
205
201
|
if (key === null)
|
|
206
202
|
return null;
|
|
207
203
|
if (key === undefined)
|
|
208
204
|
return;
|
|
209
205
|
return this.getNode(key, this._root, iterationType);
|
|
210
206
|
}
|
|
211
|
-
|
|
212
|
-
const [key] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
213
|
-
if (this.isKey(key))
|
|
214
|
-
return this.getNode(key);
|
|
215
|
-
}
|
|
216
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
217
|
-
return this.getNode(keyNodeEntryOrRaw, this._root, iterationType);
|
|
218
|
-
return;
|
|
207
|
+
return this.getNode(keyNodeOrEntry, this._root, iterationType);
|
|
219
208
|
}
|
|
220
209
|
/**
|
|
210
|
+
* Time Complexity: O(1)
|
|
211
|
+
* Space Complexity: O(1)
|
|
212
|
+
*
|
|
221
213
|
* The function isNode checks if the input is an instance of BinaryTreeNode.
|
|
222
|
-
* @param {BTNRep<K, V,
|
|
223
|
-
* `
|
|
214
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
215
|
+
* `keyNodeOrEntry` can be either a key, a node, an entry, or raw data. The function is
|
|
224
216
|
* checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
|
|
225
217
|
* accordingly.
|
|
226
|
-
* @returns The function `isNode` is checking if the input `
|
|
218
|
+
* @returns The function `isNode` is checking if the input `keyNodeOrEntry` is an instance of
|
|
227
219
|
* `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
|
|
228
220
|
* it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
|
|
229
221
|
* is not a node.
|
|
230
222
|
*/
|
|
231
|
-
isNode(
|
|
232
|
-
return
|
|
223
|
+
isNode(keyNodeOrEntry) {
|
|
224
|
+
return keyNodeOrEntry instanceof BinaryTreeNode;
|
|
233
225
|
}
|
|
234
226
|
/**
|
|
227
|
+
* Time Complexity: O(1)
|
|
228
|
+
* Space Complexity: O(1)
|
|
229
|
+
*
|
|
235
230
|
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
236
|
-
* @param {BTNRep<K, V,
|
|
231
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | R} keyNodeEntryOrRaw - BTNRep<K, V, BinaryTreeNode<K, V>>
|
|
237
232
|
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
238
233
|
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
239
234
|
* indicating that it is of type `R`.
|
|
@@ -242,88 +237,117 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
242
237
|
return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
|
|
243
238
|
}
|
|
244
239
|
/**
|
|
240
|
+
* Time Complexity: O(1)
|
|
241
|
+
* Space Complexity: O(1)
|
|
242
|
+
*
|
|
245
243
|
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
246
|
-
* @param {BTNRep<K, V,
|
|
247
|
-
* parameter in the `isRealNode` function can be of type `BTNRep<K, V,
|
|
248
|
-
* The function checks if the input parameter is a `
|
|
249
|
-
* @returns The function `isRealNode` is checking if the input `
|
|
244
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
245
|
+
* parameter in the `isRealNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`.
|
|
246
|
+
* The function checks if the input parameter is a `BinaryTreeNode<K, V>` type by verifying if it is not equal
|
|
247
|
+
* @returns The function `isRealNode` is checking if the input `keyNodeOrEntry` is a valid
|
|
250
248
|
* node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
|
|
251
249
|
* values, it then calls the `isNode` method to further determine if the input is a node. The
|
|
252
250
|
* function will return a boolean value indicating whether the
|
|
253
251
|
*/
|
|
254
|
-
isRealNode(
|
|
255
|
-
if (
|
|
252
|
+
isRealNode(keyNodeOrEntry) {
|
|
253
|
+
if (keyNodeOrEntry === this._NIL || keyNodeOrEntry === null || keyNodeOrEntry === undefined)
|
|
256
254
|
return false;
|
|
257
|
-
return this.isNode(
|
|
255
|
+
return this.isNode(keyNodeOrEntry);
|
|
258
256
|
}
|
|
259
257
|
/**
|
|
258
|
+
* Time Complexity: O(1)
|
|
259
|
+
* Space Complexity: O(1)
|
|
260
|
+
*
|
|
260
261
|
* The function checks if a given input is a valid node or null.
|
|
261
|
-
* @param {BTNRep<K, V,
|
|
262
|
-
* `
|
|
263
|
-
* V,
|
|
262
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
263
|
+
* `keyNodeOrEntry` in the `isRealNodeOrNull` function can be of type `BTNRep<K,
|
|
264
|
+
* V, BinaryTreeNode<K, V>>` or `R`. It is a union type that can either be a key, a node, an entry, or
|
|
264
265
|
* @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
|
|
265
|
-
* `
|
|
266
|
+
* `keyNodeOrEntry` is either `null` or a real node, and returns `true` if it is a node or
|
|
266
267
|
* `null`, and `false` otherwise.
|
|
267
268
|
*/
|
|
268
|
-
isRealNodeOrNull(
|
|
269
|
-
return
|
|
269
|
+
isRealNodeOrNull(keyNodeOrEntry) {
|
|
270
|
+
return keyNodeOrEntry === null || this.isRealNode(keyNodeOrEntry);
|
|
270
271
|
}
|
|
271
272
|
/**
|
|
273
|
+
* Time Complexity: O(1)
|
|
274
|
+
* Space Complexity: O(1)
|
|
275
|
+
*
|
|
272
276
|
* The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
|
|
273
|
-
* @param {BTNRep<K, V,
|
|
274
|
-
*
|
|
275
|
-
* @returns The function is checking if the `
|
|
277
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - BTNRep<K, V,
|
|
278
|
+
* BinaryTreeNode<K, V>>
|
|
279
|
+
* @returns The function is checking if the `keyNodeOrEntry` parameter is equal to the `_NIL`
|
|
276
280
|
* property of the current object and returning a boolean value based on that comparison.
|
|
277
281
|
*/
|
|
278
|
-
isNIL(
|
|
279
|
-
return
|
|
282
|
+
isNIL(keyNodeOrEntry) {
|
|
283
|
+
return keyNodeOrEntry === this._NIL;
|
|
280
284
|
}
|
|
281
|
-
|
|
282
|
-
|
|
285
|
+
/**
|
|
286
|
+
* Time Complexity: O(1)
|
|
287
|
+
* Space Complexity: O(1)
|
|
288
|
+
*
|
|
289
|
+
* The function `isRange` checks if the input parameter is an instance of the `Range` class.
|
|
290
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>> | Range<K>}
|
|
291
|
+
* keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be
|
|
292
|
+
* of type `BTNRep<K, V, BinaryTreeNode<K, V>>`, `NodePredicate<BinaryTreeNode<K, V>>`, or
|
|
293
|
+
* `Range<K>`. The function checks if the `keyNodeEntry
|
|
294
|
+
* @returns The `isRange` function is checking if the `keyNodeEntryOrPredicate` parameter is an
|
|
295
|
+
* instance of the `Range` class. If it is an instance of `Range`, the function will return `true`,
|
|
296
|
+
* indicating that the parameter is a `Range<K>`. If it is not an instance of `Range`, the function
|
|
297
|
+
* will return `false`.
|
|
298
|
+
*/
|
|
299
|
+
isRange(keyNodeEntryOrPredicate) {
|
|
300
|
+
return keyNodeEntryOrPredicate instanceof common_1.Range;
|
|
283
301
|
}
|
|
284
302
|
/**
|
|
303
|
+
* Time Complexity: O(1)
|
|
304
|
+
* Space Complexity: O(1)
|
|
305
|
+
*
|
|
285
306
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
286
307
|
* tree.
|
|
287
|
-
* @param {BTNRep<K, V,
|
|
288
|
-
* `
|
|
308
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
309
|
+
* `keyNodeOrEntry` can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It represents a
|
|
289
310
|
* key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
|
|
290
311
|
* provided
|
|
291
312
|
* @returns The function `isLeaf` returns a boolean value indicating whether the input
|
|
292
|
-
* `
|
|
313
|
+
* `keyNodeOrEntry` is a leaf node in a binary tree.
|
|
293
314
|
*/
|
|
294
|
-
isLeaf(
|
|
295
|
-
|
|
296
|
-
if (
|
|
315
|
+
isLeaf(keyNodeOrEntry) {
|
|
316
|
+
keyNodeOrEntry = this.ensureNode(keyNodeOrEntry);
|
|
317
|
+
if (keyNodeOrEntry === undefined)
|
|
297
318
|
return false;
|
|
298
|
-
if (
|
|
319
|
+
if (keyNodeOrEntry === null)
|
|
299
320
|
return true;
|
|
300
|
-
return !this.isRealNode(
|
|
321
|
+
return !this.isRealNode(keyNodeOrEntry.left) && !this.isRealNode(keyNodeOrEntry.right);
|
|
301
322
|
}
|
|
302
323
|
/**
|
|
324
|
+
* Time Complexity: O(1)
|
|
325
|
+
* Space Complexity: O(1)
|
|
326
|
+
*
|
|
303
327
|
* The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
|
|
304
328
|
* with a length of 2.
|
|
305
|
-
* @param {BTNRep<K, V,
|
|
306
|
-
* parameter in the `isEntry` function can be of type `BTNRep<K, V,
|
|
307
|
-
* The function checks if the provided `
|
|
308
|
-
* @returns The `isEntry` function is checking if the `
|
|
329
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
330
|
+
* parameter in the `isEntry` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or type `R`.
|
|
331
|
+
* The function checks if the provided `keyNodeOrEntry` is of type `BTN
|
|
332
|
+
* @returns The `isEntry` function is checking if the `keyNodeOrEntry` parameter is an array
|
|
309
333
|
* with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
|
|
310
334
|
* `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
|
|
311
335
|
*/
|
|
312
|
-
isEntry(
|
|
313
|
-
return Array.isArray(
|
|
336
|
+
isEntry(keyNodeOrEntry) {
|
|
337
|
+
return Array.isArray(keyNodeOrEntry) && keyNodeOrEntry.length === 2;
|
|
314
338
|
}
|
|
315
339
|
/**
|
|
316
340
|
* Time Complexity O(1)
|
|
317
341
|
* Space Complexity O(1)
|
|
318
342
|
*
|
|
319
|
-
* The function `
|
|
343
|
+
* The function `isValidKey` checks if a given key is comparable.
|
|
320
344
|
* @param {any} key - The `key` parameter is of type `any`, which means it can be any data type in
|
|
321
345
|
* TypeScript.
|
|
322
|
-
* @returns The function `
|
|
346
|
+
* @returns The function `isValidKey` is checking if the `key` parameter is `null` or if it is comparable.
|
|
323
347
|
* If the `key` is `null`, the function returns `true`. Otherwise, it returns the result of the
|
|
324
348
|
* `isComparable` function, which is not provided in the code snippet.
|
|
325
349
|
*/
|
|
326
|
-
|
|
350
|
+
isValidKey(key) {
|
|
327
351
|
if (key === null)
|
|
328
352
|
return true;
|
|
329
353
|
return (0, utils_1.isComparable)(key);
|
|
@@ -334,8 +358,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
334
358
|
*
|
|
335
359
|
* The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
|
|
336
360
|
* and finding the correct insertion position.
|
|
337
|
-
* @param {BTNRep<K, V,
|
|
338
|
-
* seems to be for adding a new node to a binary tree structure. The `
|
|
361
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `add` method you provided
|
|
362
|
+
* seems to be for adding a new node to a binary tree structure. The `keyNodeOrEntry`
|
|
339
363
|
* parameter in the method can accept different types of values:
|
|
340
364
|
* @param {V} [value] - The `value` parameter in the `add` method represents the value associated
|
|
341
365
|
* with the key that you want to add to the binary tree. When adding a key-value pair to the binary
|
|
@@ -345,8 +369,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
345
369
|
* node was successful, and `false` if the insertion position could not be found or if a duplicate
|
|
346
370
|
* key was found and the node was replaced instead of inserted.
|
|
347
371
|
*/
|
|
348
|
-
add(
|
|
349
|
-
const [newNode, newValue] = this.
|
|
372
|
+
add(keyNodeOrEntry, value) {
|
|
373
|
+
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
350
374
|
if (newNode === undefined)
|
|
351
375
|
return false;
|
|
352
376
|
// If the tree is empty, directly set the new node as the root node
|
|
@@ -401,14 +425,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
401
425
|
}
|
|
402
426
|
/**
|
|
403
427
|
* Time Complexity: O(k * n)
|
|
404
|
-
* Space Complexity: O(
|
|
428
|
+
* Space Complexity: O(k)
|
|
405
429
|
*
|
|
406
430
|
* The `addMany` function takes in multiple keys or nodes or entries or raw values along with
|
|
407
431
|
* optional values, and adds them to a data structure while returning an array indicating whether
|
|
408
432
|
* each insertion was successful.
|
|
409
433
|
* @param keysNodesEntriesOrRaws - `keysNodesEntriesOrRaws` is an iterable that can contain a
|
|
410
434
|
* mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
|
|
411
|
-
* `BTNRep<K, V,
|
|
435
|
+
* `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`.
|
|
412
436
|
* @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
|
|
413
437
|
* accepts an iterable of values. These values correspond to the keys or nodes being added in the
|
|
414
438
|
* `keysNodesEntriesOrRaws` parameter. If provided, the function will iterate over the values and
|
|
@@ -424,7 +448,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
424
448
|
if (values) {
|
|
425
449
|
valuesIterator = values[Symbol.iterator]();
|
|
426
450
|
}
|
|
427
|
-
for (
|
|
451
|
+
for (let keyNodeEntryOrRaw of keysNodesEntriesOrRaws) {
|
|
428
452
|
let value = undefined;
|
|
429
453
|
if (valuesIterator) {
|
|
430
454
|
const valueResult = valuesIterator.next();
|
|
@@ -432,6 +456,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
432
456
|
value = valueResult.value;
|
|
433
457
|
}
|
|
434
458
|
}
|
|
459
|
+
if (this.isRaw(keyNodeEntryOrRaw))
|
|
460
|
+
keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
|
|
435
461
|
inserted.push(this.add(keyNodeEntryOrRaw, value));
|
|
436
462
|
}
|
|
437
463
|
return inserted;
|
|
@@ -442,7 +468,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
442
468
|
*
|
|
443
469
|
* The `merge` function in TypeScript merges another binary tree into the current tree by adding all
|
|
444
470
|
* elements from the other tree.
|
|
445
|
-
* @param anotherTree -
|
|
471
|
+
* @param anotherTree - BinaryTree<K, V, R, MK, MV, MR>
|
|
446
472
|
*/
|
|
447
473
|
merge(anotherTree) {
|
|
448
474
|
this.addMany(anotherTree, []);
|
|
@@ -454,7 +480,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
454
480
|
* The `refill` function clears the existing data structure and then adds new key-value pairs based
|
|
455
481
|
* on the provided input.
|
|
456
482
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill`
|
|
457
|
-
* method can accept an iterable containing a mix of `BTNRep<K, V,
|
|
483
|
+
* method can accept an iterable containing a mix of `BTNRep<K, V, BinaryTreeNode<K, V>>` objects or `R`
|
|
458
484
|
* objects.
|
|
459
485
|
* @param [values] - The `values` parameter in the `refill` method is an optional parameter that
|
|
460
486
|
* accepts an iterable of values of type `V` or `undefined`.
|
|
@@ -469,7 +495,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
469
495
|
*
|
|
470
496
|
* The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
|
|
471
497
|
* the deleted node along with information for tree balancing.
|
|
472
|
-
* @param {BTNRep<K, V,
|
|
498
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry
|
|
473
499
|
* - The `delete` method you provided is used to delete a node from a binary tree based on the key,
|
|
474
500
|
* node, entry or raw data. The method returns an array of
|
|
475
501
|
* `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
|
|
@@ -478,11 +504,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
478
504
|
* the array contains information about the node that was deleted (`deleted`) and the node that may
|
|
479
505
|
* need to be balanced (`needBalanced`).
|
|
480
506
|
*/
|
|
481
|
-
delete(
|
|
507
|
+
delete(keyNodeOrEntry) {
|
|
482
508
|
const deletedResult = [];
|
|
483
509
|
if (!this._root)
|
|
484
510
|
return deletedResult;
|
|
485
|
-
const curr = this.getNode(
|
|
511
|
+
const curr = this.getNode(keyNodeOrEntry);
|
|
486
512
|
if (!curr)
|
|
487
513
|
return deletedResult;
|
|
488
514
|
const parent = curr === null || curr === void 0 ? void 0 : curr.parent;
|
|
@@ -531,15 +557,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
531
557
|
*
|
|
532
558
|
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
533
559
|
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
534
|
-
* @param {BTNRep<K, V,
|
|
535
|
-
* `
|
|
560
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
561
|
+
* `keyNodeEntryOrPredicate` parameter in the `search` function can accept three types of values:
|
|
536
562
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
537
563
|
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
538
564
|
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
539
565
|
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
540
566
|
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
541
|
-
* extends `NodeCallback<
|
|
542
|
-
* @param {BTNRep<K, V,
|
|
567
|
+
* extends `NodeCallback<BinaryTreeNode<K, V>>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
568
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `search` function is
|
|
543
569
|
* used to specify the node from which the search operation should begin. It represents the starting
|
|
544
570
|
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
545
571
|
* provided, the search operation will start from the root
|
|
@@ -549,15 +575,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
549
575
|
* @returns The `search` function returns an array of values that match the provided criteria based
|
|
550
576
|
* on the search algorithm implemented within the function.
|
|
551
577
|
*/
|
|
552
|
-
search(
|
|
553
|
-
if (
|
|
578
|
+
search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
579
|
+
if (keyNodeEntryOrPredicate === undefined)
|
|
554
580
|
return [];
|
|
555
|
-
if (
|
|
581
|
+
if (keyNodeEntryOrPredicate === null)
|
|
556
582
|
return [];
|
|
557
583
|
startNode = this.ensureNode(startNode);
|
|
558
584
|
if (!startNode)
|
|
559
585
|
return [];
|
|
560
|
-
const predicate = this._ensurePredicate(
|
|
586
|
+
const predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
561
587
|
const ans = [];
|
|
562
588
|
if (iterationType === 'RECURSIVE') {
|
|
563
589
|
const dfs = (cur) => {
|
|
@@ -600,12 +626,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
600
626
|
*
|
|
601
627
|
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
602
628
|
* or predicate, with options for recursive or iterative traversal.
|
|
603
|
-
* @param {BTNRep<K, V,
|
|
629
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
604
630
|
* - The `getNodes` function you provided takes several parameters:
|
|
605
631
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
606
632
|
* determines whether to return only the first node that matches the criteria specified by the
|
|
607
|
-
* `
|
|
608
|
-
* @param {BTNRep<K, V,
|
|
633
|
+
* `keyNodeEntryOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
634
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
609
635
|
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
610
636
|
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
611
637
|
* not provided, the default value is set to `this._root
|
|
@@ -615,19 +641,19 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
615
641
|
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
616
642
|
* based on the input parameters and the iteration type specified.
|
|
617
643
|
*/
|
|
618
|
-
getNodes(
|
|
619
|
-
return this.search(
|
|
644
|
+
getNodes(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
645
|
+
return this.search(keyNodeEntryOrPredicate, onlyOne, node => node, startNode, iterationType);
|
|
620
646
|
}
|
|
621
647
|
/**
|
|
622
648
|
* Time Complexity: O(n)
|
|
623
|
-
* Space Complexity: O(log n)
|
|
649
|
+
* Space Complexity: O(log n)
|
|
624
650
|
*
|
|
625
651
|
* The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
|
|
626
652
|
* predicate.
|
|
627
|
-
* @param {BTNRep<K, V,
|
|
628
|
-
* - The `
|
|
653
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
654
|
+
* - The `keyNodeEntryOrPredicate` parameter in the `getNode` function can accept a key,
|
|
629
655
|
* node, entry, raw data, or a predicate function.
|
|
630
|
-
* @param {BTNRep<K, V,
|
|
656
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
631
657
|
* `getNode` function is used to specify the starting point for searching for a node in a binary
|
|
632
658
|
* tree. If no specific starting point is provided, the default value is set to `this._root`, which
|
|
633
659
|
* is typically the root node of the binary tree.
|
|
@@ -638,9 +664,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
638
664
|
* @returns The `getNode` function is returning the first node that matches the specified criteria,
|
|
639
665
|
* or `null` if no matching node is found.
|
|
640
666
|
*/
|
|
641
|
-
getNode(
|
|
642
|
-
|
|
643
|
-
return (_a = this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
667
|
+
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
668
|
+
return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType)[0];
|
|
644
669
|
}
|
|
645
670
|
/**
|
|
646
671
|
* Time Complexity: O(n)
|
|
@@ -648,10 +673,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
648
673
|
*
|
|
649
674
|
* This function overrides the `get` method to retrieve the value associated with a specified key,
|
|
650
675
|
* node, entry, raw data, or predicate in a data structure.
|
|
651
|
-
* @param {BTNRep<K, V,
|
|
652
|
-
* - The `
|
|
676
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
677
|
+
* - The `keyNodeEntryOrPredicate` parameter in the `get` method can accept one of the
|
|
653
678
|
* following types:
|
|
654
|
-
* @param {BTNRep<K, V,
|
|
679
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `get`
|
|
655
680
|
* method is used to specify the starting point for searching for a key or node in the binary tree.
|
|
656
681
|
* If no specific starting point is provided, the default starting point is the root of the binary
|
|
657
682
|
* tree (`this._root`).
|
|
@@ -664,15 +689,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
664
689
|
* the method returns the corresponding value. If the key or node is not found, it returns
|
|
665
690
|
* `undefined`.
|
|
666
691
|
*/
|
|
667
|
-
get(
|
|
692
|
+
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
668
693
|
var _a;
|
|
669
694
|
if (this._isMapMode) {
|
|
670
|
-
const key = this._extractKey(
|
|
695
|
+
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
671
696
|
if (key === null || key === undefined)
|
|
672
697
|
return;
|
|
673
698
|
return this._store.get(key);
|
|
674
699
|
}
|
|
675
|
-
return (_a = this.getNode(
|
|
700
|
+
return (_a = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) === null || _a === void 0 ? void 0 : _a.value;
|
|
676
701
|
}
|
|
677
702
|
/**
|
|
678
703
|
* Time Complexity: O(n)
|
|
@@ -680,10 +705,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
680
705
|
*
|
|
681
706
|
* The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
|
|
682
707
|
* exists in the data structure.
|
|
683
|
-
* @param {BTNRep<K, V,
|
|
684
|
-
* - The `
|
|
708
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
709
|
+
* - The `keyNodeEntryOrPredicate` parameter in the `override has` method can accept one of
|
|
685
710
|
* the following types:
|
|
686
|
-
* @param {BTNRep<K, V,
|
|
711
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
687
712
|
* `override` method is used to specify the starting point for the search operation within the data
|
|
688
713
|
* structure. It defaults to `this._root` if not provided explicitly.
|
|
689
714
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
|
|
@@ -695,14 +720,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
695
720
|
* are matching nodes, it returns `true`, indicating that the tree contains the specified element.
|
|
696
721
|
* Otherwise, it returns `false`.
|
|
697
722
|
*/
|
|
698
|
-
has(
|
|
699
|
-
return this.search(
|
|
723
|
+
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
724
|
+
return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType).length > 0;
|
|
700
725
|
}
|
|
701
726
|
/**
|
|
702
727
|
* Time Complexity: O(1)
|
|
703
728
|
* Space Complexity: O(1)
|
|
704
729
|
*
|
|
705
|
-
* The
|
|
730
|
+
* The clear function removes nodes and values in map mode.
|
|
706
731
|
*/
|
|
707
732
|
clear() {
|
|
708
733
|
this._clearNodes();
|
|
@@ -727,7 +752,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
727
752
|
*
|
|
728
753
|
* The function checks if a binary tree is perfectly balanced by comparing its minimum height with
|
|
729
754
|
* its height.
|
|
730
|
-
* @param {BTNRep<K, V,
|
|
755
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
731
756
|
* point for checking if the binary tree is perfectly balanced. It represents the root node of the
|
|
732
757
|
* binary tree or a specific node from which the balance check should begin.
|
|
733
758
|
* @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
|
|
@@ -741,11 +766,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
741
766
|
}
|
|
742
767
|
/**
|
|
743
768
|
* Time Complexity: O(n)
|
|
744
|
-
* Space Complexity: O(
|
|
769
|
+
* Space Complexity: O(log n)
|
|
745
770
|
*
|
|
746
771
|
* The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
|
|
747
772
|
* or iterative methods.
|
|
748
|
-
* @param {BTNRep<K, V,
|
|
773
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `isBST`
|
|
749
774
|
* function represents the starting point for checking whether a binary search tree (BST) is valid.
|
|
750
775
|
* It can be a node in the BST or a reference to the root of the BST. If no specific node is
|
|
751
776
|
* provided, the function will default to
|
|
@@ -801,13 +826,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
801
826
|
}
|
|
802
827
|
/**
|
|
803
828
|
* Time Complexity: O(n)
|
|
804
|
-
* Space Complexity: O(
|
|
829
|
+
* Space Complexity: O(log n)
|
|
805
830
|
*
|
|
806
831
|
* The `getDepth` function calculates the depth between two nodes in a binary tree.
|
|
807
|
-
* @param {BTNRep<K, V,
|
|
832
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} dist - The `dist` parameter in the `getDepth`
|
|
808
833
|
* function represents the node or entry in a binary tree map, or a reference to a node in the tree.
|
|
809
834
|
* It is the target node for which you want to calculate the depth from the `startNode` node.
|
|
810
|
-
* @param {BTNRep<K, V,
|
|
835
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
811
836
|
* `getDepth` function represents the starting point from which you want to calculate the depth of a
|
|
812
837
|
* given node or entry in a binary tree. If no specific starting point is provided, the default value
|
|
813
838
|
* for `startNode` is set to the root of the binary
|
|
@@ -830,11 +855,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
830
855
|
}
|
|
831
856
|
/**
|
|
832
857
|
* Time Complexity: O(n)
|
|
833
|
-
* Space Complexity: O(
|
|
858
|
+
* Space Complexity: O(log n)
|
|
834
859
|
*
|
|
835
860
|
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
836
861
|
* or iterative approach in TypeScript.
|
|
837
|
-
* @param {BTNRep<K, V,
|
|
862
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
838
863
|
* point from which the height of the binary tree will be calculated. It can be a node in the binary
|
|
839
864
|
* tree or a reference to the root of the tree. If not provided, it defaults to the root of the
|
|
840
865
|
* binary tree data structure.
|
|
@@ -879,7 +904,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
879
904
|
*
|
|
880
905
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
881
906
|
* recursive or iterative approach in TypeScript.
|
|
882
|
-
* @param {BTNRep<K, V,
|
|
907
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
883
908
|
* `getMinHeight` function represents the starting node from which the minimum height of the binary
|
|
884
909
|
* tree will be calculated. It is either a node in the binary tree or a reference to the root of the
|
|
885
910
|
* tree. If not provided, the default value is the root
|
|
@@ -945,7 +970,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
945
970
|
* the path to the root. It is expected to be a function that takes a node as an argument and returns
|
|
946
971
|
* a value based on that node. The return type of the callback function is determined by the generic
|
|
947
972
|
* type `C
|
|
948
|
-
* @param {BTNRep<K, V,
|
|
973
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} beginNode - The `beginNode` parameter in the
|
|
949
974
|
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
|
|
950
975
|
* @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
|
|
951
976
|
* whether the resulting path from the given `beginNode` to the root should be in reverse order or
|
|
@@ -970,14 +995,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
970
995
|
}
|
|
971
996
|
/**
|
|
972
997
|
* Time Complexity: O(log n)
|
|
973
|
-
* Space Complexity: O(
|
|
998
|
+
* Space Complexity: O(log n)
|
|
974
999
|
*
|
|
975
1000
|
* The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
|
|
976
1001
|
* tail-recursive iteration.
|
|
977
1002
|
* @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
|
|
978
1003
|
* node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
|
|
979
1004
|
* value of `_DEFAULT_NODE_CALLBACK` if not specified.
|
|
980
|
-
* @param {BTNRep<K, V,
|
|
1005
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
981
1006
|
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary
|
|
982
1007
|
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
983
1008
|
* starting point is provided, the function will default
|
|
@@ -1015,15 +1040,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1015
1040
|
}
|
|
1016
1041
|
/**
|
|
1017
1042
|
* Time Complexity: O(log n)
|
|
1018
|
-
* Space Complexity: O(
|
|
1043
|
+
* Space Complexity: O(log n)
|
|
1019
1044
|
*
|
|
1020
1045
|
* The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
|
|
1021
1046
|
* or iterative traversal methods.
|
|
1022
1047
|
* @param {C} callback - The `callback` parameter is a function that will be called with the result
|
|
1023
|
-
* of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<
|
|
1048
|
+
* of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`,
|
|
1024
1049
|
* which means it is a callback function that can accept either an optional binary tree node or null
|
|
1025
1050
|
* as
|
|
1026
|
-
* @param {BTNRep<K, V,
|
|
1051
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
1027
1052
|
* `getRightMost` function represents the starting point for finding the rightmost node in a binary
|
|
1028
1053
|
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1029
1054
|
* starting point is provided, the function will default
|
|
@@ -1061,14 +1086,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1061
1086
|
}
|
|
1062
1087
|
/**
|
|
1063
1088
|
* Time Complexity: O(log n)
|
|
1064
|
-
* Space Complexity: O(
|
|
1089
|
+
* Space Complexity: O(log n)
|
|
1065
1090
|
*
|
|
1066
1091
|
* The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
|
|
1067
1092
|
* binary tree.
|
|
1068
|
-
* @param {
|
|
1093
|
+
* @param {BinaryTreeNode<K, V>} node - The `getPredecessor` function you provided seems to be attempting to find the
|
|
1069
1094
|
* predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
|
|
1070
1095
|
* while loop condition that might cause an infinite loop.
|
|
1071
|
-
* @returns The `getPredecessor` function returns the predecessor node of the input `
|
|
1096
|
+
* @returns The `getPredecessor` function returns the predecessor node of the input `BinaryTreeNode<K, V>` parameter.
|
|
1072
1097
|
* If the left child of the input node exists, it traverses to the rightmost node of the left subtree
|
|
1073
1098
|
* to find the predecessor. If the left child does not exist, it returns the input node itself.
|
|
1074
1099
|
*/
|
|
@@ -1088,12 +1113,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1088
1113
|
}
|
|
1089
1114
|
/**
|
|
1090
1115
|
* Time Complexity: O(log n)
|
|
1091
|
-
* Space Complexity: O(
|
|
1116
|
+
* Space Complexity: O(log n)
|
|
1092
1117
|
*
|
|
1093
1118
|
* The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
|
|
1094
1119
|
* binary tree.
|
|
1095
|
-
* @param {K |
|
|
1096
|
-
* type `K`, `
|
|
1120
|
+
* @param {K | BinaryTreeNode<K, V> | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
|
|
1121
|
+
* type `K`, `BinaryTreeNode<K, V>`, or `null`.
|
|
1097
1122
|
* @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
|
|
1098
1123
|
* a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
|
|
1099
1124
|
* have a right child, the function traverses up the parent nodes until it finds a node that is not
|
|
@@ -1120,12 +1145,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1120
1145
|
* The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
|
|
1121
1146
|
* specified parameters.
|
|
1122
1147
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends the
|
|
1123
|
-
* `NodeCallback` interface with a type parameter of `OptNodeOrNull<
|
|
1148
|
+
* `NodeCallback` interface with a type parameter of `OptNodeOrNull<BinaryTreeNode<K, V>>`. It has a default value of
|
|
1124
1149
|
* `this._DEFAULT_NODE_CALLBACK as C`.
|
|
1125
1150
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
|
|
1126
1151
|
* order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
|
|
1127
1152
|
* possible values for the `pattern` parameter are:
|
|
1128
|
-
* @param {BTNRep<K, V,
|
|
1153
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `dfs`
|
|
1129
1154
|
* method is used to specify the starting point for the Depth-First Search traversal. It can be
|
|
1130
1155
|
* either a `BTNRep` object representing a key, node, or entry in the binary tree map,
|
|
1131
1156
|
* or it can be a
|
|
@@ -1153,8 +1178,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1153
1178
|
* tree, executing a specified callback function on each node visited.
|
|
1154
1179
|
* @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
|
|
1155
1180
|
* called on each node visited during the breadth-first search traversal. It is a generic type `C`
|
|
1156
|
-
* that extends the `NodeCallback` type, which takes a parameter of type `
|
|
1157
|
-
* @param {BTNRep<K, V,
|
|
1181
|
+
* that extends the `NodeCallback` type, which takes a parameter of type `BinaryTreeNode<K, V>` or `null`.
|
|
1182
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `bfs`
|
|
1158
1183
|
* function represents the starting point for the breadth-first search traversal in a binary tree. It
|
|
1159
1184
|
* can be specified as a key, node, or entry in the binary tree structure. If not provided, the
|
|
1160
1185
|
* default value is the root node of the binary
|
|
@@ -1174,7 +1199,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1174
1199
|
return [];
|
|
1175
1200
|
const ans = [];
|
|
1176
1201
|
if (iterationType === 'RECURSIVE') {
|
|
1177
|
-
const queue = new queue_1.Queue([
|
|
1202
|
+
const queue = new queue_1.Queue([
|
|
1203
|
+
startNode
|
|
1204
|
+
]);
|
|
1178
1205
|
const dfs = (level) => {
|
|
1179
1206
|
if (queue.size === 0)
|
|
1180
1207
|
return;
|
|
@@ -1228,7 +1255,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1228
1255
|
* structure based on a specified callback and iteration type.
|
|
1229
1256
|
* @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
|
|
1230
1257
|
* in the binary tree. It is optional and defaults to a default callback function if not provided.
|
|
1231
|
-
* @param {BTNRep<K, V,
|
|
1258
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `leaves`
|
|
1232
1259
|
* method is used to specify the starting point for finding and processing the leaves of a binary
|
|
1233
1260
|
* tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
|
|
1234
1261
|
* explicitly provided, the default value
|
|
@@ -1283,7 +1310,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1283
1310
|
* @param {C} callback - The `callback` parameter is a function that will be applied to each node in
|
|
1284
1311
|
* the binary tree during the traversal. It is used to process each node and determine what
|
|
1285
1312
|
* information to include in the output for each level of the tree.
|
|
1286
|
-
* @param {BTNRep<K, V,
|
|
1313
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
1287
1314
|
* `listLevels` function represents the starting point for traversing the binary tree. It can be
|
|
1288
1315
|
* either a key, a node, or an entry in the binary tree. If not provided, the default value is the
|
|
1289
1316
|
* root of the binary tree.
|
|
@@ -1355,11 +1382,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1355
1382
|
* Morris Traversal algorithm with different order patterns.
|
|
1356
1383
|
* @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
|
|
1357
1384
|
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the
|
|
1358
|
-
* `NodeCallback<
|
|
1385
|
+
* `NodeCallback<BinaryTreeNode<K, V>>` type. The default value for `callback` is `this._DEFAULT
|
|
1359
1386
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
|
|
1360
1387
|
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
|
|
1361
1388
|
* values for the `pattern` parameter are:
|
|
1362
|
-
* @param {BTNRep<K, V,
|
|
1389
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `morris`
|
|
1363
1390
|
* function is the starting point for the Morris traversal algorithm. It represents the root node of
|
|
1364
1391
|
* the binary tree or the node from which the traversal should begin. It can be provided as either a
|
|
1365
1392
|
* key, a node, an entry, or a reference
|
|
@@ -1465,6 +1492,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1465
1492
|
*/
|
|
1466
1493
|
clone() {
|
|
1467
1494
|
const cloned = this.createTree();
|
|
1495
|
+
this._clone(cloned);
|
|
1496
|
+
return cloned;
|
|
1497
|
+
}
|
|
1498
|
+
_clone(cloned) {
|
|
1468
1499
|
this.bfs(node => {
|
|
1469
1500
|
if (node === null)
|
|
1470
1501
|
cloned.add(null);
|
|
@@ -1477,7 +1508,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1477
1508
|
}, this._root, this.iterationType, true);
|
|
1478
1509
|
if (this._isMapMode)
|
|
1479
1510
|
cloned._store = this._store;
|
|
1480
|
-
return cloned;
|
|
1481
1511
|
}
|
|
1482
1512
|
/**
|
|
1483
1513
|
* Time Complexity: O(n)
|
|
@@ -1499,7 +1529,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1499
1529
|
const newTree = this.createTree();
|
|
1500
1530
|
let index = 0;
|
|
1501
1531
|
for (const [key, value] of this) {
|
|
1502
|
-
if (predicate.call(thisArg,
|
|
1532
|
+
if (predicate.call(thisArg, key, value, index++, this)) {
|
|
1503
1533
|
newTree.add([key, value]);
|
|
1504
1534
|
}
|
|
1505
1535
|
}
|
|
@@ -1509,41 +1539,36 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1509
1539
|
* Time Complexity: O(n)
|
|
1510
1540
|
* Space Complexity: O(n)
|
|
1511
1541
|
*
|
|
1512
|
-
* The `map` function
|
|
1513
|
-
*
|
|
1514
|
-
* @param callback -
|
|
1515
|
-
*
|
|
1516
|
-
*
|
|
1517
|
-
*
|
|
1518
|
-
*
|
|
1519
|
-
*
|
|
1520
|
-
*
|
|
1521
|
-
*
|
|
1542
|
+
* The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
|
|
1543
|
+
* entry in the original BinaryTree.
|
|
1544
|
+
* @param callback - A function that will be called for each entry in the current binary tree. It
|
|
1545
|
+
* takes the key, value (which can be undefined), and an array containing the mapped key and value as
|
|
1546
|
+
* arguments.
|
|
1547
|
+
* @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
|
|
1548
|
+
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
|
|
1549
|
+
* tree being created during the mapping process. These options could include things like custom
|
|
1550
|
+
* comparators, initial
|
|
1551
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
|
|
1552
|
+
* of `this` when executing the `callback` function. It allows you to set the context (value of
|
|
1553
|
+
* `this`) within the callback function. If `thisArg` is provided, it will be passed
|
|
1554
|
+
* @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
|
|
1555
|
+
* the result of applying the provided `callback` function to each entry in the original tree.
|
|
1522
1556
|
*/
|
|
1523
|
-
map(callback, thisArg) {
|
|
1524
|
-
const newTree =
|
|
1557
|
+
map(callback, options, thisArg) {
|
|
1558
|
+
const newTree = new BinaryTree([], options);
|
|
1525
1559
|
let index = 0;
|
|
1526
1560
|
for (const [key, value] of this) {
|
|
1527
|
-
newTree.add(
|
|
1561
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
1528
1562
|
}
|
|
1529
1563
|
return newTree;
|
|
1530
1564
|
}
|
|
1531
|
-
// // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1532
|
-
// // map<NV>(callback: (entry: [K, V | undefined], tree: this) => NV) {
|
|
1533
|
-
// // const newTree = this.createTree();
|
|
1534
|
-
// // for (const [key, value] of this) {
|
|
1535
|
-
// // newTree.add(key, callback([key, value], this));
|
|
1536
|
-
// // }
|
|
1537
|
-
// // return newTree;
|
|
1538
|
-
// // }
|
|
1539
|
-
//
|
|
1540
1565
|
/**
|
|
1541
1566
|
* Time Complexity: O(n)
|
|
1542
1567
|
* Space Complexity: O(n)
|
|
1543
1568
|
*
|
|
1544
1569
|
* The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
|
|
1545
1570
|
* customizable options for displaying undefined, null, and sentinel nodes.
|
|
1546
|
-
* @param {BTNRep<K, V,
|
|
1571
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
1547
1572
|
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
|
|
1548
1573
|
* It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
|
|
1549
1574
|
* the default is set to the root
|
|
@@ -1568,7 +1593,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1568
1593
|
if (opts.isShowRedBlackNIL)
|
|
1569
1594
|
output += `S for Sentinel Node(NIL)\n`;
|
|
1570
1595
|
const display = (root) => {
|
|
1571
|
-
const [lines
|
|
1596
|
+
const [lines] = this._displayAux(root, opts);
|
|
1572
1597
|
let paragraph = '';
|
|
1573
1598
|
for (const line of lines) {
|
|
1574
1599
|
paragraph += line + '\n';
|
|
@@ -1588,7 +1613,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1588
1613
|
* printing options for the binary tree. It is an optional parameter that allows you to customize how
|
|
1589
1614
|
* the binary tree is printed, such as choosing between different traversal orders or formatting
|
|
1590
1615
|
* options.
|
|
1591
|
-
* @param {BTNRep<K, V,
|
|
1616
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
1592
1617
|
* `override print` method is used to specify the starting point for printing the binary tree. It can
|
|
1593
1618
|
* be either a key, a node, an entry, or the root of the tree. If no specific starting point is
|
|
1594
1619
|
* provided, the default value is set to
|
|
@@ -1596,6 +1621,42 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1596
1621
|
print(options, startNode = this._root) {
|
|
1597
1622
|
console.log(this.toVisual(startNode, options));
|
|
1598
1623
|
}
|
|
1624
|
+
/**
|
|
1625
|
+
* Time Complexity: O(1)
|
|
1626
|
+
* Space Complexity: O(1)
|
|
1627
|
+
*
|
|
1628
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
1629
|
+
* or returns null.
|
|
1630
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The
|
|
1631
|
+
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeOrEntry`, which
|
|
1632
|
+
* can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. This parameter represents either a key, a
|
|
1633
|
+
* node, an entry
|
|
1634
|
+
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
1635
|
+
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
1636
|
+
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
1637
|
+
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
1638
|
+
* (`OptNodeOrNull<BinaryTreeNode<K, V>>`) based on the input parameters provided. The function checks the type of the
|
|
1639
|
+
* input parameter (`keyNodeOrEntry`) and processes it accordingly to return a node or null
|
|
1640
|
+
* value.
|
|
1641
|
+
*/
|
|
1642
|
+
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
1643
|
+
if (keyNodeOrEntry === undefined)
|
|
1644
|
+
return [undefined, undefined];
|
|
1645
|
+
if (keyNodeOrEntry === null)
|
|
1646
|
+
return [null, undefined];
|
|
1647
|
+
if (this.isNode(keyNodeOrEntry))
|
|
1648
|
+
return [keyNodeOrEntry, value];
|
|
1649
|
+
if (this.isEntry(keyNodeOrEntry)) {
|
|
1650
|
+
const [key, entryValue] = keyNodeOrEntry;
|
|
1651
|
+
if (key === undefined)
|
|
1652
|
+
return [undefined, undefined];
|
|
1653
|
+
else if (key === null)
|
|
1654
|
+
return [null, undefined];
|
|
1655
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
1656
|
+
return [this.createNode(key, finalValue), finalValue];
|
|
1657
|
+
}
|
|
1658
|
+
return [this.createNode(keyNodeOrEntry, value), value];
|
|
1659
|
+
}
|
|
1599
1660
|
/**
|
|
1600
1661
|
* Time complexity: O(n)
|
|
1601
1662
|
* Space complexity: O(n)
|
|
@@ -1604,11 +1665,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1604
1665
|
* the specified order pattern and callback function.
|
|
1605
1666
|
* @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
|
|
1606
1667
|
* called on each node visited during the depth-first search traversal. It is of type `C`, which
|
|
1607
|
-
* extends `NodeCallback<OptNodeOrNull<
|
|
1668
|
+
* extends `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`. The default value for this parameter is `this._DEFAULT
|
|
1608
1669
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
|
|
1609
1670
|
* order in which the nodes are visited during the Depth-First Search traversal. It can have one of
|
|
1610
1671
|
* the following values:
|
|
1611
|
-
* @param {BTNRep<K, V,
|
|
1672
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `_dfs`
|
|
1612
1673
|
* method is used to specify the starting point for the depth-first search traversal in a binary
|
|
1613
1674
|
* tree. It can be provided as either a `BTNRep` object or a reference to the root node
|
|
1614
1675
|
* of the tree. If no specific
|
|
@@ -1852,12 +1913,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1852
1913
|
* Space Complexity: O(1)
|
|
1853
1914
|
*
|
|
1854
1915
|
* The _swapProperties function swaps key and value properties between two nodes in a binary tree.
|
|
1855
|
-
* @param {BTNRep<K, V,
|
|
1916
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} srcNode - The `srcNode` parameter in the
|
|
1856
1917
|
* `_swapProperties` method can be either a BTNRep object containing key and value
|
|
1857
1918
|
* properties, or it can be of type R.
|
|
1858
|
-
* @param {BTNRep<K, V,
|
|
1919
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} destNode - The `destNode` parameter in the
|
|
1859
1920
|
* `_swapProperties` method represents the node or entry where the properties will be swapped with
|
|
1860
|
-
* the `srcNode`. It can be of type `BTNRep<K, V,
|
|
1921
|
+
* the `srcNode`. It can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. The method ensures that
|
|
1861
1922
|
* both `srcNode
|
|
1862
1923
|
* @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
|
|
1863
1924
|
* with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
@@ -1885,9 +1946,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1885
1946
|
* Space Complexity: O(1)
|
|
1886
1947
|
*
|
|
1887
1948
|
* The _replaceNode function replaces an old node with a new node in a binary tree structure.
|
|
1888
|
-
* @param {
|
|
1949
|
+
* @param {BinaryTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that you want to replace in a
|
|
1889
1950
|
* tree data structure.
|
|
1890
|
-
* @param {
|
|
1951
|
+
* @param {BinaryTreeNode<K, V>} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
|
|
1891
1952
|
* that will replace the `oldNode` in a tree data structure. This function is responsible for
|
|
1892
1953
|
* updating the parent, left child, right child, and root (if necessary) references when replacing a
|
|
1893
1954
|
* node in the tree.
|
|
@@ -1917,8 +1978,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1917
1978
|
*
|
|
1918
1979
|
* The function _setRoot sets the root node of a data structure while updating the parent reference
|
|
1919
1980
|
* of the previous root node.
|
|
1920
|
-
* @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<
|
|
1921
|
-
* it can either be an optional `
|
|
1981
|
+
* @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<BinaryTreeNode<K, V>>`, which means
|
|
1982
|
+
* it can either be an optional `BinaryTreeNode<K, V>` type or `null`.
|
|
1922
1983
|
*/
|
|
1923
1984
|
_setRoot(v) {
|
|
1924
1985
|
if (v) {
|
|
@@ -1932,30 +1993,24 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1932
1993
|
*
|
|
1933
1994
|
* The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
|
|
1934
1995
|
* predicate function for a binary tree node.
|
|
1935
|
-
* @param {BTNRep<K, V,
|
|
1996
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
1936
1997
|
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
|
|
1937
|
-
* parameter `
|
|
1998
|
+
* parameter `keyNodeEntryOrPredicate` is transformed into a valid predicate function that can be
|
|
1938
1999
|
* used for filtering nodes in a binary tree.
|
|
1939
|
-
* @returns A NodePredicate<
|
|
2000
|
+
* @returns A NodePredicate<BinaryTreeNode<K, V>> function is being returned.
|
|
1940
2001
|
*/
|
|
1941
|
-
_ensurePredicate(
|
|
1942
|
-
if (
|
|
2002
|
+
_ensurePredicate(keyNodeEntryOrPredicate) {
|
|
2003
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined)
|
|
1943
2004
|
return (node) => (node ? false : false);
|
|
1944
|
-
if (this._isPredicate(
|
|
1945
|
-
return
|
|
1946
|
-
if (this.isRealNode(
|
|
1947
|
-
return (node) => node ===
|
|
1948
|
-
if (this.isEntry(
|
|
1949
|
-
const [key] =
|
|
1950
|
-
return (node) => node.key === key;
|
|
1951
|
-
}
|
|
1952
|
-
if (this.isKey(keyNodeEntryRawOrPredicate))
|
|
1953
|
-
return (node) => node.key === keyNodeEntryRawOrPredicate;
|
|
1954
|
-
if (this._toEntryFn) {
|
|
1955
|
-
const [key] = this._toEntryFn(keyNodeEntryRawOrPredicate);
|
|
2005
|
+
if (this._isPredicate(keyNodeEntryOrPredicate))
|
|
2006
|
+
return keyNodeEntryOrPredicate;
|
|
2007
|
+
if (this.isRealNode(keyNodeEntryOrPredicate))
|
|
2008
|
+
return (node) => node === keyNodeEntryOrPredicate;
|
|
2009
|
+
if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
2010
|
+
const [key] = keyNodeEntryOrPredicate;
|
|
1956
2011
|
return (node) => node.key === key;
|
|
1957
2012
|
}
|
|
1958
|
-
return (node) => node.key ===
|
|
2013
|
+
return (node) => node.key === keyNodeEntryOrPredicate;
|
|
1959
2014
|
}
|
|
1960
2015
|
/**
|
|
1961
2016
|
* Time Complexity: O(1)
|
|
@@ -1964,7 +2019,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1964
2019
|
* The function `_isPredicate` checks if a given parameter is a function.
|
|
1965
2020
|
* @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
|
|
1966
2021
|
* of value. In this context, the function `_isPredicate` is checking if `p` is a function that
|
|
1967
|
-
* satisfies the type `NodePredicate<
|
|
2022
|
+
* satisfies the type `NodePredicate<BinaryTreeNode<K, V>>`.
|
|
1968
2023
|
* @returns The function is checking if the input `p` is a function and returning a boolean value
|
|
1969
2024
|
* based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
|
|
1970
2025
|
* predicate function for a binary tree node. If `p` is not a function, it will return `false`.
|
|
@@ -1978,32 +2033,25 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1978
2033
|
*
|
|
1979
2034
|
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
1980
2035
|
* entry, raw data, or null/undefined.
|
|
1981
|
-
* @param {BTNRep<K, V,
|
|
1982
|
-
* TypeScript method that takes in a parameter `
|
|
1983
|
-
* where `BTNRep` is a generic type with keys `K`, `V`, and `
|
|
1984
|
-
* @returns The `_extractKey` method returns the key value extracted from the `
|
|
2036
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `_extractKey` method you provided is a
|
|
2037
|
+
* TypeScript method that takes in a parameter `keyNodeOrEntry` of type `BTNRep<K, V, BinaryTreeNode<K, V>>`,
|
|
2038
|
+
* where `BTNRep` is a generic type with keys `K`, `V`, and `BinaryTreeNode<K, V>`, and `
|
|
2039
|
+
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeOrEntry`
|
|
1985
2040
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
1986
2041
|
* the conditions checked in the method.
|
|
1987
2042
|
*/
|
|
1988
|
-
_extractKey(
|
|
1989
|
-
if (
|
|
2043
|
+
_extractKey(keyNodeOrEntry) {
|
|
2044
|
+
if (keyNodeOrEntry === null)
|
|
1990
2045
|
return null;
|
|
1991
|
-
if (
|
|
2046
|
+
if (keyNodeOrEntry === undefined)
|
|
1992
2047
|
return;
|
|
1993
|
-
if (
|
|
2048
|
+
if (keyNodeOrEntry === this._NIL)
|
|
1994
2049
|
return;
|
|
1995
|
-
if (this.isNode(
|
|
1996
|
-
return
|
|
1997
|
-
if (this.isEntry(
|
|
1998
|
-
return
|
|
1999
|
-
|
|
2000
|
-
if (this._toEntryFn) {
|
|
2001
|
-
const [key] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
2002
|
-
return key;
|
|
2003
|
-
}
|
|
2004
|
-
return;
|
|
2005
|
-
}
|
|
2006
|
-
return keyNodeEntryOrRaw;
|
|
2050
|
+
if (this.isNode(keyNodeOrEntry))
|
|
2051
|
+
return keyNodeOrEntry.key;
|
|
2052
|
+
if (this.isEntry(keyNodeOrEntry))
|
|
2053
|
+
return keyNodeOrEntry[0];
|
|
2054
|
+
return keyNodeOrEntry;
|
|
2007
2055
|
}
|
|
2008
2056
|
/**
|
|
2009
2057
|
* Time Complexity: O(1)
|