data-structure-typed 1.48.9 → 1.49.0

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 (35) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -1
  3. package/dist/cjs/data-structures/binary-tree/avl-tree.js +9 -0
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +15 -6
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js +19 -15
  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 +8 -1
  9. package/dist/cjs/data-structures/binary-tree/bst.js +9 -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 +10 -16
  12. package/dist/cjs/data-structures/binary-tree/rb-tree.js +16 -29
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +8 -1
  15. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +9 -0
  16. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  17. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -1
  18. package/dist/mjs/data-structures/binary-tree/avl-tree.js +9 -0
  19. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +15 -6
  20. package/dist/mjs/data-structures/binary-tree/binary-tree.js +19 -15
  21. package/dist/mjs/data-structures/binary-tree/bst.d.ts +8 -1
  22. package/dist/mjs/data-structures/binary-tree/bst.js +9 -0
  23. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +10 -16
  24. package/dist/mjs/data-structures/binary-tree/rb-tree.js +16 -28
  25. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +8 -1
  26. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +9 -0
  27. package/dist/umd/data-structure-typed.js +62 -44
  28. package/dist/umd/data-structure-typed.min.js +2 -2
  29. package/dist/umd/data-structure-typed.min.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/data-structures/binary-tree/avl-tree.ts +11 -1
  32. package/src/data-structures/binary-tree/binary-tree.ts +22 -23
  33. package/src/data-structures/binary-tree/bst.ts +11 -1
  34. package/src/data-structures/binary-tree/rb-tree.ts +18 -32
  35. package/src/data-structures/binary-tree/tree-multimap.ts +17 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.48.9",
3
+ "version": "1.49.0",
4
4
  "description": "Data Structures of Javascript & TypeScript. 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",
@@ -14,7 +14,7 @@ import type {
14
14
  BSTNodeKeyOrNode,
15
15
  BTNodeExemplar
16
16
  } from '../../types';
17
- import { BTNCallback } from '../../types';
17
+ import { BTNCallback, BTNodeKeyOrNode } from '../../types';
18
18
  import { IBinaryTree } from '../../interfaces';
19
19
 
20
20
  export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
@@ -90,6 +90,16 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
90
90
  return exemplar instanceof AVLTreeNode;
91
91
  }
92
92
 
93
+ /**
94
+ * The function "isNotNodeInstance" checks if a potential key is a K.
95
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
96
+ * data type.
97
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
98
+ */
99
+ override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
100
+ return !(potentialKey instanceof AVLTreeNode)
101
+ }
102
+
93
103
  /**
94
104
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
95
105
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -1236,8 +1236,8 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1236
1236
  * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
1237
1237
  * @returns a boolean value.
1238
1238
  */
1239
- isRealNode(node: any): node is N {
1240
- return node instanceof BinaryTreeNode && node.key.toString() !== 'NaN';
1239
+ isRealNode(node: BTNodeExemplar<K, V, N>): node is N {
1240
+ return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
1241
1241
  }
1242
1242
 
1243
1243
  /**
@@ -1245,8 +1245,8 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1245
1245
  * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
1246
1246
  * @returns a boolean value.
1247
1247
  */
1248
- isNIL(node: any) {
1249
- return node instanceof BinaryTreeNode && node.key.toString() === 'NaN';
1248
+ isNIL(node: BTNodeExemplar<K, V, N>) {
1249
+ return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
1250
1250
  }
1251
1251
 
1252
1252
  /**
@@ -1254,12 +1254,12 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1254
1254
  * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
1255
1255
  * @returns a boolean value.
1256
1256
  */
1257
- isNodeOrNull(node: any): node is N | null {
1257
+ isNodeOrNull(node: BTNodeExemplar<K, V, N>): node is N | null {
1258
1258
  return this.isRealNode(node) || node === null;
1259
1259
  }
1260
1260
 
1261
1261
  /**
1262
- * The function "isNotNodeInstance" checks if a potential key is a number.
1262
+ * The function "isNotNodeInstance" checks if a potential key is a K.
1263
1263
  * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
1264
1264
  * data type.
1265
1265
  * @returns a boolean value indicating whether the potentialKey is of type number or not.
@@ -1606,26 +1606,24 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1606
1606
  }
1607
1607
 
1608
1608
  /**
1609
- * Time complexity: O(n)
1610
- * Space complexity: O(n)
1609
+ * Time Complexity: O(log n)
1610
+ * Space Complexity: O(1)
1611
1611
  */
1612
1612
 
1613
- getPredecessor(node: N): N;
1614
-
1615
1613
  /**
1616
- * The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
1617
- * @param {K | N | null | undefined} node - The `node` parameter can be of type `K`, `N`,
1618
- * `null`, or `undefined`.
1619
- * @returns The function `getPredecessor` returns a value of type `N | undefined`.
1614
+ * Time Complexity: O(log n)
1615
+ * Space Complexity: O(1)
1616
+ *
1617
+ * The function returns the predecessor of a given node in a tree.
1618
+ * @param {N} node - The parameter `node` is of type `RedBlackTreeNode`, which represents a node in a
1619
+ * tree.
1620
+ * @returns the predecessor of the given 'node'.
1620
1621
  */
1621
- getPredecessor(node: BTNodeKeyOrNode<K, N>): N | undefined {
1622
- node = this.ensureNode(node);
1623
- if (!this.isRealNode(node)) return undefined;
1624
-
1625
- if (node.left) {
1622
+ getPredecessor(node: N): N {
1623
+ if (this.isRealNode(node.left)) {
1626
1624
  let predecessor: N | null | undefined = node.left;
1627
1625
  while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) {
1628
- if (predecessor) {
1626
+ if (this.isRealNode(predecessor)) {
1629
1627
  predecessor = predecessor.right;
1630
1628
  }
1631
1629
  }
@@ -1642,15 +1640,16 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1642
1640
  * after the given node in the inorder traversal of the binary tree.
1643
1641
  */
1644
1642
  getSuccessor(x?: K | N | null): N | null | undefined {
1643
+
1645
1644
  x = this.ensureNode(x);
1646
- if (!x) return undefined;
1645
+ if (!this.isRealNode(x)) return undefined;
1647
1646
 
1648
- if (x.right) {
1647
+ if (this.isRealNode(x.right)) {
1649
1648
  return this.getLeftMost(x.right);
1650
1649
  }
1651
1650
 
1652
1651
  let y: N | null | undefined = x.parent;
1653
- while (y && y && x === y.right) {
1652
+ while (this.isRealNode(y) && x === y.right) {
1654
1653
  x = y;
1655
1654
  y = y.parent;
1656
1655
  }
@@ -14,7 +14,7 @@ import type {
14
14
  BTNodeExemplar,
15
15
  BTNodePureExemplar
16
16
  } from '../../types';
17
- import { BSTVariant, CP, IterationType } from '../../types';
17
+ import { BSTVariant, BTNodeKeyOrNode, CP, IterationType } from '../../types';
18
18
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
19
19
  import { IBinaryTree } from '../../interfaces';
20
20
  import { Queue } from '../queue';
@@ -442,6 +442,16 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
442
442
  }
443
443
  }
444
444
 
445
+ /**
446
+ * The function "isNotNodeInstance" checks if a potential key is a K.
447
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
448
+ * data type.
449
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
450
+ */
451
+ override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
452
+ return !(potentialKey instanceof BSTNode)
453
+ }
454
+
445
455
  /**
446
456
  * Time Complexity: O(log n) - Average case for a balanced tree.
447
457
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -11,6 +11,7 @@ import {
11
11
  BSTNodeKeyOrNode,
12
12
  BTNCallback,
13
13
  BTNodeExemplar,
14
+ BTNodeKeyOrNode,
14
15
  IterationType,
15
16
  RBTNColor,
16
17
  RBTreeOptions,
@@ -115,6 +116,16 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
115
116
  return exemplar instanceof RedBlackTreeNode;
116
117
  }
117
118
 
119
+ /**
120
+ * The function "isNotNodeInstance" checks if a potential key is a K.
121
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
122
+ * data type.
123
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
124
+ */
125
+ override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
126
+ return !(potentialKey instanceof RedBlackTreeNode)
127
+ }
128
+
118
129
  /**
119
130
  * The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
120
131
  * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
@@ -300,7 +311,8 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
300
311
  */
301
312
 
302
313
  override isRealNode(node: N | undefined): node is N {
303
- return node !== this.Sentinel && node !== undefined;
314
+ if (node === this.Sentinel || node === undefined) return false;
315
+ return node instanceof RedBlackTreeNode;
304
316
  }
305
317
 
306
318
  getNode<C extends BTNCallback<N, K>>(
@@ -362,38 +374,12 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
362
374
  }
363
375
 
364
376
  /**
365
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
366
- * Space Complexity: O(1)
367
- */
368
-
369
- /**
370
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
371
- * Space Complexity: O(1)
372
- *
373
- * The function returns the successor of a given node in a red-black tree.
374
- * @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
375
- * @returns the successor of the given RedBlackTreeNode.
376
- */
377
- override getSuccessor(x: N): N | undefined {
378
- if (x.right !== this.Sentinel) {
379
- return this.getLeftMost(x.right) ?? undefined;
380
- }
381
-
382
- let y: N | undefined = x.parent;
383
- while (y !== this.Sentinel && y !== undefined && x === y.right) {
384
- x = y;
385
- y = y.parent;
386
- }
387
- return y;
388
- }
389
-
390
- /**
391
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
377
+ * Time Complexity: O(log n)
392
378
  * Space Complexity: O(1)
393
379
  */
394
380
 
395
381
  /**
396
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
382
+ * Time Complexity: O(log n)
397
383
  * Space Complexity: O(1)
398
384
  *
399
385
  * The function returns the predecessor of a given node in a red-black tree.
@@ -402,12 +388,12 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
402
388
  * @returns the predecessor of the given RedBlackTreeNode 'x'.
403
389
  */
404
390
  override getPredecessor(x: N): N {
405
- if (x.left !== this.Sentinel) {
406
- return this.getRightMost(x.left!)!;
391
+ if (this.isRealNode(x.left)) {
392
+ return this.getRightMost(x.left)!;
407
393
  }
408
394
 
409
395
  let y: N | undefined = x.parent;
410
- while (y !== this.Sentinel && x === y!.left) {
396
+ while (this.isRealNode(y) && x === y.left) {
411
397
  x = y!;
412
398
  y = y!.parent;
413
399
  }
@@ -6,7 +6,14 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BSTNodeKeyOrNode, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
9
- import { BiTreeDeleteResult, BTNCallback, FamilyPosition, IterationType, TreeMultimapNested } from '../../types';
9
+ import {
10
+ BiTreeDeleteResult,
11
+ BTNCallback,
12
+ BTNodeKeyOrNode,
13
+ FamilyPosition,
14
+ IterationType,
15
+ TreeMultimapNested
16
+ } from '../../types';
10
17
  import { IBinaryTree } from '../../interfaces';
11
18
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
19
 
@@ -85,6 +92,15 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
85
92
  return exemplar instanceof TreeMultimapNode;
86
93
  }
87
94
 
95
+ /**
96
+ * The function "isNotNodeInstance" checks if a potential key is a K.
97
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
98
+ * data type.
99
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
100
+ */
101
+ override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
102
+ return !(potentialKey instanceof TreeMultimapNode)
103
+ }
88
104
 
89
105
  /**
90
106
  * The function `exemplarToNode` converts an exemplar object into a node object.