bst-typed 1.47.5 → 1.47.7

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 (71) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +36 -18
  2. package/dist/data-structures/binary-tree/avl-tree.js +46 -29
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -129
  4. package/dist/data-structures/binary-tree/binary-tree.js +182 -184
  5. package/dist/data-structures/binary-tree/bst.d.ts +73 -63
  6. package/dist/data-structures/binary-tree/bst.js +168 -169
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -17
  8. package/dist/data-structures/binary-tree/rb-tree.js +77 -31
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +29 -40
  10. package/dist/data-structures/binary-tree/tree-multimap.js +66 -136
  11. package/dist/data-structures/graph/abstract-graph.js +1 -1
  12. package/dist/data-structures/hash/hash-map.d.ts +2 -6
  13. package/dist/data-structures/hash/hash-map.js +5 -8
  14. package/dist/data-structures/heap/heap.d.ts +19 -21
  15. package/dist/data-structures/heap/heap.js +52 -34
  16. package/dist/data-structures/heap/max-heap.d.ts +2 -5
  17. package/dist/data-structures/heap/max-heap.js +2 -2
  18. package/dist/data-structures/heap/min-heap.d.ts +2 -5
  19. package/dist/data-structures/heap/min-heap.js +2 -2
  20. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  21. package/dist/data-structures/linked-list/doubly-linked-list.js +9 -1
  22. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  23. package/dist/data-structures/linked-list/singly-linked-list.js +8 -1
  24. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  25. package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
  26. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  27. package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
  28. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
  29. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  30. package/dist/data-structures/queue/deque.d.ts +1 -0
  31. package/dist/data-structures/queue/deque.js +3 -0
  32. package/dist/data-structures/queue/queue.d.ts +1 -0
  33. package/dist/data-structures/queue/queue.js +3 -0
  34. package/dist/data-structures/stack/stack.d.ts +2 -1
  35. package/dist/data-structures/stack/stack.js +10 -2
  36. package/dist/data-structures/trie/trie.d.ts +3 -0
  37. package/dist/data-structures/trie/trie.js +19 -4
  38. package/dist/interfaces/binary-tree.d.ts +4 -2
  39. package/dist/types/common.d.ts +7 -0
  40. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  41. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  42. package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
  43. package/dist/types/data-structures/heap/heap.d.ts +4 -1
  44. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  45. package/package.json +2 -2
  46. package/src/data-structures/binary-tree/avl-tree.ts +61 -31
  47. package/src/data-structures/binary-tree/binary-tree.ts +283 -254
  48. package/src/data-structures/binary-tree/bst.ts +193 -170
  49. package/src/data-structures/binary-tree/rb-tree.ts +87 -32
  50. package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
  51. package/src/data-structures/graph/abstract-graph.ts +1 -1
  52. package/src/data-structures/hash/hash-map.ts +8 -8
  53. package/src/data-structures/heap/heap.ts +57 -39
  54. package/src/data-structures/heap/max-heap.ts +5 -5
  55. package/src/data-structures/heap/min-heap.ts +5 -5
  56. package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
  57. package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
  58. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  59. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  60. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  61. package/src/data-structures/queue/deque.ts +4 -0
  62. package/src/data-structures/queue/queue.ts +4 -0
  63. package/src/data-structures/stack/stack.ts +12 -3
  64. package/src/data-structures/trie/trie.ts +23 -4
  65. package/src/interfaces/binary-tree.ts +14 -2
  66. package/src/types/common.ts +15 -1
  67. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  68. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  69. package/src/types/data-structures/hash/hash-map.ts +1 -2
  70. package/src/types/data-structures/heap/heap.ts +3 -1
  71. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
@@ -5,7 +5,7 @@
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, BTNKey } from '../../types';
8
+ import type { BSTNested, BSTNodeKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNKey, BTNodeExemplar, Comparator } from '../../types';
9
9
  import { CP, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
@@ -33,19 +33,28 @@ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>
33
33
  */
34
34
  set right(v: N | undefined);
35
35
  }
36
+ /**
37
+ * 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
38
+ * 2. Unique Keys: No duplicate keys in a standard BST.
39
+ * 3. Efficient Search: Enables quick search, minimum, and maximum operations.
40
+ * 4. Inorder Traversal: Yields nodes in ascending order.
41
+ * 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
42
+ * 6. Balance Variability: Can become unbalanced; special types maintain balance.
43
+ * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
44
+ */
36
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> {
37
- options: BSTOptions;
38
46
  /**
39
- * The constructor function initializes a binary search tree with an optional comparator function.
40
- * @param {BSTOptions} [options] - An optional object that contains additional configuration options
41
- * for the binary search tree.
47
+ * This is the constructor function for a binary search tree class in TypeScript, which initializes
48
+ * the tree with optional elements and options.
49
+ * @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
50
+ * binary search tree.
51
+ * @param [options] - The `options` parameter is an optional object that can contain additional
52
+ * configuration options for the binary search tree. It can have the following properties:
42
53
  */
43
- constructor(options?: BSTOptions);
54
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<BSTOptions>);
44
55
  protected _root?: N;
45
- /**
46
- * Get the root node of the binary tree.
47
- */
48
56
  get root(): N | undefined;
57
+ comparator: Comparator<BTNKey>;
49
58
  /**
50
59
  * The function creates a new binary search tree node with the given key and value.
51
60
  * @param {BTNKey} key - The key parameter is the key value that will be associated with
@@ -55,7 +64,14 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
55
64
  * @returns a new instance of the BSTNode class with the specified key and value.
56
65
  */
57
66
  createNode(key: BTNKey, value?: V): N;
58
- createTree(options?: BSTOptions): TREE;
67
+ /**
68
+ * The function creates a new binary search tree with the specified options.
69
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
70
+ * behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
71
+ * that defines various options for creating a binary search tree.
72
+ * @returns a new instance of the BST class with the specified options.
73
+ */
74
+ createTree(options?: Partial<BSTOptions>): TREE;
59
75
  /**
60
76
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
61
77
  * Space Complexity: O(1) - Constant space is used.
@@ -64,43 +80,37 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
64
80
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
65
81
  * Space Complexity: O(1) - Constant space is used.
66
82
  *
67
- * The `add` function adds a new node to a binary search tree based on the provided key and value.
68
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
69
- * following types:
70
- * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
71
- * key or node being added to the binary search tree.
72
- * @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
73
- * no node was inserted, it returns `undefined`.
83
+ * The `add` function adds a new node to a binary search tree, either by key or by providing a node
84
+ * object.
85
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
86
+ * @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
87
+ * (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
74
88
  */
75
- add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
89
+ add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
76
90
  /**
77
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
78
- * Space Complexity: O(n) - Additional space is required for the sorted array.
91
+ * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
92
+ * Space Complexity: O(k) - Additional space is required for the sorted array.
79
93
  */
80
94
  /**
81
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
82
- * Space Complexity: O(n) - Additional space is required for the sorted array.
95
+ * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
96
+ * Space Complexity: O(k) - Additional space is required for the sorted array.
83
97
  *
84
- * The `addMany` function is used to efficiently add multiple keys or nodes with corresponding data
85
- * to a binary search tree.
86
- * @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes to be added to the
87
- * binary search tree. Each element can be of type `BTNKey` (binary tree node key), `N` (binary tree
88
- * node), or `undefined`.
89
- * @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
90
- * nodes being added. If provided, the length of the `data` array must be the same as the length of
91
- * the `keysOrNodes` array.
98
+ * The `addMany` function in TypeScript adds multiple nodes to a binary tree, either in a balanced or
99
+ * unbalanced manner, and returns an array of the inserted nodes.
100
+ * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
101
+ * binary tree.
92
102
  * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
93
- * adding the nodes. The default value is `true`.
103
+ * adding the nodes. The default value is true.
94
104
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
95
- * type of iteration to use when adding multiple keys or nodes to the binary search tree. It has a
96
- * default value of `this.iterationType`, which means it will use the iteration type specified in the
97
- * current instance of the binary search tree
98
- * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
105
+ * type of iteration to use when adding multiple keys or nodes to the binary tree. It has a default
106
+ * value of `this.iterationType`, which means it will use the iteration type specified by the binary
107
+ * tree instance.
108
+ * @returns The `addMany` function returns an array of `N` or `undefined` values.
99
109
  */
100
- addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: (V | undefined)[], isBalanceAdd?: boolean, iterationType?: IterationType | undefined): (N | undefined)[];
110
+ addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
101
111
  /**
102
- * Time Complexity: O(log n) - Average case for a balanced tree.
103
- * Space Complexity: O(1) - Constant space is used.
112
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
113
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
104
114
  */
105
115
  /**
106
116
  * Time Complexity: O(log n) - Average case for a balanced tree.
@@ -117,10 +127,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
117
127
  * the key of the leftmost node if the comparison result is greater than, and the key of the
118
128
  * rightmost node otherwise. If no node is found, it returns 0.
119
129
  */
120
- lastKey(beginRoot?: BTNKey | N | undefined, iterationType?: IterationType | undefined): BTNKey;
130
+ lastKey(beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): BTNKey;
121
131
  /**
122
132
  * Time Complexity: O(log n) - Average case for a balanced tree.
123
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
133
+ * Space Complexity: O(1) - Constant space is used.
124
134
  */
125
135
  /**
126
136
  * Time Complexity: O(log n) - Average case for a balanced tree.
@@ -138,7 +148,11 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
138
148
  */
139
149
  getNodeByKey(key: BTNKey, iterationType?: IterationType): N | undefined;
140
150
  /**
141
- * The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
151
+ * Time Complexity: O(log n) - Average case for a balanced tree.
152
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
153
+ */
154
+ /**
155
+ * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
142
156
  * otherwise it returns the key itself.
143
157
  * @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
144
158
  * `undefined`.
@@ -146,11 +160,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
146
160
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
147
161
  * @returns either a node object (N) or undefined.
148
162
  */
149
- ensureNotKey(key: BTNKey | N | undefined, iterationType?: IterationType): N | undefined;
150
- /**
151
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
152
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
153
- */
163
+ ensureNode(key: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N | undefined;
154
164
  /**
155
165
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
156
166
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -174,7 +184,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
174
184
  * performed on the binary tree. It can have two possible values:
175
185
  * @returns The method returns an array of nodes (`N[]`).
176
186
  */
177
- getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | undefined, iterationType?: IterationType | undefined): N[];
187
+ getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N[];
178
188
  /**
179
189
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
180
190
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -200,19 +210,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
200
210
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
201
211
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
202
212
  */
203
- lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | undefined, iterationType?: IterationType | undefined): ReturnType<C>[];
204
- /**
205
- * Balancing Adjustment:
206
- * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
207
- * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
208
- *
209
- * Use Cases and Efficiency:
210
- * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
211
- * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
212
- */
213
+ lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): ReturnType<C>[];
213
214
  /**
214
- * Time Complexity: O(n) - Building a balanced tree from a sorted array.
215
- * Space Complexity: O(n) - Additional space is required for the sorted array.
215
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
216
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
216
217
  */
217
218
  /**
218
219
  * Time Complexity: O(n) - Building a balanced tree from a sorted array.
@@ -225,10 +226,19 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
225
226
  * values:
226
227
  * @returns The function `perfectlyBalance` returns a boolean value.
227
228
  */
228
- perfectlyBalance(iterationType?: IterationType | undefined): boolean;
229
+ perfectlyBalance(iterationType?: IterationType): boolean;
229
230
  /**
230
- * Time Complexity: O(n) - Visiting each node once.
231
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
231
+ * Balancing Adjustment:
232
+ * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
233
+ * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
234
+ *
235
+ * Use Cases and Efficiency:
236
+ * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
237
+ * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
238
+ */
239
+ /**
240
+ * Time Complexity: O(n) - Building a balanced tree from a sorted array.
241
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
232
242
  */
233
243
  /**
234
244
  * Time Complexity: O(n) - Visiting each node once.
@@ -239,7 +249,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
239
249
  * to check if the AVL tree is balanced. It can have two possible values:
240
250
  * @returns a boolean value.
241
251
  */
242
- isAVLBalanced(iterationType?: IterationType | undefined): boolean;
252
+ isAVLBalanced(iterationType?: IterationType): boolean;
243
253
  protected _setRoot(v: N | undefined): void;
244
254
  /**
245
255
  * The function compares two values using a comparator function and returns whether the first value