heap-typed 1.51.7 → 1.51.8

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 (42) hide show
  1. package/README.md +72 -80
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -12
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +1 -10
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -3
  5. package/dist/data-structures/binary-tree/avl-tree.js +12 -12
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +5 -12
  7. package/dist/data-structures/binary-tree/binary-tree.js +22 -32
  8. package/dist/data-structures/binary-tree/bst.d.ts +32 -77
  9. package/dist/data-structures/binary-tree/bst.js +68 -136
  10. package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -13
  11. package/dist/data-structures/binary-tree/rb-tree.js +3 -20
  12. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
  13. package/dist/data-structures/heap/heap.d.ts +1 -3
  14. package/dist/interfaces/binary-tree.d.ts +3 -3
  15. package/dist/types/common.d.ts +1 -1
  16. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
  17. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
  18. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  19. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
  20. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
  21. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
  22. package/dist/types/utils/utils.d.ts +10 -1
  23. package/dist/utils/utils.d.ts +2 -1
  24. package/dist/utils/utils.js +29 -1
  25. package/package.json +2 -2
  26. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +4 -12
  27. package/src/data-structures/binary-tree/avl-tree.ts +15 -14
  28. package/src/data-structures/binary-tree/binary-tree.ts +29 -38
  29. package/src/data-structures/binary-tree/bst.ts +78 -148
  30. package/src/data-structures/binary-tree/rb-tree.ts +8 -22
  31. package/src/data-structures/binary-tree/tree-multi-map.ts +4 -3
  32. package/src/data-structures/heap/heap.ts +1 -1
  33. package/src/interfaces/binary-tree.ts +4 -3
  34. package/src/types/common.ts +1 -1
  35. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -2
  36. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -2
  37. package/src/types/data-structures/binary-tree/binary-tree.ts +5 -5
  38. package/src/types/data-structures/binary-tree/bst.ts +6 -5
  39. package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
  40. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -2
  41. package/src/types/utils/utils.ts +14 -1
  42. package/src/utils/utils.ts +20 -1
@@ -5,11 +5,10 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types';
9
- import { BSTNKeyOrNode, BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
8
+ import type { BSTNested, BSTNKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, Comparable, Comparator, CP, DFSOrderPattern, IterationType, KeyOrNodeOrEntry } from '../../types';
10
9
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
10
  import { IBinaryTree } from '../../interfaces';
12
- export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
11
+ export declare class BSTNode<K extends Comparable, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
13
12
  parent?: NODE;
14
13
  constructor(key: K, value?: V);
15
14
  protected _left?: NODE;
@@ -47,12 +46,13 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
47
46
  * 6. Balance Variability: Can become unbalanced; special types maintain balance.
48
47
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
49
48
  */
50
- export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, NODE, TREE> = BST<K, V, NODE, BSTNested<K, V, NODE>>> extends BinaryTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
49
+ export declare class BST<K extends Comparable, V = any, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, NODE, TREE> = BST<K, V, NODE, BSTNested<K, V, NODE>>> extends BinaryTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
51
50
  /**
52
- * This is the constructor function for a TypeScript class that initializes a binary search tree with
53
- * optional keys or nodes or entries and options.
54
- * @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used
55
- * to initialize the binary search tree with the provided keys, nodes, or entries.
51
+ * This is the constructor function for a Binary Search Tree class in TypeScript, which initializes
52
+ * the tree with keys, nodes, or entries and optional options.
53
+ * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
54
+ * contain keys, nodes, or entries. It is used to initialize the binary search tree with the provided
55
+ * keys, nodes, or entries.
56
56
  * @param [options] - The `options` parameter is an optional object that can contain additional
57
57
  * configuration options for the binary search tree. It can have the following properties:
58
58
  */
@@ -63,12 +63,12 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
63
63
  * @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
64
64
  */
65
65
  get root(): NODE | undefined;
66
- protected _variant: BSTVariant;
66
+ protected _comparator: Comparator<K>;
67
67
  /**
68
- * The function returns the value of the _variant property.
69
- * @returns The value of the `_variant` property.
68
+ * The function returns the value of the _comparator property.
69
+ * @returns The `_comparator` property is being returned.
70
70
  */
71
- get variant(): BSTVariant;
71
+ get comparator(): Comparator<K>;
72
72
  /**
73
73
  * The function creates a new BSTNode with the given key and value and returns it.
74
74
  * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
@@ -127,13 +127,11 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
127
127
  * Time Complexity: O(log n)
128
128
  * Space Complexity: O(1)
129
129
  *
130
- * The `add` function adds a new node to a binary tree, updating the value if the key already exists
131
- * or inserting a new node if the key is unique.
132
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
133
- * @param {V} [value] - The `value` parameter represents the value associated with the key that is
134
- * being added to the binary tree.
135
- * @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
136
- * node was not added.
130
+ * The `add` function in TypeScript adds a new node to a binary search tree based on the key value,
131
+ * updating the value if the key already exists.
132
+ * @param keyOrNodeOrEntry - It is a parameter that can accept three types of values:
133
+ * @param {V} [value] - The value to be added to the binary search tree.
134
+ * @returns The method returns a boolean value.
137
135
  */
138
136
  add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
139
137
  /**
@@ -144,21 +142,24 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
144
142
  * Time Complexity: O(k log n)
145
143
  * Space Complexity: O(k + log n)
146
144
  *
147
- * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
148
- * balancing the tree after each addition.
149
- * @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
150
- * the binary tree.
145
+ * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure, balancing
146
+ * the structure if specified, and returns an array indicating whether each key or node was
147
+ * successfully inserted.
148
+ * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
149
+ * data structure.
151
150
  * @param [values] - An optional iterable of values to be associated with the keys or nodes being
152
151
  * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
153
152
  * order. If not provided, undefined will be assigned as the value for each key or node.
154
- * @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
155
- * balanced or not. If set to true, the add operation will be balanced using a binary search tree
156
- * algorithm. If set to false, the add operation will not be balanced and the nodes will be added
157
- * in the order they appear in the input.
158
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
159
- * type of iteration to use when adding multiple keys or nodes. It has a default value of
160
- * `this.iterationType`, which suggests that it is a property of the current object.
161
- * @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
153
+ * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
154
+ * adding the elements. If set to true, the tree will be balanced using a binary search tree
155
+ * algorithm. If set to false, the elements will be added without balancing the tree. The default
156
+ * value is true.
157
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
158
+ * specifies the type of iteration to use when adding multiple keys or nodes to the binary tree. It
159
+ * has a default value of `this.iterationType`, which means it will use the iteration type specified
160
+ * in the binary tree instance.
161
+ * @returns The function `addMany` returns an array of booleans indicating whether each key or node
162
+ * or entry was successfully inserted into the data structure.
162
163
  */
163
164
  addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
164
165
  /**
@@ -304,24 +305,6 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
304
305
  * function.
305
306
  */
306
307
  listLevels<C extends BTNCallback<NODE>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): ReturnType<C>[][];
307
- /**
308
- * Time Complexity: O(log n)
309
- * Space Complexity: O(1)
310
- */
311
- /**
312
- * Time Complexity: O(log n)
313
- * Space Complexity: O(1)
314
- *
315
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
316
- * leftmost node if the comparison result is greater than.
317
- * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
318
- * type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
319
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
320
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
321
- * the key of the leftmost node if the comparison result is greater than, and the key of the
322
- * rightmost node otherwise. If no node is found, it returns 0.
323
- */
324
- lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, NODE>): K | undefined;
325
308
  /**
326
309
  * Time Complexity: O(log n)
327
310
  * Space Complexity: O(log n)
@@ -393,32 +376,4 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
393
376
  * can either be an object of type `NODE` or it can be `undefined`.
394
377
  */
395
378
  protected _setRoot(v: NODE | undefined): void;
396
- /**
397
- * The function compares two values using a comparator function and returns whether the first value
398
- * is greater than, less than, or equal to the second value.
399
- * @param {K} a - The parameter "a" is of type K.
400
- * @param {K} b - The parameter "b" in the above code represents a K.
401
- * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
402
- * than), 'LT' (less than), or 'EQ' (equal).
403
- */
404
- protected _compare(a: K, b: K): CP;
405
- /**
406
- * The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
407
- * `a` is less than `b` based on the specified variant.
408
- * @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
409
- * first value to be compared in the function.
410
- * @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
411
- * of the arguments for the comparison in the `_lt` function.
412
- * @returns a boolean value.
413
- */
414
- protected _lt(a: K, b: K): boolean;
415
- /**
416
- * The function compares two values using a custom extractor function and returns true if the first
417
- * value is greater than the second value.
418
- * @param {K} a - The parameter "a" is of type K, which means it can be any type.
419
- * @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
420
- * of the arguments for the comparison in the function.
421
- * @returns a boolean value.
422
- */
423
- protected _gt(a: K, b: K): boolean;
424
379
  }
@@ -60,22 +60,29 @@ exports.BSTNode = BSTNode;
60
60
  */
61
61
  class BST extends binary_tree_1.BinaryTree {
62
62
  /**
63
- * This is the constructor function for a TypeScript class that initializes a binary search tree with
64
- * optional keys or nodes or entries and options.
65
- * @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used
66
- * to initialize the binary search tree with the provided keys, nodes, or entries.
63
+ * This is the constructor function for a Binary Search Tree class in TypeScript, which initializes
64
+ * the tree with keys, nodes, or entries and optional options.
65
+ * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
66
+ * contain keys, nodes, or entries. It is used to initialize the binary search tree with the provided
67
+ * keys, nodes, or entries.
67
68
  * @param [options] - The `options` parameter is an optional object that can contain additional
68
69
  * configuration options for the binary search tree. It can have the following properties:
69
70
  */
70
71
  constructor(keysOrNodesOrEntries = [], options) {
71
72
  super([], options);
72
- this._variant = 'STANDARD';
73
+ this._root = undefined;
74
+ this._comparator = (a, b) => {
75
+ if (a > b)
76
+ return 1;
77
+ if (a < b)
78
+ return -1;
79
+ return 0;
80
+ };
73
81
  if (options) {
74
- const { variant } = options;
75
- if (variant)
76
- this._variant = variant;
82
+ const { comparator } = options;
83
+ if (comparator)
84
+ this._comparator = comparator;
77
85
  }
78
- this._root = undefined;
79
86
  if (keysOrNodesOrEntries)
80
87
  this.addMany(keysOrNodesOrEntries);
81
88
  }
@@ -87,11 +94,11 @@ class BST extends binary_tree_1.BinaryTree {
87
94
  return this._root;
88
95
  }
89
96
  /**
90
- * The function returns the value of the _variant property.
91
- * @returns The value of the `_variant` property.
97
+ * The function returns the value of the _comparator property.
98
+ * @returns The `_comparator` property is being returned.
92
99
  */
93
- get variant() {
94
- return this._variant;
100
+ get comparator() {
101
+ return this._comparator;
95
102
  }
96
103
  /**
97
104
  * The function creates a new BSTNode with the given key and value and returns it.
@@ -113,7 +120,7 @@ class BST extends binary_tree_1.BinaryTree {
113
120
  * options. The returned value is casted as TREE.
114
121
  */
115
122
  createTree(options) {
116
- return new BST([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
123
+ return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
117
124
  }
118
125
  /**
119
126
  * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
@@ -197,13 +204,11 @@ class BST extends binary_tree_1.BinaryTree {
197
204
  * Time Complexity: O(log n)
198
205
  * Space Complexity: O(1)
199
206
  *
200
- * The `add` function adds a new node to a binary tree, updating the value if the key already exists
201
- * or inserting a new node if the key is unique.
202
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
203
- * @param {V} [value] - The `value` parameter represents the value associated with the key that is
204
- * being added to the binary tree.
205
- * @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
206
- * node was not added.
207
+ * The `add` function in TypeScript adds a new node to a binary search tree based on the key value,
208
+ * updating the value if the key already exists.
209
+ * @param keyOrNodeOrEntry - It is a parameter that can accept three types of values:
210
+ * @param {V} [value] - The value to be added to the binary search tree.
211
+ * @returns The method returns a boolean value.
207
212
  */
208
213
  add(keyOrNodeOrEntry, value) {
209
214
  const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
@@ -216,7 +221,7 @@ class BST extends binary_tree_1.BinaryTree {
216
221
  }
217
222
  let current = this.root;
218
223
  while (current !== undefined) {
219
- if (this._compare(current.key, newNode.key) === 'EQ') {
224
+ if (this.comparator(current.key, newNode.key) === 0) {
220
225
  // if (current !== newNode) {
221
226
  // The key value is the same but the reference is different, update the value of the existing node
222
227
  this._replaceNode(current, newNode);
@@ -227,7 +232,7 @@ class BST extends binary_tree_1.BinaryTree {
227
232
  // return;
228
233
  // }
229
234
  }
230
- else if (this._compare(current.key, newNode.key) === 'GT') {
235
+ else if (this.comparator(current.key, newNode.key) > 0) {
231
236
  if (current.left === undefined) {
232
237
  current.left = newNode;
233
238
  this._size++;
@@ -254,21 +259,24 @@ class BST extends binary_tree_1.BinaryTree {
254
259
  * Time Complexity: O(k log n)
255
260
  * Space Complexity: O(k + log n)
256
261
  *
257
- * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
258
- * balancing the tree after each addition.
259
- * @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
260
- * the binary tree.
262
+ * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure, balancing
263
+ * the structure if specified, and returns an array indicating whether each key or node was
264
+ * successfully inserted.
265
+ * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
266
+ * data structure.
261
267
  * @param [values] - An optional iterable of values to be associated with the keys or nodes being
262
268
  * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
263
269
  * order. If not provided, undefined will be assigned as the value for each key or node.
264
- * @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
265
- * balanced or not. If set to true, the add operation will be balanced using a binary search tree
266
- * algorithm. If set to false, the add operation will not be balanced and the nodes will be added
267
- * in the order they appear in the input.
268
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
269
- * type of iteration to use when adding multiple keys or nodes. It has a default value of
270
- * `this.iterationType`, which suggests that it is a property of the current object.
271
- * @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
270
+ * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
271
+ * adding the elements. If set to true, the tree will be balanced using a binary search tree
272
+ * algorithm. If set to false, the elements will be added without balancing the tree. The default
273
+ * value is true.
274
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
275
+ * specifies the type of iteration to use when adding multiple keys or nodes to the binary tree. It
276
+ * has a default value of `this.iterationType`, which means it will use the iteration type specified
277
+ * in the binary tree instance.
278
+ * @returns The function `addMany` returns an array of booleans indicating whether each key or node
279
+ * or entry was successfully inserted into the data structure.
272
280
  */
273
281
  addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
274
282
  const inserted = [];
@@ -295,20 +303,24 @@ class BST extends binary_tree_1.BinaryTree {
295
303
  }
296
304
  let sorted = [];
297
305
  sorted = realBTNExemplars.sort((a, b) => {
298
- let aR, bR;
299
- if (this.isEntry(a))
300
- aR = this.extractor(a[0]);
306
+ let keyA, keyB;
307
+ if (this.isEntry(a)) {
308
+ keyA = a[0];
309
+ }
301
310
  else if (this.isRealNode(a))
302
- aR = this.extractor(a.key);
311
+ keyA = a.key;
303
312
  else
304
- aR = this.extractor(a);
313
+ keyA = a;
305
314
  if (this.isEntry(b))
306
- bR = this.extractor(b[0]);
315
+ keyB = b[0];
307
316
  else if (this.isRealNode(b))
308
- bR = this.extractor(b.key);
317
+ keyB = b.key;
309
318
  else
310
- bR = this.extractor(b);
311
- return aR - bR;
319
+ keyB = b;
320
+ if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
321
+ return this.comparator(keyA, keyB);
322
+ }
323
+ return 0;
312
324
  });
313
325
  const _dfs = (arr) => {
314
326
  if (arr.length === 0)
@@ -390,9 +402,9 @@ class BST extends binary_tree_1.BinaryTree {
390
402
  return;
391
403
  // TODO potential bug
392
404
  if (callback === this._DEFAULT_CALLBACK) {
393
- if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
405
+ if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
394
406
  dfs(cur.left);
395
- if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
407
+ if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
396
408
  dfs(cur.right);
397
409
  }
398
410
  else {
@@ -414,9 +426,9 @@ class BST extends binary_tree_1.BinaryTree {
414
426
  }
415
427
  // TODO potential bug
416
428
  if (callback === this._DEFAULT_CALLBACK) {
417
- if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
429
+ if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
418
430
  stack.push(cur.right);
419
- if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
431
+ if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
420
432
  stack.push(cur.left);
421
433
  // if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
422
434
  // if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
@@ -558,41 +570,6 @@ class BST extends binary_tree_1.BinaryTree {
558
570
  listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
559
571
  return super.listLevels(callback, beginRoot, iterationType, false);
560
572
  }
561
- /**
562
- * Time Complexity: O(log n)
563
- * Space Complexity: O(1)
564
- */
565
- /**
566
- * Time Complexity: O(log n)
567
- * Space Complexity: O(1)
568
- *
569
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
570
- * leftmost node if the comparison result is greater than.
571
- * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
572
- * type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
573
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
574
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
575
- * the key of the leftmost node if the comparison result is greater than, and the key of the
576
- * rightmost node otherwise. If no node is found, it returns 0.
577
- */
578
- lastKey(beginRoot = this.root) {
579
- let current = this.ensureNode(beginRoot);
580
- if (!current)
581
- return undefined;
582
- if (this._variant === 'STANDARD') {
583
- // For 'STANDARD', find the rightmost node
584
- while (current.right !== undefined) {
585
- current = current.right;
586
- }
587
- }
588
- else {
589
- // For BSTVariant.MAX, find the leftmost node
590
- while (current.left !== undefined) {
591
- current = current.left;
592
- }
593
- }
594
- return current.key;
595
- }
596
573
  /**
597
574
  * Time Complexity: O(log n)
598
575
  * Space Complexity: O(log n)
@@ -618,18 +595,18 @@ class BST extends binary_tree_1.BinaryTree {
618
595
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
619
596
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
620
597
  */
621
- lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = 'LT', targetNode = this.root, iterationType = this.iterationType) {
622
- targetNode = this.ensureNode(targetNode);
598
+ lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = -1, targetNode = this.root, iterationType = this.iterationType) {
599
+ const targetNodeEnsured = this.ensureNode(targetNode);
623
600
  const ans = [];
624
- if (!targetNode)
601
+ if (!targetNodeEnsured)
625
602
  return ans;
626
603
  if (!this.root)
627
604
  return ans;
628
- const targetKey = targetNode.key;
605
+ const targetKey = targetNodeEnsured.key;
629
606
  if (iterationType === 'RECURSIVE') {
630
607
  const dfs = (cur) => {
631
- const compared = this._compare(cur.key, targetKey);
632
- if (compared === lesserOrGreater)
608
+ const compared = this.comparator(cur.key, targetKey);
609
+ if (Math.sign(compared) === lesserOrGreater)
633
610
  ans.push(callback(cur));
634
611
  if (this.isRealNode(cur.left))
635
612
  dfs(cur.left);
@@ -644,8 +621,8 @@ class BST extends binary_tree_1.BinaryTree {
644
621
  while (queue.size > 0) {
645
622
  const cur = queue.shift();
646
623
  if (this.isRealNode(cur)) {
647
- const compared = this._compare(cur.key, targetKey);
648
- if (compared === lesserOrGreater)
624
+ const compared = this.comparator(cur.key, targetKey);
625
+ if (Math.sign(compared) === lesserOrGreater)
649
626
  ans.push(callback(cur));
650
627
  if (this.isRealNode(cur.left))
651
628
  queue.push(cur.left);
@@ -786,50 +763,5 @@ class BST extends binary_tree_1.BinaryTree {
786
763
  }
787
764
  this._root = v;
788
765
  }
789
- /**
790
- * The function compares two values using a comparator function and returns whether the first value
791
- * is greater than, less than, or equal to the second value.
792
- * @param {K} a - The parameter "a" is of type K.
793
- * @param {K} b - The parameter "b" in the above code represents a K.
794
- * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
795
- * than), 'LT' (less than), or 'EQ' (equal).
796
- */
797
- _compare(a, b) {
798
- const extractedA = this.extractor(a);
799
- const extractedB = this.extractor(b);
800
- const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
801
- if (compared > 0)
802
- return 'GT';
803
- if (compared < 0)
804
- return 'LT';
805
- return 'EQ';
806
- }
807
- /**
808
- * The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
809
- * `a` is less than `b` based on the specified variant.
810
- * @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
811
- * first value to be compared in the function.
812
- * @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
813
- * of the arguments for the comparison in the `_lt` function.
814
- * @returns a boolean value.
815
- */
816
- _lt(a, b) {
817
- const extractedA = this.extractor(a);
818
- const extractedB = this.extractor(b);
819
- return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB;
820
- }
821
- /**
822
- * The function compares two values using a custom extractor function and returns true if the first
823
- * value is greater than the second value.
824
- * @param {K} a - The parameter "a" is of type K, which means it can be any type.
825
- * @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
826
- * of the arguments for the comparison in the function.
827
- * @returns a boolean value.
828
- */
829
- _gt(a, b) {
830
- const extractedA = this.extractor(a);
831
- const extractedB = this.extractor(b);
832
- return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB;
833
- }
834
766
  }
835
767
  exports.BST = BST;
@@ -1,8 +1,7 @@
1
- import type { BinaryTreeDeleteResult, BTNCallback, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
2
- import { CP, CRUD, RBTNColor } from '../../types';
1
+ import type { BinaryTreeDeleteResult, BTNCallback, Comparable, CRUD, KeyOrNodeOrEntry, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
3
2
  import { BST, BSTNode } from './bst';
4
3
  import { IBinaryTree } from '../../interfaces';
5
- export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
4
+ export declare class RedBlackTreeNode<K extends Comparable, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
6
5
  /**
7
6
  * The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
8
7
  * color.
@@ -27,7 +26,7 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
27
26
  */
28
27
  set color(value: RBTNColor);
29
28
  }
30
- export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, NODE, TREE> = RedBlackTree<K, V, NODE, RedBlackTreeNested<K, V, NODE>>> extends BST<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
29
+ export declare class RedBlackTree<K extends Comparable, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, NODE, TREE> = RedBlackTree<K, V, NODE, RedBlackTreeNested<K, V, NODE>>> extends BST<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
31
30
  /**
32
31
  * This is the constructor function for a Red-Black Tree data structure in TypeScript.
33
32
  * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
@@ -255,13 +254,4 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
255
254
  * @returns void, which means it does not return any value.
256
255
  */
257
256
  protected _rightRotate(y: NODE | undefined): void;
258
- /**
259
- * The function compares two values using a comparator function and returns whether the first value
260
- * is greater than, less than, or equal to the second value.
261
- * @param {K} a - The parameter "a" is of type K.
262
- * @param {K} b - The parameter "b" in the above code represents a K.
263
- * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
264
- * than), 'LT' (less than), or 'EQ' (equal).
265
- */
266
- protected _compare(a: K, b: K): CP;
267
257
  }
@@ -314,10 +314,11 @@ class RedBlackTree extends bst_1.BST {
314
314
  let parent = undefined;
315
315
  while (this.isRealNode(current)) {
316
316
  parent = current;
317
- if (node.key < current.key) {
317
+ const compared = this.comparator(node.key, current.key);
318
+ if (compared < 0) {
318
319
  current = (_a = current.left) !== null && _a !== void 0 ? _a : this.NIL;
319
320
  }
320
- else if (node.key > current.key) {
321
+ else if (compared > 0) {
321
322
  current = (_b = current.right) !== null && _b !== void 0 ? _b : this.NIL;
322
323
  }
323
324
  else {
@@ -599,23 +600,5 @@ class RedBlackTree extends bst_1.BST {
599
600
  x.right = y;
600
601
  y.parent = x;
601
602
  }
602
- /**
603
- * The function compares two values using a comparator function and returns whether the first value
604
- * is greater than, less than, or equal to the second value.
605
- * @param {K} a - The parameter "a" is of type K.
606
- * @param {K} b - The parameter "b" in the above code represents a K.
607
- * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
608
- * than), 'LT' (less than), or 'EQ' (equal).
609
- */
610
- _compare(a, b) {
611
- const extractedA = this.extractor(a);
612
- const extractedB = this.extractor(b);
613
- const compared = extractedA - extractedB;
614
- if (compared > 0)
615
- return 'GT';
616
- if (compared < 0)
617
- return 'LT';
618
- return 'EQ';
619
- }
620
603
  }
621
604
  exports.RedBlackTree = RedBlackTree;
@@ -5,11 +5,10 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
9
- import { RBTNColor } from '../../types';
8
+ import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, Comparable, IterationType, KeyOrNodeOrEntry, RBTNColor, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
10
9
  import { IBinaryTree } from '../../interfaces';
11
10
  import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
12
- export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> {
11
+ export declare class TreeMultiMapNode<K extends Comparable, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> {
13
12
  /**
14
13
  * The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
15
14
  * @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
@@ -36,7 +35,7 @@ export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMa
36
35
  */
37
36
  set count(value: number);
38
37
  }
39
- export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>, TREE extends TreeMultiMap<K, V, NODE, TREE> = TreeMultiMap<K, V, NODE, TreeMultiMapNested<K, V, NODE>>> extends RedBlackTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
38
+ export declare class TreeMultiMap<K extends Comparable, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>, TREE extends TreeMultiMap<K, V, NODE, TREE> = TreeMultiMap<K, V, NODE, TreeMultiMapNested<K, V, NODE>>> extends RedBlackTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
40
39
  /**
41
40
  * The constructor function initializes a new instance of the TreeMultiMap class with optional
42
41
  * initial keys, nodes, or entries.
@@ -57,9 +57,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
57
57
  * @param elements
58
58
  * @param options
59
59
  */
60
- static heapify<E>(elements: Iterable<E>, options: {
61
- comparator: Comparator<E>;
62
- }): Heap<E>;
60
+ static heapify<E>(elements: Iterable<E>, options: HeapOptions<E>): Heap<E>;
63
61
  /**
64
62
  * Time Complexity: O(log n)
65
63
  * Space Complexity: O(1)
@@ -1,8 +1,8 @@
1
1
  import { BinaryTree, BinaryTreeNode } from '../data-structures';
2
- import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, KeyOrNodeOrEntry } 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>> {
2
+ import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, Comparable, KeyOrNodeOrEntry } from '../types';
3
+ export interface IBinaryTree<K extends Comparable, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTreeNested<K, V, N>> {
4
4
  createNode(key: K, value?: N['value']): N;
5
- createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
5
+ createTree(options?: Partial<BinaryTreeOptions>): TREE;
6
6
  add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean;
7
7
  addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[];
8
8
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<N>[];
@@ -1,5 +1,5 @@
1
1
  export type BSTVariant = 'STANDARD' | 'INVERSE';
2
- export type CP = 'LT' | 'EQ' | 'GT';
2
+ export type CP = 1 | -1 | 0;
3
3
  /**
4
4
  * Enum representing different loop types.
5
5
  *