stack-typed 1.51.1 → 1.51.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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  ![GitHub top language](https://img.shields.io/github/languages/top/zrwusa/data-structure-typed)
3
3
  ![npm](https://img.shields.io/npm/dw/stack-typed)
4
4
  ![eslint](https://aleen42.github.io/badges/src/eslint.svg)
5
- ![npm package minimized gzipped size (select exports)](https://img.shields.io/bundlejs/size/stack-typed)
5
+ ![npm bundle size](https://img.shields.io/bundlephobia/minzip/stack-typed)
6
6
  ![npm bundle size](https://img.shields.io/bundlephobia/min/stack-typed)
7
7
  ![npm](https://img.shields.io/npm/v/stack-typed)
8
8
 
@@ -97,6 +97,12 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
97
97
  * @returns The size of the object, which is a number.
98
98
  */
99
99
  get size(): number;
100
+ protected _NIL: NODE;
101
+ /**
102
+ * The function returns the value of the _NIL property.
103
+ * @returns The method is returning the value of the `_NIL` property.
104
+ */
105
+ get NIL(): NODE;
100
106
  /**
101
107
  * Creates a new instance of BinaryTreeNode with the given key and value.
102
108
  * @param {K} key - The key for the new node.
@@ -140,19 +146,18 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
140
146
  * itself if it is not a valid node key.
141
147
  */
142
148
  ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE | null | undefined;
149
+ /**
150
+ * The function checks if a given node is a real node or null.
151
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
152
+ * @returns a boolean value.
153
+ */
154
+ isNodeOrNull(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null;
143
155
  /**
144
156
  * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
145
157
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
146
158
  * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class NODE.
147
159
  */
148
160
  isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
149
- /**
150
- * The function checks if a given value is an entry in a binary tree node.
151
- * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
152
- * two type parameters V and NODE, representing the value and node type respectively.
153
- * @returns a boolean value.
154
- */
155
- isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V>;
156
161
  /**
157
162
  * The function checks if a given node is a real node by verifying if it is an instance of
158
163
  * BinaryTreeNode and its key is not NaN.
@@ -167,11 +172,12 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
167
172
  */
168
173
  isNIL(node: KeyOrNodeOrEntry<K, V, NODE>): boolean;
169
174
  /**
170
- * The function checks if a given node is a real node or null.
171
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
175
+ * The function checks if a given value is an entry in a binary tree node.
176
+ * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
177
+ * two type parameters V and NODE, representing the value and node type respectively.
172
178
  * @returns a boolean value.
173
179
  */
174
- isNodeOrNull(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null;
180
+ isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V>;
175
181
  /**
176
182
  * Time Complexity O(n)
177
183
  * Space Complexity O(1)
@@ -107,6 +107,7 @@ class BinaryTree extends base_1.IterableEntryBase {
107
107
  super();
108
108
  this.iterationType = 'ITERATIVE';
109
109
  this._extractor = (key) => (typeof key === 'number' ? key : Number(key));
110
+ this._NIL = new BinaryTreeNode(NaN);
110
111
  this._DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
111
112
  if (options) {
112
113
  const { iterationType, extractor } = options;
@@ -141,6 +142,13 @@ class BinaryTree extends base_1.IterableEntryBase {
141
142
  get size() {
142
143
  return this._size;
143
144
  }
145
+ /**
146
+ * The function returns the value of the _NIL property.
147
+ * @returns The method is returning the value of the `_NIL` property.
148
+ */
149
+ get NIL() {
150
+ return this._NIL;
151
+ }
144
152
  /**
145
153
  * Creates a new instance of BinaryTreeNode with the given key and value.
146
154
  * @param {K} key - The key for the new node.
@@ -235,6 +243,14 @@ class BinaryTree extends base_1.IterableEntryBase {
235
243
  return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
236
244
  }
237
245
  }
246
+ /**
247
+ * The function checks if a given node is a real node or null.
248
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
249
+ * @returns a boolean value.
250
+ */
251
+ isNodeOrNull(node) {
252
+ return this.isRealNode(node) || node === null;
253
+ }
238
254
  /**
239
255
  * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
240
256
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
@@ -243,15 +259,6 @@ class BinaryTree extends base_1.IterableEntryBase {
243
259
  isNode(keyOrNodeOrEntry) {
244
260
  return keyOrNodeOrEntry instanceof BinaryTreeNode;
245
261
  }
246
- /**
247
- * The function checks if a given value is an entry in a binary tree node.
248
- * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
249
- * two type parameters V and NODE, representing the value and node type respectively.
250
- * @returns a boolean value.
251
- */
252
- isEntry(keyOrNodeOrEntry) {
253
- return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
254
- }
255
262
  /**
256
263
  * The function checks if a given node is a real node by verifying if it is an instance of
257
264
  * BinaryTreeNode and its key is not NaN.
@@ -259,7 +266,9 @@ class BinaryTree extends base_1.IterableEntryBase {
259
266
  * @returns a boolean value.
260
267
  */
261
268
  isRealNode(node) {
262
- return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
269
+ if (!this.isNode(node))
270
+ return false;
271
+ return node !== this.NIL;
263
272
  }
264
273
  /**
265
274
  * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
@@ -267,15 +276,16 @@ class BinaryTree extends base_1.IterableEntryBase {
267
276
  * @returns a boolean value.
268
277
  */
269
278
  isNIL(node) {
270
- return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
279
+ return node === this.NIL;
271
280
  }
272
281
  /**
273
- * The function checks if a given node is a real node or null.
274
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
282
+ * The function checks if a given value is an entry in a binary tree node.
283
+ * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
284
+ * two type parameters V and NODE, representing the value and node type respectively.
275
285
  * @returns a boolean value.
276
286
  */
277
- isNodeOrNull(node) {
278
- return this.isRealNode(node) || node === null;
287
+ isEntry(keyOrNodeOrEntry) {
288
+ return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
279
289
  }
280
290
  /**
281
291
  * Time Complexity O(n)
@@ -502,10 +512,10 @@ class BinaryTree extends base_1.IterableEntryBase {
502
512
  if (onlyOne)
503
513
  return;
504
514
  }
505
- if (!cur.left && !cur.right)
515
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
506
516
  return;
507
- cur.left && dfs(cur.left);
508
- cur.right && dfs(cur.right);
517
+ this.isRealNode(cur.left) && dfs(cur.left);
518
+ this.isRealNode(cur.right) && dfs(cur.right);
509
519
  };
510
520
  dfs(beginRoot);
511
521
  }
@@ -513,14 +523,14 @@ class BinaryTree extends base_1.IterableEntryBase {
513
523
  const stack = [beginRoot];
514
524
  while (stack.length > 0) {
515
525
  const cur = stack.pop();
516
- if (cur) {
526
+ if (this.isRealNode(cur)) {
517
527
  if (callback(cur) === identifier) {
518
528
  ans.push(cur);
519
529
  if (onlyOne)
520
530
  return ans;
521
531
  }
522
- cur.left && stack.push(cur.left);
523
- cur.right && stack.push(cur.right);
532
+ this.isRealNode(cur.left) && stack.push(cur.left);
533
+ this.isRealNode(cur.right) && stack.push(cur.right);
524
534
  }
525
535
  }
526
536
  }
@@ -553,8 +563,6 @@ class BinaryTree extends base_1.IterableEntryBase {
553
563
  */
554
564
  getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
555
565
  var _a;
556
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
557
- callback = (node => node);
558
566
  return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
559
567
  }
560
568
  /**
@@ -632,8 +640,6 @@ class BinaryTree extends base_1.IterableEntryBase {
632
640
  */
633
641
  get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
634
642
  var _a, _b;
635
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
636
- callback = (node => node);
637
643
  return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : undefined;
638
644
  }
639
645
  /**
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types';
9
- import { BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
9
+ import { BSTNKeyOrNode, BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
12
12
  export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
@@ -209,6 +209,32 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
209
209
  * @returns The method returns an array of nodes (`NODE[]`).
210
210
  */
211
211
  getNodes<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE[];
212
+ /**
213
+ * Time Complexity: O(log n)
214
+ * Space Complexity: O(1)
215
+ */
216
+ /**
217
+ * Time Complexity: O(log n)
218
+ * Space Complexity: O(1)
219
+ *
220
+ * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
221
+ * callback function.
222
+ * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
223
+ * that you want to search for in the binary search tree. It can be of any type that is compatible
224
+ * with the type of nodes in the tree.
225
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
226
+ * the tree. It is used to determine whether a node matches the given identifier. The `callback`
227
+ * function should take a node as its parameter and return a value that can be compared to the
228
+ * `identifier` parameter.
229
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
230
+ * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
231
+ * using the `ensureNode` method. If it is not provided, the `root`
232
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
233
+ * be performed when searching for nodes in the binary search tree. It is an optional parameter and
234
+ * its default value is taken from the `iterationType` property of the class.
235
+ * @returns The method is returning a value of type `NODE | null | undefined`.
236
+ */
237
+ getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?: IterationType): NODE | undefined;
212
238
  /**
213
239
  * Time complexity: O(n)
214
240
  * Space complexity: O(n)
@@ -482,6 +482,35 @@ class BST extends binary_tree_1.BinaryTree {
482
482
  }
483
483
  return ans;
484
484
  }
485
+ /**
486
+ * Time Complexity: O(log n)
487
+ * Space Complexity: O(1)
488
+ */
489
+ /**
490
+ * Time Complexity: O(log n)
491
+ * Space Complexity: O(1)
492
+ *
493
+ * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
494
+ * callback function.
495
+ * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
496
+ * that you want to search for in the binary search tree. It can be of any type that is compatible
497
+ * with the type of nodes in the tree.
498
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
499
+ * the tree. It is used to determine whether a node matches the given identifier. The `callback`
500
+ * function should take a node as its parameter and return a value that can be compared to the
501
+ * `identifier` parameter.
502
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
503
+ * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
504
+ * using the `ensureNode` method. If it is not provided, the `root`
505
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
506
+ * be performed when searching for nodes in the binary search tree. It is an optional parameter and
507
+ * its default value is taken from the `iterationType` property of the class.
508
+ * @returns The method is returning a value of type `NODE | null | undefined`.
509
+ */
510
+ getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
511
+ var _a;
512
+ return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
513
+ }
485
514
  /**
486
515
  * Time complexity: O(n)
487
516
  * Space complexity: O(n)
@@ -1,4 +1,4 @@
1
- import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
1
+ import type { BinaryTreeDeleteResult, BTNCallback, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
2
2
  import { CRUD, RBTNColor } from '../../types';
3
3
  import { BST, BSTNode } from './bst';
4
4
  import { IBinaryTree } from '../../interfaces';
@@ -39,12 +39,6 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
39
39
  * should compare keys and
40
40
  */
41
41
  constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: RBTreeOptions<K>);
42
- protected _SENTINEL: NODE;
43
- /**
44
- * The function returns the value of the _SENTINEL property.
45
- * @returns The method is returning the value of the `_SENTINEL` property.
46
- */
47
- get SENTINEL(): NODE;
48
42
  protected _root: NODE | undefined;
49
43
  /**
50
44
  * The function returns the root node of a tree or undefined if there is no root.
@@ -101,46 +95,6 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
101
95
  * @returns {boolean} - `true` if the object is a Red-Black Tree node, `false` otherwise.
102
96
  */
103
97
  isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
104
- /**
105
- * Time Complexity: O(1)
106
- * Space Complexity: O(1)
107
- */
108
- /**
109
- * Time Complexity: O(1)
110
- * Space Complexity: O(1)
111
- *
112
- * The function checks if a given node is a real node in a Red-Black Tree.
113
- * @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
114
- * it can either be of type `NODE` or `undefined`.
115
- * @returns a boolean value.
116
- */
117
- isRealNode(node: NODE | undefined): node is NODE;
118
- /**
119
- * Time Complexity: O(log n)
120
- * Space Complexity: O(1)
121
- */
122
- /**
123
- * Time Complexity: O(log n)
124
- * Space Complexity: O(1)
125
- *
126
- * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
127
- * callback function.
128
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
129
- * that you want to search for in the binary search tree. It can be of any type that is compatible
130
- * with the type of nodes in the tree.
131
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
132
- * the tree. It is used to determine whether a node matches the given identifier. The `callback`
133
- * function should take a node as its parameter and return a value that can be compared to the
134
- * `identifier` parameter.
135
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
136
- * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
137
- * using the `ensureNode` method. If it is not provided, the `root`
138
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
139
- * be performed when searching for nodes in the binary search tree. It is an optional parameter and
140
- * its default value is taken from the `iterationType` property of the class.
141
- * @returns The method is returning a value of type `NODE | null | undefined`.
142
- */
143
- getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?: IterationType): NODE | null | undefined;
144
98
  /**
145
99
  * Time Complexity: O(1)
146
100
  * Space Complexity: O(1)
@@ -47,19 +47,11 @@ class RedBlackTree extends bst_1.BST {
47
47
  */
48
48
  constructor(keysOrNodesOrEntries = [], options) {
49
49
  super([], options);
50
- this._SENTINEL = new RedBlackTreeNode(NaN);
51
- this._root = this.SENTINEL;
50
+ this._root = this.NIL;
52
51
  if (keysOrNodesOrEntries) {
53
52
  this.addMany(keysOrNodesOrEntries);
54
53
  }
55
54
  }
56
- /**
57
- * The function returns the value of the _SENTINEL property.
58
- * @returns The method is returning the value of the `_SENTINEL` property.
59
- */
60
- get SENTINEL() {
61
- return this._SENTINEL;
62
- }
63
55
  /**
64
56
  * The function returns the root node of a tree or undefined if there is no root.
65
57
  * @returns The root node of the tree structure, or undefined if there is no root node.
@@ -147,53 +139,6 @@ class RedBlackTree extends bst_1.BST {
147
139
  isNode(keyOrNodeOrEntry) {
148
140
  return keyOrNodeOrEntry instanceof RedBlackTreeNode;
149
141
  }
150
- /**
151
- * Time Complexity: O(1)
152
- * Space Complexity: O(1)
153
- */
154
- /**
155
- * Time Complexity: O(1)
156
- * Space Complexity: O(1)
157
- *
158
- * The function checks if a given node is a real node in a Red-Black Tree.
159
- * @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
160
- * it can either be of type `NODE` or `undefined`.
161
- * @returns a boolean value.
162
- */
163
- isRealNode(node) {
164
- if (node === this.SENTINEL || node === undefined)
165
- return false;
166
- return node instanceof RedBlackTreeNode;
167
- }
168
- /**
169
- * Time Complexity: O(log n)
170
- * Space Complexity: O(1)
171
- */
172
- /**
173
- * Time Complexity: O(log n)
174
- * Space Complexity: O(1)
175
- *
176
- * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
177
- * callback function.
178
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
179
- * that you want to search for in the binary search tree. It can be of any type that is compatible
180
- * with the type of nodes in the tree.
181
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
182
- * the tree. It is used to determine whether a node matches the given identifier. The `callback`
183
- * function should take a node as its parameter and return a value that can be compared to the
184
- * `identifier` parameter.
185
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
186
- * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
187
- * using the `ensureNode` method. If it is not provided, the `root`
188
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
189
- * be performed when searching for nodes in the binary search tree. It is an optional parameter and
190
- * its default value is taken from the `iterationType` property of the class.
191
- * @returns The method is returning a value of type `NODE | null | undefined`.
192
- */
193
- getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
194
- var _a;
195
- return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
196
- }
197
142
  /**
198
143
  * Time Complexity: O(1)
199
144
  * Space Complexity: O(1)
@@ -207,7 +152,7 @@ class RedBlackTree extends bst_1.BST {
207
152
  */
208
153
  clear() {
209
154
  super.clear();
210
- this._root = this.SENTINEL;
155
+ this._root = this.NIL;
211
156
  }
212
157
  /**
213
158
  * Time Complexity: O(log n)
@@ -369,10 +314,10 @@ class RedBlackTree extends bst_1.BST {
369
314
  while (this.isRealNode(current)) {
370
315
  parent = current;
371
316
  if (node.key < current.key) {
372
- current = (_a = current.left) !== null && _a !== void 0 ? _a : this.SENTINEL;
317
+ current = (_a = current.left) !== null && _a !== void 0 ? _a : this.NIL;
373
318
  }
374
319
  else if (node.key > current.key) {
375
- current = (_b = current.right) !== null && _b !== void 0 ? _b : this.SENTINEL;
320
+ current = (_b = current.right) !== null && _b !== void 0 ? _b : this.NIL;
376
321
  }
377
322
  else {
378
323
  this._replaceNode(current, node);
@@ -389,8 +334,8 @@ class RedBlackTree extends bst_1.BST {
389
334
  else {
390
335
  parent.right = node;
391
336
  }
392
- node.left = this.SENTINEL;
393
- node.right = this.SENTINEL;
337
+ node.left = this.NIL;
338
+ node.right = this.NIL;
394
339
  node.color = 'RED';
395
340
  this._insertFixup(node);
396
341
  return 'CREATED';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stack-typed",
3
- "version": "1.51.1",
3
+ "version": "1.51.3",
4
4
  "description": "Stack. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -65,6 +65,6 @@
65
65
  "typescript": "^4.9.5"
66
66
  },
67
67
  "dependencies": {
68
- "data-structure-typed": "^1.51.0"
68
+ "data-structure-typed": "^1.51.3"
69
69
  }
70
70
  }
@@ -191,6 +191,16 @@ export class BinaryTree<
191
191
  return this._size;
192
192
  }
193
193
 
194
+ protected _NIL: NODE = new BinaryTreeNode<K, V>(NaN as K) as unknown as NODE;
195
+
196
+ /**
197
+ * The function returns the value of the _NIL property.
198
+ * @returns The method is returning the value of the `_NIL` property.
199
+ */
200
+ get NIL(): NODE {
201
+ return this._NIL;
202
+ }
203
+
194
204
  /**
195
205
  * Creates a new instance of BinaryTreeNode with the given key and value.
196
206
  * @param {K} key - The key for the new node.
@@ -281,6 +291,15 @@ export class BinaryTree<
281
291
  }
282
292
  }
283
293
 
294
+ /**
295
+ * The function checks if a given node is a real node or null.
296
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
297
+ * @returns a boolean value.
298
+ */
299
+ isNodeOrNull(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
300
+ return this.isRealNode(node) || node === null;
301
+ }
302
+
284
303
  /**
285
304
  * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
286
305
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
@@ -290,16 +309,6 @@ export class BinaryTree<
290
309
  return keyOrNodeOrEntry instanceof BinaryTreeNode;
291
310
  }
292
311
 
293
- /**
294
- * The function checks if a given value is an entry in a binary tree node.
295
- * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
296
- * two type parameters V and NODE, representing the value and node type respectively.
297
- * @returns a boolean value.
298
- */
299
- isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V> {
300
- return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
301
- }
302
-
303
312
  /**
304
313
  * The function checks if a given node is a real node by verifying if it is an instance of
305
314
  * BinaryTreeNode and its key is not NaN.
@@ -307,7 +316,8 @@ export class BinaryTree<
307
316
  * @returns a boolean value.
308
317
  */
309
318
  isRealNode(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE {
310
- return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
319
+ if (!this.isNode(node)) return false;
320
+ return node !== this.NIL;
311
321
  }
312
322
 
313
323
  /**
@@ -316,16 +326,17 @@ export class BinaryTree<
316
326
  * @returns a boolean value.
317
327
  */
318
328
  isNIL(node: KeyOrNodeOrEntry<K, V, NODE>) {
319
- return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
329
+ return node === this.NIL;
320
330
  }
321
331
 
322
332
  /**
323
- * The function checks if a given node is a real node or null.
324
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
333
+ * The function checks if a given value is an entry in a binary tree node.
334
+ * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
335
+ * two type parameters V and NODE, representing the value and node type respectively.
325
336
  * @returns a boolean value.
326
337
  */
327
- isNodeOrNull(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
328
- return this.isRealNode(node) || node === null;
338
+ isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V> {
339
+ return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
329
340
  }
330
341
 
331
342
  /**
@@ -612,9 +623,9 @@ export class BinaryTree<
612
623
  ans.push(cur);
613
624
  if (onlyOne) return;
614
625
  }
615
- if (!cur.left && !cur.right) return;
616
- cur.left && dfs(cur.left);
617
- cur.right && dfs(cur.right);
626
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
627
+ this.isRealNode(cur.left) && dfs(cur.left);
628
+ this.isRealNode(cur.right) && dfs(cur.right);
618
629
  };
619
630
 
620
631
  dfs(beginRoot);
@@ -622,13 +633,13 @@ export class BinaryTree<
622
633
  const stack = [beginRoot];
623
634
  while (stack.length > 0) {
624
635
  const cur = stack.pop();
625
- if (cur) {
636
+ if (this.isRealNode(cur)) {
626
637
  if (callback(cur) === identifier) {
627
638
  ans.push(cur);
628
639
  if (onlyOne) return ans;
629
640
  }
630
- cur.left && stack.push(cur.left);
631
- cur.right && stack.push(cur.right);
641
+ this.isRealNode(cur.left) && stack.push(cur.left);
642
+ this.isRealNode(cur.right) && stack.push(cur.right);
632
643
  }
633
644
  }
634
645
  }
@@ -689,9 +700,6 @@ export class BinaryTree<
689
700
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
690
701
  iterationType: IterationType = this.iterationType
691
702
  ): NODE | null | undefined {
692
- if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
693
- callback = (node => node) as C;
694
-
695
703
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
696
704
  }
697
705
 
@@ -793,9 +801,6 @@ export class BinaryTree<
793
801
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
794
802
  iterationType: IterationType = this.iterationType
795
803
  ): V | undefined {
796
- if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
797
- callback = (node => node) as C;
798
-
799
804
  return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
800
805
  }
801
806
 
@@ -13,7 +13,7 @@ import type {
13
13
  BTNodePureExemplar,
14
14
  KeyOrNodeOrEntry
15
15
  } from '../../types';
16
- import { BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
16
+ import { BSTNKeyOrNode, BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
17
17
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
18
18
  import { IBinaryTree } from '../../interfaces';
19
19
  import { Queue } from '../queue';
@@ -543,6 +543,41 @@ export class BST<
543
543
  return ans;
544
544
  }
545
545
 
546
+ /**
547
+ * Time Complexity: O(log n)
548
+ * Space Complexity: O(1)
549
+ */
550
+
551
+ /**
552
+ * Time Complexity: O(log n)
553
+ * Space Complexity: O(1)
554
+ *
555
+ * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
556
+ * callback function.
557
+ * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
558
+ * that you want to search for in the binary search tree. It can be of any type that is compatible
559
+ * with the type of nodes in the tree.
560
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
561
+ * the tree. It is used to determine whether a node matches the given identifier. The `callback`
562
+ * function should take a node as its parameter and return a value that can be compared to the
563
+ * `identifier` parameter.
564
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
565
+ * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
566
+ * using the `ensureNode` method. If it is not provided, the `root`
567
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
568
+ * be performed when searching for nodes in the binary search tree. It is an optional parameter and
569
+ * its default value is taken from the `iterationType` property of the class.
570
+ * @returns The method is returning a value of type `NODE | null | undefined`.
571
+ */
572
+ override getNode<C extends BTNCallback<NODE>>(
573
+ identifier: ReturnType<C> | undefined,
574
+ callback: C = this._DEFAULT_CALLBACK as C,
575
+ beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
576
+ iterationType: IterationType = this.iterationType
577
+ ): NODE | undefined {
578
+ return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
579
+ }
580
+
546
581
  /**
547
582
  * Time complexity: O(n)
548
583
  * Space complexity: O(n)
@@ -1,8 +1,6 @@
1
1
  import type {
2
2
  BinaryTreeDeleteResult,
3
- BSTNKeyOrNode,
4
3
  BTNCallback,
5
- IterationType,
6
4
  KeyOrNodeOrEntry,
7
5
  RBTreeOptions,
8
6
  RedBlackTreeNested,
@@ -73,23 +71,13 @@ export class RedBlackTree<
73
71
  constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: RBTreeOptions<K>) {
74
72
  super([], options);
75
73
 
76
- this._root = this.SENTINEL;
74
+ this._root = this.NIL;
77
75
 
78
76
  if (keysOrNodesOrEntries) {
79
77
  this.addMany(keysOrNodesOrEntries);
80
78
  }
81
79
  }
82
80
 
83
- protected _SENTINEL: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
84
-
85
- /**
86
- * The function returns the value of the _SENTINEL property.
87
- * @returns The method is returning the value of the `_SENTINEL` property.
88
- */
89
- get SENTINEL(): NODE {
90
- return this._SENTINEL;
91
- }
92
-
93
81
  protected override _root: NODE | undefined;
94
82
 
95
83
  /**
@@ -184,60 +172,6 @@ export class RedBlackTree<
184
172
  return keyOrNodeOrEntry instanceof RedBlackTreeNode;
185
173
  }
186
174
 
187
- /**
188
- * Time Complexity: O(1)
189
- * Space Complexity: O(1)
190
- */
191
-
192
- /**
193
- * Time Complexity: O(1)
194
- * Space Complexity: O(1)
195
- *
196
- * The function checks if a given node is a real node in a Red-Black Tree.
197
- * @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
198
- * it can either be of type `NODE` or `undefined`.
199
- * @returns a boolean value.
200
- */
201
- override isRealNode(node: NODE | undefined): node is NODE {
202
- if (node === this.SENTINEL || node === undefined) return false;
203
- return node instanceof RedBlackTreeNode;
204
- }
205
-
206
- /**
207
- * Time Complexity: O(log n)
208
- * Space Complexity: O(1)
209
- */
210
-
211
- /**
212
- * Time Complexity: O(log n)
213
- * Space Complexity: O(1)
214
- *
215
- * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
216
- * callback function.
217
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
218
- * that you want to search for in the binary search tree. It can be of any type that is compatible
219
- * with the type of nodes in the tree.
220
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
221
- * the tree. It is used to determine whether a node matches the given identifier. The `callback`
222
- * function should take a node as its parameter and return a value that can be compared to the
223
- * `identifier` parameter.
224
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
225
- * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
226
- * using the `ensureNode` method. If it is not provided, the `root`
227
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
228
- * be performed when searching for nodes in the binary search tree. It is an optional parameter and
229
- * its default value is taken from the `iterationType` property of the class.
230
- * @returns The method is returning a value of type `NODE | null | undefined`.
231
- */
232
- override getNode<C extends BTNCallback<NODE>>(
233
- identifier: ReturnType<C> | undefined,
234
- callback: C = this._DEFAULT_CALLBACK as C,
235
- beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
236
- iterationType: IterationType = this.iterationType
237
- ): NODE | null | undefined {
238
- return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
239
- }
240
-
241
175
  /**
242
176
  * Time Complexity: O(1)
243
177
  * Space Complexity: O(1)
@@ -252,7 +186,7 @@ export class RedBlackTree<
252
186
  */
253
187
  override clear() {
254
188
  super.clear();
255
- this._root = this.SENTINEL;
189
+ this._root = this.NIL;
256
190
  }
257
191
 
258
192
  /**
@@ -430,9 +364,9 @@ export class RedBlackTree<
430
364
  while (this.isRealNode(current)) {
431
365
  parent = current;
432
366
  if (node.key < current.key) {
433
- current = current.left ?? this.SENTINEL;
367
+ current = current.left ?? this.NIL;
434
368
  } else if (node.key > current.key) {
435
- current = current.right ?? this.SENTINEL;
369
+ current = current.right ?? this.NIL;
436
370
  } else {
437
371
  this._replaceNode(current, node);
438
372
  return 'UPDATED';
@@ -449,8 +383,8 @@ export class RedBlackTree<
449
383
  parent.right = node;
450
384
  }
451
385
 
452
- node.left = this.SENTINEL;
453
- node.right = this.SENTINEL;
386
+ node.left = this.NIL;
387
+ node.right = this.NIL;
454
388
  node.color = 'RED';
455
389
 
456
390
  this._insertFixup(node);