data-structure-typed 1.51.1 → 1.51.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.
Files changed (30) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +158 -146
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +16 -10
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js +31 -25
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/bst.d.ts +27 -1
  9. package/dist/cjs/data-structures/binary-tree/bst.js +29 -0
  10. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +1 -47
  12. package/dist/cjs/data-structures/binary-tree/rb-tree.js +6 -61
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  14. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +16 -10
  15. package/dist/mjs/data-structures/binary-tree/binary-tree.js +31 -25
  16. package/dist/mjs/data-structures/binary-tree/bst.d.ts +27 -1
  17. package/dist/mjs/data-structures/binary-tree/bst.js +28 -0
  18. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +1 -47
  19. package/dist/mjs/data-structures/binary-tree/rb-tree.js +6 -60
  20. package/dist/umd/data-structure-typed.js +66 -86
  21. package/dist/umd/data-structure-typed.min.js +2 -2
  22. package/dist/umd/data-structure-typed.min.js.map +1 -1
  23. package/package.json +6 -6
  24. package/src/data-structures/binary-tree/binary-tree.ts +33 -28
  25. package/src/data-structures/binary-tree/bst.ts +36 -1
  26. package/src/data-structures/binary-tree/rb-tree.ts +6 -72
  27. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -0
  28. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +4 -0
  29. package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
  30. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +40 -40
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.51.1",
3
+ "version": "1.51.2",
4
4
  "description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
@@ -66,11 +66,11 @@
66
66
  "@typescript-eslint/eslint-plugin": "^6.7.4",
67
67
  "@typescript-eslint/parser": "^6.7.4",
68
68
  "auto-changelog": "^2.4.0",
69
- "avl-tree-typed": "^1.51.0",
69
+ "avl-tree-typed": "^1.51.1",
70
70
  "benchmark": "^2.1.4",
71
- "binary-tree-typed": "^1.51.0",
72
- "bst-typed": "^1.51.0",
73
- "data-structure-typed": "^1.51.0",
71
+ "binary-tree-typed": "^1.51.1",
72
+ "bst-typed": "^1.51.1",
73
+ "data-structure-typed": "^1.51.1",
74
74
  "dependency-cruiser": "^14.1.0",
75
75
  "doctoc": "^2.2.1",
76
76
  "eslint": "^8.50.0",
@@ -79,7 +79,7 @@
79
79
  "eslint-import-resolver-typescript": "^3.6.1",
80
80
  "eslint-plugin-import": "^2.28.1",
81
81
  "fast-glob": "^3.3.1",
82
- "heap-typed": "^1.51.0",
82
+ "heap-typed": "^1.51.1",
83
83
  "istanbul-badges-readme": "^1.8.5",
84
84
  "jest": "^29.7.0",
85
85
  "js-sdsl": "^4.4.2",
@@ -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);
@@ -12,6 +12,10 @@ suite
12
12
  avlTree.clear();
13
13
  for (let i = 0; i < randomArray.length; i++) avlTree.add(i);
14
14
  })
15
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} add randomly`, () => {
16
+ avlTree.clear();
17
+ for (let i = 0; i < randomArray.length; i++) avlTree.add(randomArray[i]);
18
+ })
15
19
  .add(`${HUNDRED_THOUSAND.toLocaleString()} get`, () => {
16
20
  for (let i = 0; i < randomArray.length; i++) avlTree.get(randomArray[i]);
17
21
  })
@@ -15,6 +15,10 @@ suite
15
15
  rbTree.clear();
16
16
  for (let i = 0; i < randomArray.length; i++) rbTree.add(i);
17
17
  })
18
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} add randomly`, () => {
19
+ rbTree.clear();
20
+ for (let i = 0; i < randomArray.length; i++) rbTree.add(randomArray[i]);
21
+ })
18
22
  .add(`${HUNDRED_THOUSAND.toLocaleString()} get`, () => {
19
23
  for (let i = 0; i < randomArray.length; i++) rbTree.get(randomArray[i]);
20
24
  })
@@ -20,8 +20,8 @@ describe('Overall BinaryTree Test', () => {
20
20
  leftMost?.key === 1; // true
21
21
  expect(leftMost?.key).toBe(1);
22
22
  bst.delete(6);
23
- bst.getNode(6); // null
24
- expect(bst.getNode(6)).toBeNull();
23
+ bst.getNode(6); // undefined
24
+ expect(bst.getNode(6)).toBe(undefined);
25
25
  bst.isAVLBalanced(); // true or false
26
26
  expect(bst.isAVLBalanced()).toBe(true);
27
27
  const bfsIDs: number[] = [];
@@ -74,7 +74,7 @@ describe('RedBlackTree 1', () => {
74
74
 
75
75
  it('should handle an empty rbTree', () => {
76
76
  const minNode = rbTree.getLeftMost(rbTree.root);
77
- expect(minNode).toBe(rbTree.SENTINEL);
77
+ expect(minNode).toBe(rbTree.NIL);
78
78
  });
79
79
  });
80
80
 
@@ -92,7 +92,7 @@ describe('RedBlackTree 1', () => {
92
92
 
93
93
  it('should handle an empty rbTree', () => {
94
94
  const maxNode = rbTree.getRightMost(rbTree.root);
95
- expect(maxNode).toBe(rbTree.SENTINEL);
95
+ expect(maxNode).toBe(rbTree.NIL);
96
96
  });
97
97
  });
98
98
 
@@ -116,7 +116,7 @@ describe('RedBlackTree 1', () => {
116
116
 
117
117
  const node = rbTree.getNode(10);
118
118
  const successorNode = rbTree.getSuccessor(node!);
119
- // TODO not sure if it should be undefined or rbTree.SENTINEL
119
+ // TODO not sure if it should be undefined or rbTree.NIL
120
120
  expect(successorNode).toBe(undefined);
121
121
  });
122
122
  });
@@ -141,7 +141,7 @@ describe('RedBlackTree 1', () => {
141
141
 
142
142
  const node = rbTree.getNode(20);
143
143
  const predecessorNode = rbTree.getPredecessor(node!);
144
- // TODO not sure if it should be rbTree.SENTINEL or something else.
144
+ // TODO not sure if it should be rbTree.NIL or something else.
145
145
  expect(predecessorNode).toBe(rbTree.getNode(20));
146
146
  });
147
147
  });
@@ -279,28 +279,28 @@ describe('RedBlackTree 2', () => {
279
279
  expect(node5F?.parent).toBe(node10F);
280
280
  expect(node15F?.key).toBe(15);
281
281
  expect(node15F?.color).toBe('RED');
282
- expect(node15F?.left).toBe(rbTree.SENTINEL);
283
- expect(node15F?.right).toBe(rbTree.SENTINEL);
282
+ expect(node15F?.left).toBe(rbTree.NIL);
283
+ expect(node15F?.right).toBe(rbTree.NIL);
284
284
  expect(node15F?.parent).toBe(node20F);
285
285
  expect(node21F?.key).toBe(21);
286
286
  expect(node21F?.color).toBe('RED');
287
- expect(node21F?.left).toBe(rbTree.SENTINEL);
288
- expect(node21F?.right).toBe(rbTree.SENTINEL);
287
+ expect(node21F?.left).toBe(rbTree.NIL);
288
+ expect(node21F?.right).toBe(rbTree.NIL);
289
289
  expect(node21F?.parent).toBe(node20F);
290
290
  expect(node6F?.key).toBe(6);
291
291
  expect(node6F?.color).toBe('RED');
292
- expect(node6F?.left).toBe(rbTree.SENTINEL);
293
- expect(node6F?.right).toBe(rbTree.SENTINEL);
292
+ expect(node6F?.left).toBe(rbTree.NIL);
293
+ expect(node6F?.right).toBe(rbTree.NIL);
294
294
  expect(node6F?.parent).toBe(node5F);
295
295
  expect(node2F?.key).toBe(2);
296
296
  expect(node2F?.color).toBe('RED');
297
- expect(node2F?.left).toBe(rbTree.SENTINEL);
298
- expect(node2F?.right).toBe(rbTree.SENTINEL);
297
+ expect(node2F?.left).toBe(rbTree.NIL);
298
+ expect(node2F?.right).toBe(rbTree.NIL);
299
299
  expect(node2F?.parent).toBe(node5F);
300
300
  expect(node15F?.key).toBe(15);
301
301
  expect(node15F?.color).toBe('RED');
302
- expect(node15F?.left).toBe(rbTree.SENTINEL);
303
- expect(node15F?.right).toBe(rbTree.SENTINEL);
302
+ expect(node15F?.left).toBe(rbTree.NIL);
303
+ expect(node15F?.right).toBe(rbTree.NIL);
304
304
  expect(node15F?.parent).toBe(node20F);
305
305
  rbTree.delete(5);
306
306
  node10F = rbTree.getNode(10);
@@ -323,28 +323,28 @@ describe('RedBlackTree 2', () => {
323
323
  expect(node5F).toBe(undefined);
324
324
  expect(node15F?.key).toBe(15);
325
325
  expect(node15F?.color).toBe('RED');
326
- expect(node15F?.left).toBe(rbTree.SENTINEL);
327
- expect(node15F?.right).toBe(rbTree.SENTINEL);
326
+ expect(node15F?.left).toBe(rbTree.NIL);
327
+ expect(node15F?.right).toBe(rbTree.NIL);
328
328
  expect(node15F?.parent).toBe(node20F);
329
329
  expect(node21F?.key).toBe(21);
330
330
  expect(node21F?.color).toBe('RED');
331
- expect(node21F?.left).toBe(rbTree.SENTINEL);
332
- expect(node21F?.right).toBe(rbTree.SENTINEL);
331
+ expect(node21F?.left).toBe(rbTree.NIL);
332
+ expect(node21F?.right).toBe(rbTree.NIL);
333
333
  expect(node21F?.parent).toBe(node20F);
334
334
  expect(node6F?.key).toBe(6);
335
335
  expect(node6F?.color).toBe('BLACK');
336
336
  expect(node6F?.left).toBe(node2F);
337
- expect(node6F?.right).toBe(rbTree.SENTINEL);
337
+ expect(node6F?.right).toBe(rbTree.NIL);
338
338
  expect(node6F?.parent).toBe(node10F);
339
339
  expect(node2F?.key).toBe(2);
340
340
  expect(node2F?.color).toBe('RED');
341
- expect(node2F?.left).toBe(rbTree.SENTINEL);
342
- expect(node2F?.right).toBe(rbTree.SENTINEL);
341
+ expect(node2F?.left).toBe(rbTree.NIL);
342
+ expect(node2F?.right).toBe(rbTree.NIL);
343
343
  expect(node2F?.parent).toBe(node6F);
344
344
  expect(node15F?.key).toBe(15);
345
345
  expect(node15F?.color).toBe('RED');
346
- expect(node15F?.left).toBe(rbTree.SENTINEL);
347
- expect(node15F?.right).toBe(rbTree.SENTINEL);
346
+ expect(node15F?.left).toBe(rbTree.NIL);
347
+ expect(node15F?.right).toBe(rbTree.NIL);
348
348
  expect(node15F?.parent).toBe(node20F);
349
349
  rbTree.delete(20);
350
350
  node10F = rbTree.getNode(10);
@@ -363,28 +363,28 @@ describe('RedBlackTree 2', () => {
363
363
  expect(node5F).toBe(undefined);
364
364
  expect(node15F?.key).toBe(15);
365
365
  expect(node15F?.color).toBe('RED');
366
- expect(node15F?.left).toBe(rbTree.SENTINEL);
367
- expect(node15F?.right).toBe(rbTree.SENTINEL);
366
+ expect(node15F?.left).toBe(rbTree.NIL);
367
+ expect(node15F?.right).toBe(rbTree.NIL);
368
368
  expect(node15F?.parent).toBe(node21F);
369
369
  expect(node21F?.key).toBe(21);
370
370
  expect(node21F?.color).toBe('BLACK');
371
371
  expect(node21F?.left).toBe(node15F);
372
- expect(node21F?.right).toBe(rbTree.SENTINEL);
372
+ expect(node21F?.right).toBe(rbTree.NIL);
373
373
  expect(node21F?.parent).toBe(node10F);
374
374
  expect(node6F?.key).toBe(6);
375
375
  expect(node6F?.color).toBe('BLACK');
376
376
  expect(node6F?.left).toBe(node2F);
377
- expect(node6F?.right).toBe(rbTree.SENTINEL);
377
+ expect(node6F?.right).toBe(rbTree.NIL);
378
378
  expect(node6F?.parent).toBe(node10F);
379
379
  expect(node2F?.key).toBe(2);
380
380
  expect(node2F?.color).toBe('RED');
381
- expect(node2F?.left).toBe(rbTree.SENTINEL);
382
- expect(node2F?.right).toBe(rbTree.SENTINEL);
381
+ expect(node2F?.left).toBe(rbTree.NIL);
382
+ expect(node2F?.right).toBe(rbTree.NIL);
383
383
  expect(node2F?.parent).toBe(node6F);
384
384
  expect(node15F?.key).toBe(15);
385
385
  expect(node15F?.color).toBe('RED');
386
- expect(node15F?.left).toBe(rbTree.SENTINEL);
387
- expect(node15F?.right).toBe(rbTree.SENTINEL);
386
+ expect(node15F?.left).toBe(rbTree.NIL);
387
+ expect(node15F?.right).toBe(rbTree.NIL);
388
388
  expect(node15F?.parent).toBe(node21F);
389
389
  });
390
390
 
@@ -394,8 +394,8 @@ describe('RedBlackTree 2', () => {
394
394
  rbTree.add(5);
395
395
  rbTree.add(15);
396
396
  const node15F = rbTree.getNode(15);
397
- expect(node15F?.left).toBe(rbTree.SENTINEL);
398
- expect(node15F?.right).toBe(rbTree.SENTINEL);
397
+ expect(node15F?.left).toBe(rbTree.NIL);
398
+ expect(node15F?.right).toBe(rbTree.NIL);
399
399
  expect(node15F?.parent).toBe(rbTree.getNode(5));
400
400
 
401
401
  rbTree.add(25);
@@ -410,8 +410,8 @@ describe('RedBlackTree 2', () => {
410
410
  rbTree.add(155);
411
411
  rbTree.add(225);
412
412
  const node225F = rbTree.getNode(225);
413
- expect(node225F?.left).toBe(rbTree.SENTINEL);
414
- expect(node225F?.right).toBe(rbTree.SENTINEL);
413
+ expect(node225F?.left).toBe(rbTree.NIL);
414
+ expect(node225F?.right).toBe(rbTree.NIL);
415
415
  expect(node225F?.parent?.key).toBe(155);
416
416
  rbTree.add(7);
417
417
  isDebug && rbTree.print();
@@ -438,14 +438,14 @@ describe('RedBlackTree 2', () => {
438
438
  const node50 = rbTree.getNode(50);
439
439
  expect(node50?.key).toBe(50);
440
440
  expect(node50?.left?.key).toBe(33);
441
- expect(node50?.right).toBe(rbTree.SENTINEL);
441
+ expect(node50?.right).toBe(rbTree.NIL);
442
442
  const node15Fo = rbTree.getNode(15);
443
443
 
444
444
  expect(node15Fo?.key).toBe(15);
445
- expect(node15Fo?.left).toBe(rbTree.SENTINEL);
445
+ expect(node15Fo?.left).toBe(rbTree.NIL);
446
446
  const node225S = rbTree.getNode(225);
447
- expect(node225S?.left).toBe(rbTree.SENTINEL);
448
- expect(node225S?.right).toBe(rbTree.SENTINEL);
447
+ expect(node225S?.left).toBe(rbTree.NIL);
448
+ expect(node225S?.right).toBe(rbTree.NIL);
449
449
  expect(node225S?.parent?.key).toBe(155);
450
450
  // TODO
451
451
  // expect(rbTree.getNode(0)).toBe(undefined);
@@ -512,7 +512,7 @@ describe('RedBlackTree 2', () => {
512
512
  rbTree.delete(getRandomInt(-100, 1000));
513
513
  }
514
514
 
515
- // TODO there is a bug when dfs the rbTree with SENTINEL node
515
+ // TODO there is a bug when dfs the rbTree with NIL node
516
516
  // expect(rbTree.isBST()).toBe(true);
517
517
  });
518
518
  const { HUNDRED_THOUSAND } = magnitude;