data-structure-typed 1.48.2 → 1.48.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.
Files changed (69) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +16 -16
  3. package/benchmark/report.html +16 -16
  4. package/benchmark/report.json +184 -184
  5. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +16 -16
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.js +7 -7
  7. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +89 -87
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js +67 -58
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/bst.d.ts +28 -47
  12. package/dist/cjs/data-structures/binary-tree/bst.js +54 -57
  13. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +15 -15
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js +7 -7
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +22 -22
  18. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +11 -11
  19. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  20. package/dist/cjs/interfaces/binary-tree.d.ts +6 -6
  21. package/dist/cjs/types/common.d.ts +11 -8
  22. package/dist/cjs/types/common.js +6 -1
  23. package/dist/cjs/types/common.js.map +1 -1
  24. package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
  25. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  26. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +6 -6
  27. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  28. package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
  29. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +16 -16
  30. package/dist/mjs/data-structures/binary-tree/avl-tree.js +7 -7
  31. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +89 -87
  32. package/dist/mjs/data-structures/binary-tree/binary-tree.js +67 -58
  33. package/dist/mjs/data-structures/binary-tree/bst.d.ts +28 -47
  34. package/dist/mjs/data-structures/binary-tree/bst.js +55 -57
  35. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +15 -15
  36. package/dist/mjs/data-structures/binary-tree/rb-tree.js +7 -7
  37. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +22 -22
  38. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +11 -11
  39. package/dist/mjs/interfaces/binary-tree.d.ts +6 -6
  40. package/dist/mjs/types/common.d.ts +11 -8
  41. package/dist/mjs/types/common.js +5 -0
  42. package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
  43. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  44. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +6 -6
  45. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  46. package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
  47. package/dist/umd/data-structure-typed.js +152 -140
  48. package/dist/umd/data-structure-typed.min.js +2 -2
  49. package/dist/umd/data-structure-typed.min.js.map +1 -1
  50. package/package.json +2 -2
  51. package/src/data-structures/binary-tree/avl-tree.ts +20 -21
  52. package/src/data-structures/binary-tree/binary-tree.ts +147 -136
  53. package/src/data-structures/binary-tree/bst.ts +86 -82
  54. package/src/data-structures/binary-tree/rb-tree.ts +25 -26
  55. package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
  56. package/src/interfaces/binary-tree.ts +5 -6
  57. package/src/types/common.ts +11 -8
  58. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
  59. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
  60. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  61. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  62. package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
  63. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +3 -3
  64. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
  65. package/test/unit/data-structures/binary-tree/bst.test.ts +10 -9
  66. package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
  67. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +1 -1
  68. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +5 -5
  69. package/test/unit/unrestricted-interconversion.test.ts +1 -1
@@ -1,4 +1,4 @@
1
- import { CP, IterationType } from '../../types';
1
+ import { BSTVariant, CP, IterationType } from '../../types';
2
2
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
3
3
  import { Queue } from '../queue';
4
4
  export class BSTNode extends BinaryTreeNode {
@@ -65,9 +65,9 @@ export class BST extends BinaryTree {
65
65
  constructor(elements, options) {
66
66
  super([], options);
67
67
  if (options) {
68
- const { comparator } = options;
69
- if (comparator) {
70
- this.comparator = comparator;
68
+ const { variant } = options;
69
+ if (variant) {
70
+ this._variant = variant;
71
71
  }
72
72
  }
73
73
  this._root = undefined;
@@ -78,10 +78,13 @@ export class BST extends BinaryTree {
78
78
  get root() {
79
79
  return this._root;
80
80
  }
81
- comparator = (a, b) => a - b;
81
+ _variant = BSTVariant.MIN;
82
+ get variant() {
83
+ return this._variant;
84
+ }
82
85
  /**
83
86
  * The function creates a new binary search tree node with the given key and value.
84
- * @param {BTNKey} key - The key parameter is the key value that will be associated with
87
+ * @param {K} key - The key parameter is the key value that will be associated with
85
88
  * the new node. It is used to determine the position of the node in the binary search tree.
86
89
  * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
87
90
  * represents the value associated with the node in a binary search tree.
@@ -100,12 +103,12 @@ export class BST extends BinaryTree {
100
103
  createTree(options) {
101
104
  return new BST([], {
102
105
  iterationType: this.iterationType,
103
- comparator: this.comparator, ...options
106
+ variant: this.variant, ...options
104
107
  });
105
108
  }
106
109
  /**
107
110
  * The function checks if an exemplar is an instance of BSTNode.
108
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
111
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V, N>`.
109
112
  * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
110
113
  */
111
114
  isNode(exemplar) {
@@ -114,7 +117,7 @@ export class BST extends BinaryTree {
114
117
  /**
115
118
  * The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
116
119
  * is valid, otherwise it returns undefined.
117
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
120
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
118
121
  * @returns a variable `node` which is of type `N` or `undefined`.
119
122
  */
120
123
  exemplarToNode(exemplar) {
@@ -134,7 +137,7 @@ export class BST extends BinaryTree {
134
137
  node = this.createNode(key, value);
135
138
  }
136
139
  }
137
- else if (this.isNodeKey(exemplar)) {
140
+ else if (this.isNotNodeInstance(exemplar)) {
138
141
  node = this.createNode(exemplar);
139
142
  }
140
143
  else {
@@ -242,17 +245,17 @@ export class BST extends BinaryTree {
242
245
  sorted = realBTNExemplars.sort((a, b) => {
243
246
  let aR, bR;
244
247
  if (this.isEntry(a))
245
- aR = a[0];
248
+ aR = this.extractor(a[0]);
246
249
  else if (this.isRealNode(a))
247
- aR = a.key;
250
+ aR = this.extractor(a.key);
248
251
  else
249
- aR = a;
252
+ aR = this.extractor(a);
250
253
  if (this.isEntry(b))
251
- bR = b[0];
254
+ bR = this.extractor(b[0]);
252
255
  else if (this.isRealNode(b))
253
- bR = b.key;
256
+ bR = this.extractor(b.key);
254
257
  else
255
- bR = b;
258
+ bR = this.extractor(b);
256
259
  return aR - bR;
257
260
  });
258
261
  const _dfs = (arr) => {
@@ -289,33 +292,31 @@ export class BST extends BinaryTree {
289
292
  }
290
293
  return inserted;
291
294
  }
292
- /**
293
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
294
- * Space Complexity: O(n) - Additional space is required for the sorted array.
295
- */
296
- /**
297
- * Time Complexity: O(log n) - Average case for a balanced tree.
298
- * Space Complexity: O(1) - Constant space is used.
299
- *
300
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
301
- * leftmost node if the comparison result is greater than.
302
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
303
- * type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
304
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
305
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
306
- * be performed. It can have one of the following values:
307
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
308
- * the key of the leftmost node if the comparison result is greater than, and the key of the
309
- * rightmost node otherwise. If no node is found, it returns 0.
310
- */
311
- lastKey(beginRoot = this.root, iterationType = this.iterationType) {
312
- if (this._compare(0, 1) === CP.lt)
313
- return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
314
- else if (this._compare(0, 1) === CP.gt)
315
- return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
316
- else
317
- return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
318
- }
295
+ // /**
296
+ // * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
297
+ // * Space Complexity: O(n) - Additional space is required for the sorted array.
298
+ // */
299
+ //
300
+ // /**
301
+ // * Time Complexity: O(log n) - Average case for a balanced tree.
302
+ // * Space Complexity: O(1) - Constant space is used.
303
+ // *
304
+ // * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
305
+ // * leftmost node if the comparison result is greater than.
306
+ // * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
307
+ // * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
308
+ // * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
309
+ // * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
310
+ // * be performed. It can have one of the following values:
311
+ // * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
312
+ // * the key of the leftmost node if the comparison result is greater than, and the key of the
313
+ // * rightmost node otherwise. If no node is found, it returns 0.
314
+ // */
315
+ // lastKey(beginRoot: BSTNodeKeyOrNode<K,N> = this.root, iterationType = this.iterationType): K {
316
+ // if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
317
+ // else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
318
+ // else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
319
+ // }
319
320
  /**
320
321
  * Time Complexity: O(log n) - Average case for a balanced tree.
321
322
  * Space Complexity: O(1) - Constant space is used.
@@ -326,7 +327,7 @@ export class BST extends BinaryTree {
326
327
  *
327
328
  * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
328
329
  * either recursive or iterative methods.
329
- * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
330
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
330
331
  * It is used to identify the node that we want to retrieve.
331
332
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
332
333
  * type of iteration to use when searching for a node in the binary tree. It can have two possible
@@ -372,14 +373,14 @@ export class BST extends BinaryTree {
372
373
  /**
373
374
  * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
374
375
  * otherwise it returns the key itself.
375
- * @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
376
+ * @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
376
377
  * `undefined`.
377
378
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
378
379
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
379
380
  * @returns either a node object (N) or undefined.
380
381
  */
381
382
  ensureNode(key, iterationType = IterationType.ITERATIVE) {
382
- return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
383
+ return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
383
384
  }
384
385
  /**
385
386
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
@@ -397,7 +398,7 @@ export class BST extends BinaryTree {
397
398
  * first node that matches the identifier. If set to true, the function will return an array
398
399
  * containing only the first matching node. If set to false (default), the function will continue
399
400
  * searching for all nodes that match the identifier and return an array containing
400
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
401
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
401
402
  * for the traversal. It can be either a key value or a node object. If it is undefined, the
402
403
  * traversal will start from the root of the tree.
403
404
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be
@@ -477,7 +478,7 @@ export class BST extends BinaryTree {
477
478
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
478
479
  * `CP`, which is a custom type representing the comparison operator. The possible values for
479
480
  * `lesserOrGreater` are
480
- * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter represents the node in the
481
+ * @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
481
482
  * binary tree that you want to traverse from. It can be specified either by its key, by the node
482
483
  * object itself, or it can be left undefined to start the traversal from the root of the tree.
483
484
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -653,18 +654,15 @@ export class BST extends BinaryTree {
653
654
  /**
654
655
  * The function compares two values using a comparator function and returns whether the first value
655
656
  * is greater than, less than, or equal to the second value.
656
- * @param {BTNKey} a - The parameter "a" is of type BTNKey.
657
- * @param {BTNKey} b - The parameter "b" in the above code represents a BTNKey.
657
+ * @param {K} a - The parameter "a" is of type K.
658
+ * @param {K} b - The parameter "b" in the above code represents a K.
658
659
  * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
659
660
  * than), CP.lt (less than), or CP.eq (equal).
660
661
  */
661
662
  _compare(a, b) {
662
- const compared = this.comparator(a, b);
663
- if (compared > 0)
664
- return CP.gt;
665
- else if (compared < 0)
666
- return CP.lt;
667
- else
668
- return CP.eq;
663
+ const extractedA = this.extractor(a);
664
+ const extractedB = this.extractor(b);
665
+ const compared = this.variant === BSTVariant.MIN ? extractedA - extractedB : extractedB - extractedA;
666
+ return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq;
669
667
  }
670
668
  }
@@ -5,12 +5,12 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { BiTreeDeleteResult, BTNCallback, BTNKey, BTNodeExemplar, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
8
+ import { BiTreeDeleteResult, BTNCallback, BTNodeExemplar, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
9
9
  import { BST, BSTNode } from './bst';
10
10
  import { IBinaryTree } from '../../interfaces';
11
- export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
11
+ export declare class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
12
12
  color: RBTNColor;
13
- constructor(key: BTNKey, value?: V, color?: RBTNColor);
13
+ constructor(key: K, value?: V, color?: RBTNColor);
14
14
  }
15
15
  /**
16
16
  * 1. Each node is either red or black.
@@ -19,12 +19,12 @@ export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N>
19
19
  * 4. Red nodes must have black children.
20
20
  * 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
21
21
  */
22
- export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>, TREE extends RedBlackTree<V, N, TREE> = RedBlackTree<V, N, RedBlackTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
22
+ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, N, TREE> = RedBlackTree<K, V, N, RedBlackTreeNested<K, V, N>>> extends BST<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
23
23
  Sentinel: N;
24
24
  /**
25
25
  * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
26
26
  * initializes the tree with optional elements and options.
27
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
27
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
28
28
  * objects. It represents the initial elements that will be added to the RBTree during its
29
29
  * construction. If this parameter is provided, the `addMany` method is called to add all the
30
30
  * elements to the
@@ -32,14 +32,14 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
32
32
  * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
33
33
  * only a subset of the properties defined in the `RBTreeOptions` interface.
34
34
  */
35
- constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<RBTreeOptions>);
35
+ constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>);
36
36
  protected _root: N;
37
37
  get root(): N;
38
38
  protected _size: number;
39
39
  get size(): number;
40
40
  /**
41
41
  * The function creates a new Red-Black Tree node with the specified key, value, and color.
42
- * @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
42
+ * @param {K} key - The key parameter is the key value associated with the node. It is used to
43
43
  * identify and compare nodes in the Red-Black Tree.
44
44
  * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
45
45
  * associated with the node. It is of type `V`, which is a generic type that can be replaced with any
@@ -49,7 +49,7 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
49
49
  * @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
50
50
  * value, and color.
51
51
  */
52
- createNode(key: BTNKey, value?: V, color?: RBTNColor): N;
52
+ createNode(key: K, value?: V, color?: RBTNColor): N;
53
53
  /**
54
54
  * The function creates a Red-Black Tree with the specified options and returns it.
55
55
  * @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
@@ -57,23 +57,23 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
57
57
  * class.
58
58
  * @returns a new instance of a RedBlackTree object.
59
59
  */
60
- createTree(options?: RBTreeOptions): TREE;
60
+ createTree(options?: RBTreeOptions<K>): TREE;
61
61
  /**
62
62
  * The function checks if an exemplar is an instance of the RedBlackTreeNode class.
63
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
63
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
64
64
  * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
65
65
  * class.
66
66
  */
67
- isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
67
+ isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
68
68
  /**
69
69
  * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
70
70
  * otherwise it returns undefined.
71
- * @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
71
+ * @param exemplar - BTNodeExemplar<K, V, N> - A generic type representing an exemplar of a binary tree
72
72
  * node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
73
73
  * that is not a valid exemplar.
74
74
  * @returns a variable `node` which is of type `N | undefined`.
75
75
  */
76
- exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined;
76
+ exemplarToNode(exemplar: BTNodeExemplar<K, V, N>): N | undefined;
77
77
  /**
78
78
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
79
79
  * Space Complexity: O(1)
@@ -84,7 +84,7 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
84
84
  * @returns The method `add` returns either an instance of `N` (the node that was added) or
85
85
  * `undefined`.
86
86
  */
87
- add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
87
+ add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>): N | undefined;
88
88
  /**
89
89
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
90
90
  * Space Complexity: O(1)
@@ -110,7 +110,7 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
110
110
  * Space Complexity: O(1)
111
111
  */
112
112
  isRealNode(node: N | undefined): node is N;
113
- getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
113
+ getNode<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
114
114
  getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
115
115
  getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
116
116
  /**
@@ -27,7 +27,7 @@ export class RedBlackTree extends BST {
27
27
  /**
28
28
  * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
29
29
  * initializes the tree with optional elements and options.
30
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
30
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
31
31
  * objects. It represents the initial elements that will be added to the RBTree during its
32
32
  * construction. If this parameter is provided, the `addMany` method is called to add all the
33
33
  * elements to the
@@ -51,7 +51,7 @@ export class RedBlackTree extends BST {
51
51
  }
52
52
  /**
53
53
  * The function creates a new Red-Black Tree node with the specified key, value, and color.
54
- * @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
54
+ * @param {K} key - The key parameter is the key value associated with the node. It is used to
55
55
  * identify and compare nodes in the Red-Black Tree.
56
56
  * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
57
57
  * associated with the node. It is of type `V`, which is a generic type that can be replaced with any
@@ -74,12 +74,12 @@ export class RedBlackTree extends BST {
74
74
  createTree(options) {
75
75
  return new RedBlackTree([], {
76
76
  iterationType: this.iterationType,
77
- comparator: this.comparator, ...options
77
+ variant: this.variant, ...options
78
78
  });
79
79
  }
80
80
  /**
81
81
  * The function checks if an exemplar is an instance of the RedBlackTreeNode class.
82
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
82
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
83
83
  * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
84
84
  * class.
85
85
  */
@@ -89,7 +89,7 @@ export class RedBlackTree extends BST {
89
89
  /**
90
90
  * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
91
91
  * otherwise it returns undefined.
92
- * @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
92
+ * @param exemplar - BTNodeExemplar<K, V, N> - A generic type representing an exemplar of a binary tree
93
93
  * node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
94
94
  * that is not a valid exemplar.
95
95
  * @returns a variable `node` which is of type `N | undefined`.
@@ -111,7 +111,7 @@ export class RedBlackTree extends BST {
111
111
  node = this.createNode(key, value, RBTNColor.RED);
112
112
  }
113
113
  }
114
- else if (this.isNodeKey(exemplar)) {
114
+ else if (this.isNotNodeInstance(exemplar)) {
115
115
  node = this.createNode(exemplar, undefined, RBTNColor.RED);
116
116
  }
117
117
  else {
@@ -277,7 +277,7 @@ export class RedBlackTree extends BST {
277
277
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
278
278
  * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
279
279
  * function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
280
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is the starting point for
280
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is the starting point for
281
281
  * searching for a node in a binary tree. It can be either a key value or a node object. If it is not
282
282
  * provided, the search will start from the root of the binary tree.
283
283
  * @param iterationType - The `iterationType` parameter is a variable that determines the type of
@@ -5,15 +5,15 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNodeKeyOrNode, BTNKey, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
8
+ import type { BSTNodeKeyOrNode, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
9
9
  import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
- export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
12
+ export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNodeNested<K, V>> extends AVLTreeNode<K, V, N> {
13
13
  count: number;
14
14
  /**
15
15
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
16
- * @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
16
+ * @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
17
17
  * of the binary tree node.
18
18
  * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
19
19
  * tree node. If no value is provided, it will be `undefined`.
@@ -21,42 +21,42 @@ export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N>
21
21
  * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
22
22
  * parameter when creating a new instance of the `BinaryTreeNode` class.
23
23
  */
24
- constructor(key: BTNKey, value?: V, count?: number);
24
+ constructor(key: K, value?: V, count?: number);
25
25
  }
26
26
  /**
27
27
  * The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
28
28
  */
29
- export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNode<V, TreeMultimapNodeNested<V>>, TREE extends TreeMultimap<V, N, TREE> = TreeMultimap<V, N, TreeMultimapNested<V, N>>> extends AVLTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
30
- constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<TreeMultimapOptions>);
29
+ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
30
+ constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>);
31
31
  private _count;
32
32
  get count(): number;
33
33
  /**
34
34
  * The function creates a new BSTNode with the given key, value, and count.
35
- * @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
35
+ * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
36
36
  * distinguish one node from another in the tree.
37
37
  * @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
38
38
  * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
39
39
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
40
40
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
41
41
  */
42
- createNode(key: BTNKey, value?: V, count?: number): N;
43
- createTree(options?: TreeMultimapOptions): TREE;
42
+ createNode(key: K, value?: V, count?: number): N;
43
+ createTree(options?: TreeMultimapOptions<K>): TREE;
44
44
  /**
45
45
  * The function checks if an exemplar is an instance of the TreeMultimapNode class.
46
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
46
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
47
47
  * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
48
48
  * class.
49
49
  */
50
- isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
50
+ isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
51
51
  /**
52
52
  * The function `exemplarToNode` converts an exemplar object into a node object.
53
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
53
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where `V` represents
54
54
  * the value type and `N` represents the node type.
55
55
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
56
56
  * times the node should be created. If not provided, it defaults to 1.
57
57
  * @returns a value of type `N` (the generic type parameter) or `undefined`.
58
58
  */
59
- exemplarToNode(exemplar: BTNodeExemplar<V, N>, count?: number): N | undefined;
59
+ exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, count?: number): N | undefined;
60
60
  /**
61
61
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
62
62
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -73,7 +73,7 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
73
73
  * is 1.
74
74
  * @returns either a node (`N`) or `undefined`.
75
75
  */
76
- add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | undefined;
76
+ add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, count?: number): N | undefined;
77
77
  /**
78
78
  * Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
79
79
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -88,7 +88,7 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
88
88
  * either keys, nodes, or entries.
89
89
  * @returns The method is returning an array of type `N | undefined`.
90
90
  */
91
- addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>): (N | undefined)[];
91
+ addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>): (N | undefined)[];
92
92
  /**
93
93
  * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
94
94
  * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
@@ -158,22 +158,22 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
158
158
  * @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
159
159
  * added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
160
160
  * `undefined` if there is no node to add.
161
- * @param {BTNKey | N | undefined} parent - The `parent` parameter represents the parent node to
161
+ * @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
162
162
  * which the new node will be added as a child. It can be either a node object (`N`) or a key value
163
- * (`BTNKey`).
163
+ * (`K`).
164
164
  * @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
165
165
  * added, or `undefined` if no node was added.
166
166
  */
167
- protected _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<N>): N | undefined;
167
+ protected _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<K, N>): N | undefined;
168
168
  /**
169
169
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
170
- * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
171
- * which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
172
- * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
170
+ * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
171
+ * which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
172
+ * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
173
173
  * node where the values from the source node will be swapped to.
174
174
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
175
175
  * if either `srcNode` or `destNode` is undefined.
176
176
  */
177
- protected _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined;
177
+ protected _swapProperties(srcNode: BSTNodeKeyOrNode<K, N>, destNode: BSTNodeKeyOrNode<K, N>): N | undefined;
178
178
  protected _replaceNode(oldNode: N, newNode: N): N;
179
179
  }
@@ -4,7 +4,7 @@ export class TreeMultimapNode extends AVLTreeNode {
4
4
  count;
5
5
  /**
6
6
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
7
- * @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
7
+ * @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
8
8
  * of the binary tree node.
9
9
  * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
10
10
  * tree node. If no value is provided, it will be `undefined`.
@@ -35,7 +35,7 @@ export class TreeMultimap extends AVLTree {
35
35
  }
36
36
  /**
37
37
  * The function creates a new BSTNode with the given key, value, and count.
38
- * @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
38
+ * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
39
39
  * distinguish one node from another in the tree.
40
40
  * @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
41
41
  * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
@@ -48,12 +48,12 @@ export class TreeMultimap extends AVLTree {
48
48
  createTree(options) {
49
49
  return new TreeMultimap([], {
50
50
  iterationType: this.iterationType,
51
- comparator: this.comparator, ...options
51
+ variant: this.variant, ...options
52
52
  });
53
53
  }
54
54
  /**
55
55
  * The function checks if an exemplar is an instance of the TreeMultimapNode class.
56
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
56
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
57
57
  * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
58
58
  * class.
59
59
  */
@@ -62,7 +62,7 @@ export class TreeMultimap extends AVLTree {
62
62
  }
63
63
  /**
64
64
  * The function `exemplarToNode` converts an exemplar object into a node object.
65
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
65
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where `V` represents
66
66
  * the value type and `N` represents the node type.
67
67
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
68
68
  * times the node should be created. If not provided, it defaults to 1.
@@ -85,7 +85,7 @@ export class TreeMultimap extends AVLTree {
85
85
  node = this.createNode(key, value, count);
86
86
  }
87
87
  }
88
- else if (this.isNodeKey(exemplar)) {
88
+ else if (this.isNotNodeInstance(exemplar)) {
89
89
  node = this.createNode(exemplar, undefined, count);
90
90
  }
91
91
  else {
@@ -304,9 +304,9 @@ export class TreeMultimap extends AVLTree {
304
304
  * @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
305
305
  * added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
306
306
  * `undefined` if there is no node to add.
307
- * @param {BTNKey | N | undefined} parent - The `parent` parameter represents the parent node to
307
+ * @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
308
308
  * which the new node will be added as a child. It can be either a node object (`N`) or a key value
309
- * (`BTNKey`).
309
+ * (`K`).
310
310
  * @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
311
311
  * added, or `undefined` if no node was added.
312
312
  */
@@ -339,9 +339,9 @@ export class TreeMultimap extends AVLTree {
339
339
  }
340
340
  /**
341
341
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
342
- * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
343
- * which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
344
- * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
342
+ * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
343
+ * which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
344
+ * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
345
345
  * node where the values from the source node will be swapped to.
346
346
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
347
347
  * if either `srcNode` or `destNode` is undefined.
@@ -1,9 +1,9 @@
1
1
  import { BinaryTree, BinaryTreeNode } from '../data-structures';
2
- import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey, BTNodeExemplar } from '../types';
3
- export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
4
- createNode(key: BTNKey, value?: N['value']): N;
5
- createTree(options?: Partial<BinaryTreeOptions>): TREE;
6
- add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | null | undefined;
7
- addMany(nodes: Iterable<BTNodeExemplar<V, N>>): (N | null | undefined)[];
2
+ import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNodeExemplar } from '../types';
3
+ export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTreeNested<K, V, N>> {
4
+ createNode(key: K, value?: N['value']): N;
5
+ createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
6
+ add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, count?: number): N | null | undefined;
7
+ addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>): (N | null | undefined)[];
8
8
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
9
9
  }
@@ -1,5 +1,8 @@
1
- import { BTNKey } from "./data-structures";
2
- export type Comparator<T> = (a: T, b: T) => number;
1
+ export type Comparator<K> = (a: K, b: K) => number;
2
+ export declare enum BSTVariant {
3
+ MIN = "MIN",
4
+ MAX = "MAX"
5
+ }
3
6
  export type DFSOrderPattern = 'pre' | 'in' | 'post';
4
7
  export type BTNCallback<N, D = any> = (node: N) => D;
5
8
  export declare enum CP {
@@ -19,9 +22,9 @@ export type BinaryTreePrintOptions = {
19
22
  isShowNull?: boolean;
20
23
  isShowRedBlackNIL?: boolean;
21
24
  };
22
- export type BTNodeEntry<T> = [BTNKey | null | undefined, T | undefined];
23
- export type BTNodeKeyOrNode<N> = BTNKey | null | undefined | N;
24
- export type BTNodeExemplar<T, N> = BTNodeEntry<T> | BTNodeKeyOrNode<N>;
25
- export type BTNodePureExemplar<T, N> = [BTNKey, T | undefined] | BTNodePureKeyOrNode<N>;
26
- export type BTNodePureKeyOrNode<N> = BTNKey | N;
27
- export type BSTNodeKeyOrNode<N> = BTNKey | undefined | N;
25
+ export type BTNodeEntry<K, V> = [K | null | undefined, V | undefined];
26
+ export type BTNodeKeyOrNode<K, N> = K | null | undefined | N;
27
+ export type BTNodeExemplar<K, V, N> = BTNodeEntry<K, V> | BTNodeKeyOrNode<K, N>;
28
+ export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
29
+ export type BTNodePureKeyOrNode<K, N> = K | N;
30
+ export type BSTNodeKeyOrNode<K, N> = K | undefined | N;