linked-list-typed 1.48.1 → 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 (76) hide show
  1. package/dist/data-structures/base/index.d.ts +1 -0
  2. package/dist/data-structures/base/index.js +17 -0
  3. package/dist/data-structures/base/iterable-base.d.ts +232 -0
  4. package/dist/data-structures/base/iterable-base.js +312 -0
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +16 -16
  6. package/dist/data-structures/binary-tree/avl-tree.js +7 -7
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +121 -152
  8. package/dist/data-structures/binary-tree/binary-tree.js +140 -182
  9. package/dist/data-structures/binary-tree/bst.d.ts +28 -47
  10. package/dist/data-structures/binary-tree/bst.js +54 -57
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +15 -15
  12. package/dist/data-structures/binary-tree/rb-tree.js +7 -7
  13. package/dist/data-structures/binary-tree/tree-multimap.d.ts +22 -22
  14. package/dist/data-structures/binary-tree/tree-multimap.js +11 -11
  15. package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
  16. package/dist/data-structures/graph/abstract-graph.js +50 -27
  17. package/dist/data-structures/hash/hash-map.d.ts +59 -100
  18. package/dist/data-structures/hash/hash-map.js +69 -173
  19. package/dist/data-structures/heap/heap.d.ts +50 -7
  20. package/dist/data-structures/heap/heap.js +60 -30
  21. package/dist/data-structures/index.d.ts +1 -0
  22. package/dist/data-structures/index.js +1 -0
  23. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  24. package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
  25. package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  26. package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
  27. package/dist/data-structures/queue/deque.d.ts +29 -51
  28. package/dist/data-structures/queue/deque.js +36 -71
  29. package/dist/data-structures/queue/queue.d.ts +49 -48
  30. package/dist/data-structures/queue/queue.js +69 -82
  31. package/dist/data-structures/stack/stack.d.ts +43 -10
  32. package/dist/data-structures/stack/stack.js +50 -31
  33. package/dist/data-structures/trie/trie.d.ts +41 -6
  34. package/dist/data-structures/trie/trie.js +53 -32
  35. package/dist/interfaces/binary-tree.d.ts +6 -6
  36. package/dist/types/common.d.ts +11 -8
  37. package/dist/types/common.js +6 -1
  38. package/dist/types/data-structures/base/base.d.ts +5 -0
  39. package/dist/types/data-structures/base/base.js +2 -0
  40. package/dist/types/data-structures/base/index.d.ts +1 -0
  41. package/dist/types/data-structures/base/index.js +17 -0
  42. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
  43. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  44. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
  45. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  46. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
  47. package/dist/types/data-structures/index.d.ts +1 -0
  48. package/dist/types/data-structures/index.js +1 -0
  49. package/package.json +2 -2
  50. package/src/data-structures/base/index.ts +1 -0
  51. package/src/data-structures/base/iterable-base.ts +329 -0
  52. package/src/data-structures/binary-tree/avl-tree.ts +20 -21
  53. package/src/data-structures/binary-tree/binary-tree.ts +222 -267
  54. package/src/data-structures/binary-tree/bst.ts +86 -82
  55. package/src/data-structures/binary-tree/rb-tree.ts +25 -26
  56. package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
  57. package/src/data-structures/graph/abstract-graph.ts +55 -28
  58. package/src/data-structures/hash/hash-map.ts +76 -185
  59. package/src/data-structures/heap/heap.ts +63 -36
  60. package/src/data-structures/index.ts +1 -0
  61. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  62. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  63. package/src/data-structures/queue/deque.ts +40 -82
  64. package/src/data-structures/queue/queue.ts +72 -87
  65. package/src/data-structures/stack/stack.ts +53 -34
  66. package/src/data-structures/trie/trie.ts +58 -35
  67. package/src/interfaces/binary-tree.ts +5 -6
  68. package/src/types/common.ts +11 -8
  69. package/src/types/data-structures/base/base.ts +6 -0
  70. package/src/types/data-structures/base/index.ts +1 -0
  71. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
  72. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
  73. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  74. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  75. package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
  76. package/src/types/data-structures/index.ts +1 -0
@@ -5,13 +5,13 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNested, BSTNodeKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNKey, BTNodeExemplar, Comparator } from '../../types';
9
- import { CP, IterationType } from '../../types';
8
+ import type { BSTNested, BSTNodeKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNodeExemplar } from '../../types';
9
+ import { BSTVariant, CP, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
12
- export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
12
+ export declare class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, N> {
13
13
  parent?: N;
14
- constructor(key: BTNKey, value?: V);
14
+ constructor(key: K, value?: V);
15
15
  protected _left?: N;
16
16
  /**
17
17
  * Get the left child node.
@@ -42,7 +42,7 @@ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>
42
42
  * 6. Balance Variability: Can become unbalanced; special types maintain balance.
43
43
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
44
44
  */
45
- export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>, TREE extends BST<V, N, TREE> = BST<V, N, BSTNested<V, N>>> extends BinaryTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
45
+ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, N, TREE> = BST<K, V, N, BSTNested<K, V, N>>> extends BinaryTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
46
46
  /**
47
47
  * This is the constructor function for a binary search tree class in TypeScript, which initializes
48
48
  * the tree with optional elements and options.
@@ -51,19 +51,20 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
51
51
  * @param [options] - The `options` parameter is an optional object that can contain additional
52
52
  * configuration options for the binary search tree. It can have the following properties:
53
53
  */
54
- constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<BSTOptions>);
54
+ constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<BSTOptions<K>>);
55
55
  protected _root?: N;
56
56
  get root(): N | undefined;
57
- comparator: Comparator<BTNKey>;
57
+ protected _variant: BSTVariant;
58
+ get variant(): BSTVariant;
58
59
  /**
59
60
  * The function creates a new binary search tree node with the given key and value.
60
- * @param {BTNKey} key - The key parameter is the key value that will be associated with
61
+ * @param {K} key - The key parameter is the key value that will be associated with
61
62
  * the new node. It is used to determine the position of the node in the binary search tree.
62
63
  * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
63
64
  * represents the value associated with the node in a binary search tree.
64
65
  * @returns a new instance of the BSTNode class with the specified key and value.
65
66
  */
66
- createNode(key: BTNKey, value?: V): N;
67
+ createNode(key: K, value?: V): N;
67
68
  /**
68
69
  * The function creates a new binary search tree with the specified options.
69
70
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
@@ -71,20 +72,20 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
71
72
  * that defines various options for creating a binary search tree.
72
73
  * @returns a new instance of the BST class with the specified options.
73
74
  */
74
- createTree(options?: Partial<BSTOptions>): TREE;
75
+ createTree(options?: Partial<BSTOptions<K>>): TREE;
75
76
  /**
76
77
  * The function checks if an exemplar is an instance of BSTNode.
77
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
78
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V, N>`.
78
79
  * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
79
80
  */
80
- isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
81
+ isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
81
82
  /**
82
83
  * The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
83
84
  * is valid, otherwise it returns undefined.
84
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
85
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
85
86
  * @returns a variable `node` which is of type `N` or `undefined`.
86
87
  */
87
- exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined;
88
+ exemplarToNode(exemplar: BTNodeExemplar<K, V, N>): N | undefined;
88
89
  /**
89
90
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
90
91
  * Space Complexity: O(1) - Constant space is used.
@@ -99,7 +100,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
99
100
  * @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
100
101
  * (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
101
102
  */
102
- add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
103
+ add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>): N | undefined;
103
104
  /**
104
105
  * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
105
106
  * Space Complexity: O(k) - Additional space is required for the sorted array.
@@ -120,27 +121,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
120
121
  * tree instance.
121
122
  * @returns The `addMany` function returns an array of `N` or `undefined` values.
122
123
  */
123
- addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
124
- /**
125
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
126
- * Space Complexity: O(n) - Additional space is required for the sorted array.
127
- */
128
- /**
129
- * Time Complexity: O(log n) - Average case for a balanced tree.
130
- * Space Complexity: O(1) - Constant space is used.
131
- *
132
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
133
- * leftmost node if the comparison result is greater than.
134
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
135
- * type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
136
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
137
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
138
- * be performed. It can have one of the following values:
139
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
140
- * the key of the leftmost node if the comparison result is greater than, and the key of the
141
- * rightmost node otherwise. If no node is found, it returns 0.
142
- */
143
- lastKey(beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): BTNKey;
124
+ addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
144
125
  /**
145
126
  * Time Complexity: O(log n) - Average case for a balanced tree.
146
127
  * Space Complexity: O(1) - Constant space is used.
@@ -151,7 +132,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
151
132
  *
152
133
  * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
153
134
  * either recursive or iterative methods.
154
- * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
135
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
155
136
  * It is used to identify the node that we want to retrieve.
156
137
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
157
138
  * type of iteration to use when searching for a node in the binary tree. It can have two possible
@@ -159,7 +140,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
159
140
  * @returns The function `getNodeByKey` returns a node (`N`) if a node with the specified key is
160
141
  * found in the binary tree. If no node is found, it returns `undefined`.
161
142
  */
162
- getNodeByKey(key: BTNKey, iterationType?: IterationType): N | undefined;
143
+ getNodeByKey(key: K, iterationType?: IterationType): N | undefined;
163
144
  /**
164
145
  * Time Complexity: O(log n) - Average case for a balanced tree.
165
146
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -167,13 +148,13 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
167
148
  /**
168
149
  * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
169
150
  * otherwise it returns the key itself.
170
- * @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
151
+ * @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
171
152
  * `undefined`.
172
153
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
173
154
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
174
155
  * @returns either a node object (N) or undefined.
175
156
  */
176
- ensureNode(key: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N | undefined;
157
+ ensureNode(key: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): N | undefined;
177
158
  /**
178
159
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
179
160
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -190,14 +171,14 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
190
171
  * first node that matches the identifier. If set to true, the function will return an array
191
172
  * containing only the first matching node. If set to false (default), the function will continue
192
173
  * searching for all nodes that match the identifier and return an array containing
193
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
174
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
194
175
  * for the traversal. It can be either a key value or a node object. If it is undefined, the
195
176
  * traversal will start from the root of the tree.
196
177
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be
197
178
  * performed on the binary tree. It can have two possible values:
198
179
  * @returns The method returns an array of nodes (`N[]`).
199
180
  */
200
- getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N[];
181
+ getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): N[];
201
182
  /**
202
183
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
203
184
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -215,7 +196,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
215
196
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
216
197
  * `CP`, which is a custom type representing the comparison operator. The possible values for
217
198
  * `lesserOrGreater` are
218
- * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter represents the node in the
199
+ * @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
219
200
  * binary tree that you want to traverse from. It can be specified either by its key, by the node
220
201
  * object itself, or it can be left undefined to start the traversal from the root of the tree.
221
202
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -223,7 +204,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
223
204
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
224
205
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
225
206
  */
226
- lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): ReturnType<C>[];
207
+ lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): ReturnType<C>[];
227
208
  /**
228
209
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
229
210
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -267,10 +248,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
267
248
  /**
268
249
  * The function compares two values using a comparator function and returns whether the first value
269
250
  * is greater than, less than, or equal to the second value.
270
- * @param {BTNKey} a - The parameter "a" is of type BTNKey.
271
- * @param {BTNKey} b - The parameter "b" in the above code represents a BTNKey.
251
+ * @param {K} a - The parameter "a" is of type K.
252
+ * @param {K} b - The parameter "b" in the above code represents a K.
272
253
  * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
273
254
  * than), CP.lt (less than), or CP.eq (equal).
274
255
  */
275
- protected _compare(a: BTNKey, b: BTNKey): CP;
256
+ protected _compare(a: K, b: K): CP;
276
257
  }
@@ -65,11 +65,11 @@ class BST extends binary_tree_1.BinaryTree {
65
65
  */
66
66
  constructor(elements, options) {
67
67
  super([], options);
68
- this.comparator = (a, b) => a - b;
68
+ this._variant = types_1.BSTVariant.MIN;
69
69
  if (options) {
70
- const { comparator } = options;
71
- if (comparator) {
72
- this.comparator = comparator;
70
+ const { variant } = options;
71
+ if (variant) {
72
+ this._variant = variant;
73
73
  }
74
74
  }
75
75
  this._root = undefined;
@@ -79,9 +79,12 @@ class BST extends binary_tree_1.BinaryTree {
79
79
  get root() {
80
80
  return this._root;
81
81
  }
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.
@@ -98,11 +101,11 @@ class BST extends binary_tree_1.BinaryTree {
98
101
  * @returns a new instance of the BST class with the specified options.
99
102
  */
100
103
  createTree(options) {
101
- return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
104
+ return new BST([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
102
105
  }
103
106
  /**
104
107
  * The function checks if an exemplar is an instance of BSTNode.
105
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
108
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V, N>`.
106
109
  * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
107
110
  */
108
111
  isNode(exemplar) {
@@ -111,7 +114,7 @@ class BST extends binary_tree_1.BinaryTree {
111
114
  /**
112
115
  * The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
113
116
  * is valid, otherwise it returns undefined.
114
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
117
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
115
118
  * @returns a variable `node` which is of type `N` or `undefined`.
116
119
  */
117
120
  exemplarToNode(exemplar) {
@@ -131,7 +134,7 @@ class BST extends binary_tree_1.BinaryTree {
131
134
  node = this.createNode(key, value);
132
135
  }
133
136
  }
134
- else if (this.isNodeKey(exemplar)) {
137
+ else if (this.isNotNodeInstance(exemplar)) {
135
138
  node = this.createNode(exemplar);
136
139
  }
137
140
  else {
@@ -239,17 +242,17 @@ class BST extends binary_tree_1.BinaryTree {
239
242
  sorted = realBTNExemplars.sort((a, b) => {
240
243
  let aR, bR;
241
244
  if (this.isEntry(a))
242
- aR = a[0];
245
+ aR = this.extractor(a[0]);
243
246
  else if (this.isRealNode(a))
244
- aR = a.key;
247
+ aR = this.extractor(a.key);
245
248
  else
246
- aR = a;
249
+ aR = this.extractor(a);
247
250
  if (this.isEntry(b))
248
- bR = b[0];
251
+ bR = this.extractor(b[0]);
249
252
  else if (this.isRealNode(b))
250
- bR = b.key;
253
+ bR = this.extractor(b.key);
251
254
  else
252
- bR = b;
255
+ bR = this.extractor(b);
253
256
  return aR - bR;
254
257
  });
255
258
  const _dfs = (arr) => {
@@ -286,34 +289,31 @@ class BST extends binary_tree_1.BinaryTree {
286
289
  }
287
290
  return inserted;
288
291
  }
289
- /**
290
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
291
- * Space Complexity: O(n) - Additional space is required for the sorted array.
292
- */
293
- /**
294
- * Time Complexity: O(log n) - Average case for a balanced tree.
295
- * Space Complexity: O(1) - Constant space is used.
296
- *
297
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
298
- * leftmost node if the comparison result is greater than.
299
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
300
- * type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
301
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
302
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
303
- * be performed. It can have one of the following values:
304
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
305
- * the key of the leftmost node if the comparison result is greater than, and the key of the
306
- * rightmost node otherwise. If no node is found, it returns 0.
307
- */
308
- lastKey(beginRoot = this.root, iterationType = this.iterationType) {
309
- var _a, _b, _c, _d, _e, _f;
310
- if (this._compare(0, 1) === types_1.CP.lt)
311
- return (_b = (_a = this.getRightMost(beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
312
- else if (this._compare(0, 1) === types_1.CP.gt)
313
- return (_d = (_c = this.getLeftMost(beginRoot, iterationType)) === null || _c === void 0 ? void 0 : _c.key) !== null && _d !== void 0 ? _d : 0;
314
- else
315
- return (_f = (_e = this.getRightMost(beginRoot, iterationType)) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
316
- }
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
+ // /**
298
+ // * Time Complexity: O(log n) - Average case for a balanced tree.
299
+ // * Space Complexity: O(1) - Constant space is used.
300
+ // *
301
+ // * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
302
+ // * leftmost node if the comparison result is greater than.
303
+ // * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
304
+ // * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
305
+ // * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
306
+ // * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
307
+ // * be performed. It can have one of the following values:
308
+ // * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
309
+ // * the key of the leftmost node if the comparison result is greater than, and the key of the
310
+ // * rightmost node otherwise. If no node is found, it returns 0.
311
+ // */
312
+ // lastKey(beginRoot: BSTNodeKeyOrNode<K,N> = this.root, iterationType = this.iterationType): K {
313
+ // if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
314
+ // else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
315
+ // else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
316
+ // }
317
317
  /**
318
318
  * Time Complexity: O(log n) - Average case for a balanced tree.
319
319
  * Space Complexity: O(1) - Constant space is used.
@@ -324,7 +324,7 @@ class BST extends binary_tree_1.BinaryTree {
324
324
  *
325
325
  * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
326
326
  * either recursive or iterative methods.
327
- * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
327
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
328
328
  * It is used to identify the node that we want to retrieve.
329
329
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
330
330
  * type of iteration to use when searching for a node in the binary tree. It can have two possible
@@ -370,14 +370,14 @@ class BST extends binary_tree_1.BinaryTree {
370
370
  /**
371
371
  * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
372
372
  * otherwise it returns the key itself.
373
- * @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
373
+ * @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
374
374
  * `undefined`.
375
375
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
376
376
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
377
377
  * @returns either a node object (N) or undefined.
378
378
  */
379
379
  ensureNode(key, iterationType = types_1.IterationType.ITERATIVE) {
380
- return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
380
+ return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
381
381
  }
382
382
  /**
383
383
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
@@ -395,7 +395,7 @@ class BST extends binary_tree_1.BinaryTree {
395
395
  * first node that matches the identifier. If set to true, the function will return an array
396
396
  * containing only the first matching node. If set to false (default), the function will continue
397
397
  * searching for all nodes that match the identifier and return an array containing
398
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
398
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
399
399
  * for the traversal. It can be either a key value or a node object. If it is undefined, the
400
400
  * traversal will start from the root of the tree.
401
401
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be
@@ -475,7 +475,7 @@ class BST extends binary_tree_1.BinaryTree {
475
475
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
476
476
  * `CP`, which is a custom type representing the comparison operator. The possible values for
477
477
  * `lesserOrGreater` are
478
- * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter represents the node in the
478
+ * @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
479
479
  * binary tree that you want to traverse from. It can be specified either by its key, by the node
480
480
  * object itself, or it can be left undefined to start the traversal from the root of the tree.
481
481
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -652,19 +652,16 @@ class BST extends binary_tree_1.BinaryTree {
652
652
  /**
653
653
  * The function compares two values using a comparator function and returns whether the first value
654
654
  * is greater than, less than, or equal to the second value.
655
- * @param {BTNKey} a - The parameter "a" is of type BTNKey.
656
- * @param {BTNKey} b - The parameter "b" in the above code represents a BTNKey.
655
+ * @param {K} a - The parameter "a" is of type K.
656
+ * @param {K} b - The parameter "b" in the above code represents a K.
657
657
  * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
658
658
  * than), CP.lt (less than), or CP.eq (equal).
659
659
  */
660
660
  _compare(a, b) {
661
- const compared = this.comparator(a, b);
662
- if (compared > 0)
663
- return types_1.CP.gt;
664
- else if (compared < 0)
665
- return types_1.CP.lt;
666
- else
667
- return types_1.CP.eq;
661
+ const extractedA = this.extractor(a);
662
+ const extractedB = this.extractor(b);
663
+ const compared = this.variant === types_1.BSTVariant.MIN ? extractedA - extractedB : extractedB - extractedA;
664
+ return compared > 0 ? types_1.CP.gt : compared < 0 ? types_1.CP.lt : types_1.CP.eq;
668
665
  }
669
666
  }
670
667
  exports.BST = BST;
@@ -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
  /**
@@ -29,7 +29,7 @@ class RedBlackTree extends bst_1.BST {
29
29
  /**
30
30
  * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
31
31
  * initializes the tree with optional elements and options.
32
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
32
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
33
33
  * objects. It represents the initial elements that will be added to the RBTree during its
34
34
  * construction. If this parameter is provided, the `addMany` method is called to add all the
35
35
  * elements to the
@@ -53,7 +53,7 @@ class RedBlackTree extends bst_1.BST {
53
53
  }
54
54
  /**
55
55
  * The function creates a new Red-Black Tree node with the specified key, value, and color.
56
- * @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
56
+ * @param {K} key - The key parameter is the key value associated with the node. It is used to
57
57
  * identify and compare nodes in the Red-Black Tree.
58
58
  * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
59
59
  * associated with the node. It is of type `V`, which is a generic type that can be replaced with any
@@ -74,11 +74,11 @@ class RedBlackTree extends bst_1.BST {
74
74
  * @returns a new instance of a RedBlackTree object.
75
75
  */
76
76
  createTree(options) {
77
- return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
77
+ return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
78
78
  }
79
79
  /**
80
80
  * The function checks if an exemplar is an instance of the RedBlackTreeNode class.
81
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
81
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
82
82
  * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
83
83
  * class.
84
84
  */
@@ -88,7 +88,7 @@ class RedBlackTree extends bst_1.BST {
88
88
  /**
89
89
  * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
90
90
  * otherwise it returns undefined.
91
- * @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
91
+ * @param exemplar - BTNodeExemplar<K, V, N> - A generic type representing an exemplar of a binary tree
92
92
  * node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
93
93
  * that is not a valid exemplar.
94
94
  * @returns a variable `node` which is of type `N | undefined`.
@@ -110,7 +110,7 @@ class RedBlackTree extends bst_1.BST {
110
110
  node = this.createNode(key, value, types_1.RBTNColor.RED);
111
111
  }
112
112
  }
113
- else if (this.isNodeKey(exemplar)) {
113
+ else if (this.isNotNodeInstance(exemplar)) {
114
114
  node = this.createNode(exemplar, undefined, types_1.RBTNColor.RED);
115
115
  }
116
116
  else {
@@ -276,7 +276,7 @@ class RedBlackTree extends bst_1.BST {
276
276
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
277
277
  * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
278
278
  * function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
279
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is the starting point for
279
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is the starting point for
280
280
  * searching for a node in a binary tree. It can be either a key value or a node object. If it is not
281
281
  * provided, the search will start from the root of the binary tree.
282
282
  * @param iterationType - The `iterationType` parameter is a variable that determines the type of