heap-typed 1.53.7 → 1.53.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.
- package/dist/common/index.js +5 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +2 -2
- package/dist/data-structures/binary-tree/bst.d.ts +53 -23
- package/dist/data-structures/binary-tree/bst.js +59 -25
- package/dist/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/data-structures/binary-tree/index.js +1 -1
- package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +49 -0
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +50 -1
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
- package/dist/data-structures/hash/hash-map.d.ts +30 -0
- package/dist/data-structures/hash/hash-map.js +30 -0
- package/dist/data-structures/heap/heap.d.ts +20 -3
- package/dist/data-structures/heap/heap.js +31 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +36 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +56 -9
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +34 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +54 -10
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +8 -3
- package/dist/data-structures/trie/trie.js +8 -3
- package/package.json +2 -2
- package/src/common/index.ts +7 -1
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/data-structures/binary-tree/bst.ts +64 -25
- package/src/data-structures/binary-tree/index.ts +1 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +50 -1
- package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/data-structures/hash/hash-map.ts +30 -0
- package/src/data-structures/heap/heap.ts +33 -10
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -8
- package/src/data-structures/linked-list/singly-linked-list.ts +60 -10
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +8 -3
package/dist/common/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Range = exports.DFSOperation = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
4
5
|
var DFSOperation;
|
|
5
6
|
(function (DFSOperation) {
|
|
6
7
|
DFSOperation[DFSOperation["VISIT"] = 0] = "VISIT";
|
|
@@ -12,6 +13,10 @@ class Range {
|
|
|
12
13
|
this.high = high;
|
|
13
14
|
this.includeLow = includeLow;
|
|
14
15
|
this.includeHigh = includeHigh;
|
|
16
|
+
if (!((0, utils_1.isComparable)(low) && (0, utils_1.isComparable)(high)))
|
|
17
|
+
throw new RangeError('low or high is not comparable');
|
|
18
|
+
if (low > high)
|
|
19
|
+
throw new RangeError('low must be less than or equal to high');
|
|
15
20
|
}
|
|
16
21
|
// Determine whether a key is within the range
|
|
17
22
|
isInRange(key, comparator) {
|
|
@@ -102,7 +102,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends
|
|
|
102
102
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
103
103
|
* @returns either a NODE object or undefined.
|
|
104
104
|
*/
|
|
105
|
-
|
|
105
|
+
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined];
|
|
106
106
|
/**
|
|
107
107
|
* Time Complexity: O(log n)
|
|
108
108
|
* Space Complexity: O(1)
|
|
@@ -120,7 +120,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
120
120
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
121
121
|
* @returns either a NODE object or undefined.
|
|
122
122
|
*/
|
|
123
|
-
|
|
123
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
124
124
|
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
|
|
125
125
|
return [undefined, undefined];
|
|
126
126
|
if (this.isNode(keyNodeEntryOrRaw))
|
|
@@ -160,7 +160,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
160
160
|
* @returns a boolean value.
|
|
161
161
|
*/
|
|
162
162
|
add(keyNodeEntryOrRaw, value, count = 1) {
|
|
163
|
-
const [newNode, newValue] = this.
|
|
163
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
164
164
|
if (newNode === undefined)
|
|
165
165
|
return false;
|
|
166
166
|
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
@@ -92,7 +92,7 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
92
92
|
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
93
93
|
* value.
|
|
94
94
|
*/
|
|
95
|
-
|
|
95
|
+
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNodeOrNull<NODE>, V | undefined];
|
|
96
96
|
/**
|
|
97
97
|
* Time Complexity: O(n)
|
|
98
98
|
* Space Complexity: O(log n)
|
|
@@ -150,7 +150,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
150
150
|
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
151
151
|
* value.
|
|
152
152
|
*/
|
|
153
|
-
|
|
153
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
154
154
|
if (keyNodeEntryOrRaw === undefined)
|
|
155
155
|
return [undefined, undefined];
|
|
156
156
|
if (keyNodeEntryOrRaw === null)
|
|
@@ -346,7 +346,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
346
346
|
* key was found and the node was replaced instead of inserted.
|
|
347
347
|
*/
|
|
348
348
|
add(keyNodeEntryOrRaw, value) {
|
|
349
|
-
const [newNode, newValue] = this.
|
|
349
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
350
350
|
if (newNode === undefined)
|
|
351
351
|
return false;
|
|
352
352
|
// If the tree is empty, directly set the new node as the root node
|
|
@@ -47,32 +47,48 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
|
|
|
47
47
|
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
48
48
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
49
49
|
* @example
|
|
50
|
-
* //
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
50
|
+
* // Merge 3 sorted datasets
|
|
51
|
+
* const dataset1 = new BST<number, string>([
|
|
52
|
+
* [1, 'A'],
|
|
53
|
+
* [7, 'G']
|
|
54
|
+
* ]);
|
|
55
|
+
* const dataset2 = [
|
|
56
|
+
* [2, 'B'],
|
|
57
|
+
* [6, 'F']
|
|
58
|
+
* ];
|
|
59
|
+
* const dataset3 = new BST<number, string>([
|
|
60
|
+
* [3, 'C'],
|
|
61
|
+
* [5, 'E'],
|
|
62
|
+
* [4, 'D']
|
|
63
|
+
* ]);
|
|
54
64
|
*
|
|
55
|
-
* //
|
|
56
|
-
* const
|
|
57
|
-
*
|
|
58
|
-
*
|
|
65
|
+
* // Merge datasets into a single BinarySearchTree
|
|
66
|
+
* const merged = new BST<number, string>(dataset1);
|
|
67
|
+
* merged.addMany(dataset2);
|
|
68
|
+
* merged.merge(dataset3);
|
|
59
69
|
*
|
|
60
|
-
* //
|
|
61
|
-
* console.log(
|
|
62
|
-
* console.log(findKthSmallest(3)); // 4
|
|
63
|
-
* console.log(findKthSmallest(7)); // 8
|
|
70
|
+
* // Verify merged dataset is in sorted order
|
|
71
|
+
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
|
|
64
72
|
* @example
|
|
65
73
|
* // Find elements in a range
|
|
66
74
|
* const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
67
75
|
* console.log(bst.search(new Range(5, 10))); // [10, 5, 7]
|
|
68
|
-
* console.log(bst.
|
|
76
|
+
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7']
|
|
69
77
|
* console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7]
|
|
70
|
-
* console.log(bst.
|
|
78
|
+
* console.log(bst.rangeSearch([15, 20])); // [15, 18]
|
|
71
79
|
* console.log(bst.search(new Range(15, 20, false))); // [18]
|
|
72
80
|
* @example
|
|
73
81
|
* // Find lowest common ancestor
|
|
74
82
|
* const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
|
|
75
83
|
*
|
|
84
|
+
* // LCA helper function
|
|
85
|
+
* const findLCA = (num1: number, num2: number): number | undefined => {
|
|
86
|
+
* const path1 = bst.getPathToRoot(num1);
|
|
87
|
+
* const path2 = bst.getPathToRoot(num2);
|
|
88
|
+
* // Find the first common ancestor
|
|
89
|
+
* return findFirstCommon(path1, path2);
|
|
90
|
+
* };
|
|
91
|
+
*
|
|
76
92
|
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
|
|
77
93
|
* for (const num of arr1) {
|
|
78
94
|
* if (arr2.indexOf(num) !== -1) {
|
|
@@ -82,14 +98,6 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
|
|
|
82
98
|
* return undefined;
|
|
83
99
|
* }
|
|
84
100
|
*
|
|
85
|
-
* // LCA helper function
|
|
86
|
-
* const findLCA = (num1: number, num2: number): number | undefined => {
|
|
87
|
-
* const path1 = bst.getPathToRoot(num1);
|
|
88
|
-
* const path2 = bst.getPathToRoot(num2);
|
|
89
|
-
* // Find the first common ancestor
|
|
90
|
-
* return findFirstCommon(path1, path2);
|
|
91
|
-
* };
|
|
92
|
-
*
|
|
93
101
|
* // Assertions
|
|
94
102
|
* console.log(findLCA(3, 10)); // 7
|
|
95
103
|
* console.log(findLCA(5, 35)); // 15
|
|
@@ -144,7 +152,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
144
152
|
* value associated with a key in a key-value pair.
|
|
145
153
|
* @returns either a NODE object or undefined.
|
|
146
154
|
*/
|
|
147
|
-
|
|
155
|
+
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNode<NODE>, V | undefined];
|
|
148
156
|
/**
|
|
149
157
|
* Time Complexity: O(log n)
|
|
150
158
|
* Space Complexity: O(log n)
|
|
@@ -251,6 +259,28 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
251
259
|
* collected in an array and returned as the output of the method.
|
|
252
260
|
*/
|
|
253
261
|
search<C extends NodeCallback<NODE>>(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE> | Range<K>, onlyOne?: boolean, callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[];
|
|
262
|
+
/**
|
|
263
|
+
* Time Complexity: O(log n)
|
|
264
|
+
* Space Complexity: O(n)
|
|
265
|
+
*
|
|
266
|
+
* The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
|
|
267
|
+
* @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
|
|
268
|
+
* either a `Range` object or an array of two elements representing the range boundaries.
|
|
269
|
+
* @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
|
|
270
|
+
* function that is used to process each node that is found within the specified range during the
|
|
271
|
+
* search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the
|
|
272
|
+
* data structure.
|
|
273
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch`
|
|
274
|
+
* function represents the node from which the search for nodes within the specified range will
|
|
275
|
+
* begin. It is the starting point for the range search operation.
|
|
276
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
|
|
277
|
+
* is used to specify the type of iteration to be performed during the search operation. It has a
|
|
278
|
+
* default value of `this.iterationType`, which suggests that it is likely a property of the class or
|
|
279
|
+
* object that the `rangeSearch`
|
|
280
|
+
* @returns The `rangeSearch` function is returning the result of calling the `search` method with
|
|
281
|
+
* the specified parameters.
|
|
282
|
+
*/
|
|
283
|
+
rangeSearch<C extends NodeCallback<NODE>>(range: Range<K> | [K, K], callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[];
|
|
254
284
|
/**
|
|
255
285
|
* Time Complexity: O(log n)
|
|
256
286
|
* Space Complexity: O(1)
|
|
@@ -4,6 +4,7 @@ exports.BST = exports.BSTNode = void 0;
|
|
|
4
4
|
const binary_tree_1 = require("./binary-tree");
|
|
5
5
|
const queue_1 = require("../queue");
|
|
6
6
|
const utils_1 = require("../../utils");
|
|
7
|
+
const common_1 = require("../../common");
|
|
7
8
|
class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
8
9
|
constructor(key, value) {
|
|
9
10
|
super(key, value);
|
|
@@ -59,32 +60,48 @@ exports.BSTNode = BSTNode;
|
|
|
59
60
|
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
60
61
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
61
62
|
* @example
|
|
62
|
-
* //
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
63
|
+
* // Merge 3 sorted datasets
|
|
64
|
+
* const dataset1 = new BST<number, string>([
|
|
65
|
+
* [1, 'A'],
|
|
66
|
+
* [7, 'G']
|
|
67
|
+
* ]);
|
|
68
|
+
* const dataset2 = [
|
|
69
|
+
* [2, 'B'],
|
|
70
|
+
* [6, 'F']
|
|
71
|
+
* ];
|
|
72
|
+
* const dataset3 = new BST<number, string>([
|
|
73
|
+
* [3, 'C'],
|
|
74
|
+
* [5, 'E'],
|
|
75
|
+
* [4, 'D']
|
|
76
|
+
* ]);
|
|
66
77
|
*
|
|
67
|
-
* //
|
|
68
|
-
* const
|
|
69
|
-
*
|
|
70
|
-
*
|
|
78
|
+
* // Merge datasets into a single BinarySearchTree
|
|
79
|
+
* const merged = new BST<number, string>(dataset1);
|
|
80
|
+
* merged.addMany(dataset2);
|
|
81
|
+
* merged.merge(dataset3);
|
|
71
82
|
*
|
|
72
|
-
* //
|
|
73
|
-
* console.log(
|
|
74
|
-
* console.log(findKthSmallest(3)); // 4
|
|
75
|
-
* console.log(findKthSmallest(7)); // 8
|
|
83
|
+
* // Verify merged dataset is in sorted order
|
|
84
|
+
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
|
|
76
85
|
* @example
|
|
77
86
|
* // Find elements in a range
|
|
78
87
|
* const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
79
88
|
* console.log(bst.search(new Range(5, 10))); // [10, 5, 7]
|
|
80
|
-
* console.log(bst.
|
|
89
|
+
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7']
|
|
81
90
|
* console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7]
|
|
82
|
-
* console.log(bst.
|
|
91
|
+
* console.log(bst.rangeSearch([15, 20])); // [15, 18]
|
|
83
92
|
* console.log(bst.search(new Range(15, 20, false))); // [18]
|
|
84
93
|
* @example
|
|
85
94
|
* // Find lowest common ancestor
|
|
86
95
|
* const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
|
|
87
96
|
*
|
|
97
|
+
* // LCA helper function
|
|
98
|
+
* const findLCA = (num1: number, num2: number): number | undefined => {
|
|
99
|
+
* const path1 = bst.getPathToRoot(num1);
|
|
100
|
+
* const path2 = bst.getPathToRoot(num2);
|
|
101
|
+
* // Find the first common ancestor
|
|
102
|
+
* return findFirstCommon(path1, path2);
|
|
103
|
+
* };
|
|
104
|
+
*
|
|
88
105
|
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
|
|
89
106
|
* for (const num of arr1) {
|
|
90
107
|
* if (arr2.indexOf(num) !== -1) {
|
|
@@ -94,14 +111,6 @@ exports.BSTNode = BSTNode;
|
|
|
94
111
|
* return undefined;
|
|
95
112
|
* }
|
|
96
113
|
*
|
|
97
|
-
* // LCA helper function
|
|
98
|
-
* const findLCA = (num1: number, num2: number): number | undefined => {
|
|
99
|
-
* const path1 = bst.getPathToRoot(num1);
|
|
100
|
-
* const path2 = bst.getPathToRoot(num2);
|
|
101
|
-
* // Find the first common ancestor
|
|
102
|
-
* return findFirstCommon(path1, path2);
|
|
103
|
-
* };
|
|
104
|
-
*
|
|
105
114
|
* // Assertions
|
|
106
115
|
* console.log(findLCA(3, 10)); // 7
|
|
107
116
|
* console.log(findLCA(5, 35)); // 15
|
|
@@ -195,8 +204,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
195
204
|
* value associated with a key in a key-value pair.
|
|
196
205
|
* @returns either a NODE object or undefined.
|
|
197
206
|
*/
|
|
198
|
-
|
|
199
|
-
const [node, entryValue] = super.
|
|
207
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
208
|
+
const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
200
209
|
if (node === null)
|
|
201
210
|
return [undefined, undefined];
|
|
202
211
|
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
@@ -253,7 +262,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
253
262
|
* @returns a boolean value.
|
|
254
263
|
*/
|
|
255
264
|
add(keyNodeEntryOrRaw, value) {
|
|
256
|
-
const [newNode, newValue] = this.
|
|
265
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
257
266
|
if (newNode === undefined)
|
|
258
267
|
return false;
|
|
259
268
|
if (this._root === undefined) {
|
|
@@ -547,6 +556,31 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
547
556
|
}
|
|
548
557
|
return ans;
|
|
549
558
|
}
|
|
559
|
+
/**
|
|
560
|
+
* Time Complexity: O(log n)
|
|
561
|
+
* Space Complexity: O(n)
|
|
562
|
+
*
|
|
563
|
+
* The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
|
|
564
|
+
* @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
|
|
565
|
+
* either a `Range` object or an array of two elements representing the range boundaries.
|
|
566
|
+
* @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
|
|
567
|
+
* function that is used to process each node that is found within the specified range during the
|
|
568
|
+
* search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the
|
|
569
|
+
* data structure.
|
|
570
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch`
|
|
571
|
+
* function represents the node from which the search for nodes within the specified range will
|
|
572
|
+
* begin. It is the starting point for the range search operation.
|
|
573
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
|
|
574
|
+
* is used to specify the type of iteration to be performed during the search operation. It has a
|
|
575
|
+
* default value of `this.iterationType`, which suggests that it is likely a property of the class or
|
|
576
|
+
* object that the `rangeSearch`
|
|
577
|
+
* @returns The `rangeSearch` function is returning the result of calling the `search` method with
|
|
578
|
+
* the specified parameters.
|
|
579
|
+
*/
|
|
580
|
+
rangeSearch(range, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
581
|
+
const searchRange = range instanceof common_1.Range ? range : new common_1.Range(range[0], range[1]);
|
|
582
|
+
return this.search(searchRange, false, callback, startNode, iterationType);
|
|
583
|
+
}
|
|
550
584
|
/**
|
|
551
585
|
* Time Complexity: O(log n)
|
|
552
586
|
* Space Complexity: O(1)
|
|
@@ -19,6 +19,6 @@ __exportStar(require("./bst"), exports);
|
|
|
19
19
|
__exportStar(require("./binary-indexed-tree"), exports);
|
|
20
20
|
__exportStar(require("./segment-tree"), exports);
|
|
21
21
|
__exportStar(require("./avl-tree"), exports);
|
|
22
|
-
__exportStar(require("./
|
|
22
|
+
__exportStar(require("./red-black-tree"), exports);
|
|
23
23
|
__exportStar(require("./avl-tree-multi-map"), exports);
|
|
24
24
|
__exportStar(require("./tree-multi-map"), exports);
|
|
@@ -29,6 +29,55 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
|
|
|
29
29
|
/**
|
|
30
30
|
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
|
|
31
31
|
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
32
|
+
* @example
|
|
33
|
+
* // Find elements in a range
|
|
34
|
+
* const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
35
|
+
* console.log(bst.search(new Range(5, 10))); // [5, 10, 7]
|
|
36
|
+
* console.log(bst.search(new Range(4, 12))); // [5, 10, 12, 7]
|
|
37
|
+
* console.log(bst.search(new Range(15, 20))); // [15, 18]
|
|
38
|
+
* @example
|
|
39
|
+
* // using Red-Black Tree as a price-based index for stock data
|
|
40
|
+
* // Define the structure of individual stock records
|
|
41
|
+
* interface StockRecord {
|
|
42
|
+
* price: number; // Stock price (key for indexing)
|
|
43
|
+
* symbol: string; // Stock ticker symbol
|
|
44
|
+
* volume: number; // Trade volume
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* // Simulate stock market data as it might come from an external feed
|
|
48
|
+
* const marketStockData: StockRecord[] = [
|
|
49
|
+
* { price: 142.5, symbol: 'AAPL', volume: 1000000 },
|
|
50
|
+
* { price: 335.2, symbol: 'MSFT', volume: 800000 },
|
|
51
|
+
* { price: 3285.04, symbol: 'AMZN', volume: 500000 },
|
|
52
|
+
* { price: 267.98, symbol: 'META', volume: 750000 },
|
|
53
|
+
* { price: 234.57, symbol: 'GOOGL', volume: 900000 }
|
|
54
|
+
* ];
|
|
55
|
+
*
|
|
56
|
+
* // Extend the stock record type to include metadata for database usage
|
|
57
|
+
* type StockTableRecord = StockRecord & { lastUpdated: Date };
|
|
58
|
+
*
|
|
59
|
+
* // Create a Red-Black Tree to index stock records by price
|
|
60
|
+
* // Simulates a database index with stock price as the key for quick lookups
|
|
61
|
+
* const priceIndex = new RedBlackTree<number, StockTableRecord, StockRecord>(marketStockData, {
|
|
62
|
+
* toEntryFn: stockRecord => [
|
|
63
|
+
* stockRecord.price, // Use stock price as the key
|
|
64
|
+
* {
|
|
65
|
+
* ...stockRecord,
|
|
66
|
+
* lastUpdated: new Date() // Add a timestamp for when the record was indexed
|
|
67
|
+
* }
|
|
68
|
+
* ]
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* // Query the stock with the highest price
|
|
72
|
+
* const highestPricedStock = priceIndex.getRightMost();
|
|
73
|
+
* console.log(priceIndex.get(highestPricedStock)?.symbol); // 'AMZN' // Amazon has the highest price
|
|
74
|
+
*
|
|
75
|
+
* // Query stocks within a specific price range (200 to 400)
|
|
76
|
+
* const stocksInRange = priceIndex.rangeSearch(
|
|
77
|
+
* [200, 400], // Price range
|
|
78
|
+
* node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
|
|
79
|
+
* );
|
|
80
|
+
* console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META']
|
|
32
81
|
*/
|
|
33
82
|
export declare class RedBlackTree<K = any, V = any, R = object, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>> extends BST<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
34
83
|
/**
|
|
@@ -37,6 +37,55 @@ exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
|
37
37
|
/**
|
|
38
38
|
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
|
|
39
39
|
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
40
|
+
* @example
|
|
41
|
+
* // Find elements in a range
|
|
42
|
+
* const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
43
|
+
* console.log(bst.search(new Range(5, 10))); // [5, 10, 7]
|
|
44
|
+
* console.log(bst.search(new Range(4, 12))); // [5, 10, 12, 7]
|
|
45
|
+
* console.log(bst.search(new Range(15, 20))); // [15, 18]
|
|
46
|
+
* @example
|
|
47
|
+
* // using Red-Black Tree as a price-based index for stock data
|
|
48
|
+
* // Define the structure of individual stock records
|
|
49
|
+
* interface StockRecord {
|
|
50
|
+
* price: number; // Stock price (key for indexing)
|
|
51
|
+
* symbol: string; // Stock ticker symbol
|
|
52
|
+
* volume: number; // Trade volume
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* // Simulate stock market data as it might come from an external feed
|
|
56
|
+
* const marketStockData: StockRecord[] = [
|
|
57
|
+
* { price: 142.5, symbol: 'AAPL', volume: 1000000 },
|
|
58
|
+
* { price: 335.2, symbol: 'MSFT', volume: 800000 },
|
|
59
|
+
* { price: 3285.04, symbol: 'AMZN', volume: 500000 },
|
|
60
|
+
* { price: 267.98, symbol: 'META', volume: 750000 },
|
|
61
|
+
* { price: 234.57, symbol: 'GOOGL', volume: 900000 }
|
|
62
|
+
* ];
|
|
63
|
+
*
|
|
64
|
+
* // Extend the stock record type to include metadata for database usage
|
|
65
|
+
* type StockTableRecord = StockRecord & { lastUpdated: Date };
|
|
66
|
+
*
|
|
67
|
+
* // Create a Red-Black Tree to index stock records by price
|
|
68
|
+
* // Simulates a database index with stock price as the key for quick lookups
|
|
69
|
+
* const priceIndex = new RedBlackTree<number, StockTableRecord, StockRecord>(marketStockData, {
|
|
70
|
+
* toEntryFn: stockRecord => [
|
|
71
|
+
* stockRecord.price, // Use stock price as the key
|
|
72
|
+
* {
|
|
73
|
+
* ...stockRecord,
|
|
74
|
+
* lastUpdated: new Date() // Add a timestamp for when the record was indexed
|
|
75
|
+
* }
|
|
76
|
+
* ]
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* // Query the stock with the highest price
|
|
80
|
+
* const highestPricedStock = priceIndex.getRightMost();
|
|
81
|
+
* console.log(priceIndex.get(highestPricedStock)?.symbol); // 'AMZN' // Amazon has the highest price
|
|
82
|
+
*
|
|
83
|
+
* // Query stocks within a specific price range (200 to 400)
|
|
84
|
+
* const stocksInRange = priceIndex.rangeSearch(
|
|
85
|
+
* [200, 400], // Price range
|
|
86
|
+
* node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
|
|
87
|
+
* );
|
|
88
|
+
* console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META']
|
|
40
89
|
*/
|
|
41
90
|
class RedBlackTree extends bst_1.BST {
|
|
42
91
|
/**
|
|
@@ -164,7 +213,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
164
213
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
165
214
|
*/
|
|
166
215
|
add(keyNodeEntryOrRaw, value) {
|
|
167
|
-
const [newNode, newValue] = this.
|
|
216
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
168
217
|
if (!this.isRealNode(newNode))
|
|
169
218
|
return false;
|
|
170
219
|
const insertStatus = this._insert(newNode);
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, IterationType, RBTNColor, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
|
-
import { RedBlackTree, RedBlackTreeNode } from './
|
|
10
|
+
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
11
11
|
export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> {
|
|
12
12
|
/**
|
|
13
13
|
* The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
|
|
@@ -96,7 +96,7 @@ export declare class TreeMultiMap<K = any, V = any, R = object, NODE extends Tre
|
|
|
96
96
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
97
97
|
* @returns either a NODE object or undefined.
|
|
98
98
|
*/
|
|
99
|
-
|
|
99
|
+
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined];
|
|
100
100
|
/**
|
|
101
101
|
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
102
102
|
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TreeMultiMap = exports.TreeMultiMapNode = void 0;
|
|
4
|
-
const
|
|
5
|
-
class TreeMultiMapNode extends
|
|
4
|
+
const red_black_tree_1 = require("./red-black-tree");
|
|
5
|
+
class TreeMultiMapNode extends red_black_tree_1.RedBlackTreeNode {
|
|
6
6
|
/**
|
|
7
7
|
* The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
|
|
8
8
|
* @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
|
|
@@ -37,7 +37,7 @@ class TreeMultiMapNode extends rb_tree_1.RedBlackTreeNode {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
exports.TreeMultiMapNode = TreeMultiMapNode;
|
|
40
|
-
class TreeMultiMap extends
|
|
40
|
+
class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
41
41
|
/**
|
|
42
42
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
43
43
|
* @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
|
|
@@ -113,7 +113,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
113
113
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
114
114
|
* @returns either a NODE object or undefined.
|
|
115
115
|
*/
|
|
116
|
-
|
|
116
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
117
117
|
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
|
|
118
118
|
return [undefined, undefined];
|
|
119
119
|
if (this.isNode(keyNodeEntryOrRaw))
|
|
@@ -163,7 +163,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
163
163
|
* was successful, and false otherwise.
|
|
164
164
|
*/
|
|
165
165
|
add(keyNodeEntryOrRaw, value, count = 1) {
|
|
166
|
-
const [newNode, newValue] = this.
|
|
166
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
167
167
|
const orgCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
168
168
|
const isSuccessAdded = super.add(newNode, newValue);
|
|
169
169
|
if (isSuccessAdded) {
|
|
@@ -62,6 +62,9 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
62
62
|
*/
|
|
63
63
|
get hashFn(): (key: K) => string;
|
|
64
64
|
/**
|
|
65
|
+
* Time Complexity: O(1)
|
|
66
|
+
* Space Complexity: O(1)
|
|
67
|
+
*
|
|
65
68
|
* The function checks if a given element is an array with exactly two elements.
|
|
66
69
|
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
67
70
|
* data type.
|
|
@@ -69,16 +72,25 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
69
72
|
*/
|
|
70
73
|
isEntry(rawElement: any): rawElement is [K, V];
|
|
71
74
|
/**
|
|
75
|
+
* Time Complexity: O(1)
|
|
76
|
+
* Space Complexity: O(1)
|
|
77
|
+
*
|
|
72
78
|
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
73
79
|
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
74
80
|
*/
|
|
75
81
|
isEmpty(): boolean;
|
|
76
82
|
/**
|
|
83
|
+
* Time Complexity: O(1)
|
|
84
|
+
* Space Complexity: O(1)
|
|
85
|
+
*
|
|
77
86
|
* The clear() function resets the state of an object by clearing its internal store, object map, and
|
|
78
87
|
* size.
|
|
79
88
|
*/
|
|
80
89
|
clear(): void;
|
|
81
90
|
/**
|
|
91
|
+
* Time Complexity: O(1)
|
|
92
|
+
* Space Complexity: O(1)
|
|
93
|
+
*
|
|
82
94
|
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if
|
|
83
95
|
* the key is not already present.
|
|
84
96
|
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It
|
|
@@ -89,6 +101,9 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
89
101
|
*/
|
|
90
102
|
set(key: K, value: V): boolean;
|
|
91
103
|
/**
|
|
104
|
+
* Time Complexity: O(k)
|
|
105
|
+
* Space Complexity: O(k)
|
|
106
|
+
*
|
|
92
107
|
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
93
108
|
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
94
109
|
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
@@ -97,6 +112,9 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
97
112
|
*/
|
|
98
113
|
setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[];
|
|
99
114
|
/**
|
|
115
|
+
* Time Complexity: O(1)
|
|
116
|
+
* Space Complexity: O(1)
|
|
117
|
+
*
|
|
100
118
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
101
119
|
* a string map.
|
|
102
120
|
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
@@ -106,6 +124,9 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
106
124
|
*/
|
|
107
125
|
get(key: K): V | undefined;
|
|
108
126
|
/**
|
|
127
|
+
* Time Complexity: O(1)
|
|
128
|
+
* Space Complexity: O(1)
|
|
129
|
+
*
|
|
109
130
|
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it
|
|
110
131
|
* is an object key or not.
|
|
111
132
|
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
|
|
@@ -113,6 +134,9 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
113
134
|
*/
|
|
114
135
|
has(key: K): boolean;
|
|
115
136
|
/**
|
|
137
|
+
* Time Complexity: O(1)
|
|
138
|
+
* Space Complexity: O(1)
|
|
139
|
+
*
|
|
116
140
|
* The `delete` function removes an element from a map-like data structure based on the provided key.
|
|
117
141
|
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the
|
|
118
142
|
* data structure.
|
|
@@ -292,6 +316,9 @@ export declare class LinkedHashMap<K = any, V = any, R = [K, V]> extends Iterabl
|
|
|
292
316
|
*/
|
|
293
317
|
set(key: K, value?: V): boolean;
|
|
294
318
|
/**
|
|
319
|
+
* Time Complexity: O(k)
|
|
320
|
+
* Space Complexity: O(k)
|
|
321
|
+
*
|
|
295
322
|
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
|
|
296
323
|
* using a provided function, and sets each key-value pair in the current object, returning an array
|
|
297
324
|
* of booleans indicating the success of each set operation.
|
|
@@ -301,6 +328,9 @@ export declare class LinkedHashMap<K = any, V = any, R = [K, V]> extends Iterabl
|
|
|
301
328
|
*/
|
|
302
329
|
setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[];
|
|
303
330
|
/**
|
|
331
|
+
* Time Complexity: O(1)
|
|
332
|
+
* Space Complexity: O(1)
|
|
333
|
+
*
|
|
304
334
|
* The function checks if a given key exists in a map, using different logic depending on whether the
|
|
305
335
|
* key is a weak key or not.
|
|
306
336
|
* @param {K} key - The `key` parameter is the key that is being checked for existence in the map.
|