directed-graph-typed 1.53.7 → 1.53.9
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/base/iterable-entry-base.js +4 -4
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +36 -17
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +65 -36
- package/dist/data-structures/binary-tree/avl-tree.d.ts +12 -8
- package/dist/data-structures/binary-tree/avl-tree.js +19 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +53 -40
- package/dist/data-structures/binary-tree/binary-tree.js +76 -72
- package/dist/data-structures/binary-tree/bst.d.ts +87 -52
- package/dist/data-structures/binary-tree/bst.js +111 -63
- 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} +83 -10
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +91 -44
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +34 -18
- package/dist/data-structures/binary-tree/tree-multi-map.js +66 -40
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +31 -1
- package/dist/data-structures/hash/hash-map.js +35 -5
- 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 +46 -11
- package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +44 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +70 -26
- 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/dist/interfaces/binary-tree.d.ts +3 -4
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -5
- package/package.json +2 -2
- package/src/common/index.ts +7 -1
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +82 -55
- package/src/data-structures/binary-tree/avl-tree.ts +32 -15
- package/src/data-structures/binary-tree/binary-tree.ts +89 -84
- package/src/data-structures/binary-tree/bst.ts +149 -97
- package/src/data-structures/binary-tree/index.ts +1 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +105 -55
- package/src/data-structures/binary-tree/tree-multi-map.ts +81 -51
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +37 -7
- package/src/data-structures/heap/heap.ts +33 -10
- package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
- package/src/data-structures/linked-list/singly-linked-list.ts +77 -27
- 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/src/interfaces/binary-tree.ts +3 -13
- package/src/types/data-structures/base/base.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/bst.ts +3 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -6
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -6
|
@@ -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
|
|
@@ -128,22 +137,22 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
128
137
|
return -1;
|
|
129
138
|
return 0;
|
|
130
139
|
}
|
|
131
|
-
if (this.
|
|
132
|
-
if (this.
|
|
140
|
+
if (this._specifyComparable) {
|
|
141
|
+
if (this._specifyComparable(a) > this._specifyComparable(b))
|
|
133
142
|
return 1;
|
|
134
|
-
if (this.
|
|
143
|
+
if (this._specifyComparable(a) < this._specifyComparable(b))
|
|
135
144
|
return -1;
|
|
136
145
|
return 0;
|
|
137
146
|
}
|
|
138
147
|
if (typeof a === 'object' || typeof b === 'object') {
|
|
139
|
-
throw TypeError(`When comparing object types, a custom
|
|
148
|
+
throw TypeError(`When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.`);
|
|
140
149
|
}
|
|
141
150
|
return 0;
|
|
142
151
|
};
|
|
143
152
|
if (options) {
|
|
144
|
-
const {
|
|
145
|
-
if (typeof
|
|
146
|
-
this.
|
|
153
|
+
const { specifyComparable, isReverse } = options;
|
|
154
|
+
if (typeof specifyComparable === 'function')
|
|
155
|
+
this._specifyComparable = specifyComparable;
|
|
147
156
|
if (isReverse !== undefined)
|
|
148
157
|
this._isReverse = isReverse;
|
|
149
158
|
}
|
|
@@ -165,6 +174,21 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
165
174
|
get isReverse() {
|
|
166
175
|
return this._isReverse;
|
|
167
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* The function returns the value of the _comparator property.
|
|
179
|
+
* @returns The `_comparator` property is being returned.
|
|
180
|
+
*/
|
|
181
|
+
get comparator() {
|
|
182
|
+
return this._comparator;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* This function returns the value of the `_specifyComparable` property.
|
|
186
|
+
* @returns The method `specifyComparable()` is being returned, which is a getter method for the
|
|
187
|
+
* `_specifyComparable` property.
|
|
188
|
+
*/
|
|
189
|
+
get specifyComparable() {
|
|
190
|
+
return this._specifyComparable;
|
|
191
|
+
}
|
|
168
192
|
/**
|
|
169
193
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
170
194
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -177,29 +201,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
177
201
|
return new BSTNode(key, this._isMapMode ? undefined : value);
|
|
178
202
|
}
|
|
179
203
|
/**
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
204
|
+
* Time Complexity: O(1)
|
|
205
|
+
* Space Complexity: O(1)
|
|
206
|
+
*
|
|
207
|
+
* The `createTree` function in TypeScript overrides the default options with the provided options to
|
|
208
|
+
* create a new Binary Search Tree.
|
|
209
|
+
* @param [options] - The `options` parameter in the `createTree` method is an optional object that
|
|
210
|
+
* can contain the following properties:
|
|
211
|
+
* @returns A new instance of a Binary Search Tree (BST) is being returned with the specified options
|
|
212
|
+
* and properties inherited from the current instance.
|
|
185
213
|
*/
|
|
214
|
+
// @ts-ignore
|
|
186
215
|
createTree(options) {
|
|
187
|
-
return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
191
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
192
|
-
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
193
|
-
* element.
|
|
194
|
-
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
195
|
-
* value associated with a key in a key-value pair.
|
|
196
|
-
* @returns either a NODE object or undefined.
|
|
197
|
-
*/
|
|
198
|
-
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
199
|
-
const [node, entryValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
200
|
-
if (node === null)
|
|
201
|
-
return [undefined, undefined];
|
|
202
|
-
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
216
|
+
return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
203
217
|
}
|
|
204
218
|
/**
|
|
205
219
|
* Time Complexity: O(log n)
|
|
@@ -239,7 +253,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
239
253
|
* this._DEFAULT_COMPARATOR`.
|
|
240
254
|
*/
|
|
241
255
|
isKey(key) {
|
|
242
|
-
return (0, utils_1.isComparable)(key, this.
|
|
256
|
+
return (0, utils_1.isComparable)(key, this._specifyComparable !== undefined);
|
|
243
257
|
}
|
|
244
258
|
/**
|
|
245
259
|
* Time Complexity: O(log n)
|
|
@@ -253,7 +267,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
253
267
|
* @returns a boolean value.
|
|
254
268
|
*/
|
|
255
269
|
add(keyNodeEntryOrRaw, value) {
|
|
256
|
-
const [newNode, newValue] = this.
|
|
270
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
257
271
|
if (newNode === undefined)
|
|
258
272
|
return false;
|
|
259
273
|
if (this._root === undefined) {
|
|
@@ -547,6 +561,31 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
547
561
|
}
|
|
548
562
|
return ans;
|
|
549
563
|
}
|
|
564
|
+
/**
|
|
565
|
+
* Time Complexity: O(log n)
|
|
566
|
+
* Space Complexity: O(n)
|
|
567
|
+
*
|
|
568
|
+
* The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
|
|
569
|
+
* @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
|
|
570
|
+
* either a `Range` object or an array of two elements representing the range boundaries.
|
|
571
|
+
* @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
|
|
572
|
+
* function that is used to process each node that is found within the specified range during the
|
|
573
|
+
* search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the
|
|
574
|
+
* data structure.
|
|
575
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch`
|
|
576
|
+
* function represents the node from which the search for nodes within the specified range will
|
|
577
|
+
* begin. It is the starting point for the range search operation.
|
|
578
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
|
|
579
|
+
* is used to specify the type of iteration to be performed during the search operation. It has a
|
|
580
|
+
* default value of `this.iterationType`, which suggests that it is likely a property of the class or
|
|
581
|
+
* object that the `rangeSearch`
|
|
582
|
+
* @returns The `rangeSearch` function is returning the result of calling the `search` method with
|
|
583
|
+
* the specified parameters.
|
|
584
|
+
*/
|
|
585
|
+
rangeSearch(range, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
586
|
+
const searchRange = range instanceof common_1.Range ? range : new common_1.Range(range[0], range[1]);
|
|
587
|
+
return this.search(searchRange, false, callback, startNode, iterationType);
|
|
588
|
+
}
|
|
550
589
|
/**
|
|
551
590
|
* Time Complexity: O(log n)
|
|
552
591
|
* Space Complexity: O(1)
|
|
@@ -806,20 +845,29 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
806
845
|
}
|
|
807
846
|
return balanced;
|
|
808
847
|
}
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
848
|
+
// @ts-ignore
|
|
849
|
+
map(callback, options, thisArg) {
|
|
850
|
+
const newTree = new BST([], options);
|
|
851
|
+
let index = 0;
|
|
852
|
+
for (const [key, value] of this) {
|
|
853
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
854
|
+
}
|
|
855
|
+
return newTree;
|
|
815
856
|
}
|
|
816
857
|
/**
|
|
817
|
-
*
|
|
818
|
-
* @
|
|
819
|
-
*
|
|
858
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
859
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
860
|
+
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
861
|
+
* element.
|
|
862
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
863
|
+
* value associated with a key in a key-value pair.
|
|
864
|
+
* @returns either a NODE object or undefined.
|
|
820
865
|
*/
|
|
821
|
-
|
|
822
|
-
|
|
866
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
867
|
+
const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
868
|
+
if (node === null)
|
|
869
|
+
return [undefined, undefined];
|
|
870
|
+
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
823
871
|
}
|
|
824
872
|
/**
|
|
825
873
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
@@ -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);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BinaryTreeDeleteResult, BTNRep, CRUD,
|
|
1
|
+
import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, RBTNColor, RedBlackTreeOptions, RedBlackTreeNodeNested } from '../../types';
|
|
2
2
|
import { BST, BSTNode } from './bst';
|
|
3
3
|
import { IBinaryTree } from '../../interfaces';
|
|
4
4
|
export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
@@ -29,19 +29,68 @@ 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
|
-
export declare class RedBlackTree<K = any, V = any, R = object, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V
|
|
82
|
+
export declare class RedBlackTree<K = any, V = any, R = object, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>> extends BST<K, V, R, NODE> implements IBinaryTree<K, V, R, NODE> {
|
|
34
83
|
/**
|
|
35
84
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
36
85
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
37
86
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
38
|
-
* initialize the
|
|
87
|
+
* initialize the RedBlackTree with the provided elements.
|
|
39
88
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
40
|
-
* constructor. It is of type `
|
|
89
|
+
* constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for
|
|
41
90
|
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
42
91
|
* depend on the implementation
|
|
43
92
|
*/
|
|
44
|
-
constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?:
|
|
93
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: RedBlackTreeOptions<K, V, R>);
|
|
45
94
|
protected _root: NODE | undefined;
|
|
46
95
|
/**
|
|
47
96
|
* The function returns the root node of a tree or undefined if there is no root.
|
|
@@ -64,12 +113,15 @@ export declare class RedBlackTree<K = any, V = any, R = object, NODE extends Red
|
|
|
64
113
|
*/
|
|
65
114
|
createNode(key: K, value?: V, color?: RBTNColor): NODE;
|
|
66
115
|
/**
|
|
67
|
-
* The function
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
116
|
+
* The function `createTree` overrides the default implementation to create a Red-Black Tree with
|
|
117
|
+
* specified options in TypeScript.
|
|
118
|
+
* @param [options] - The `options` parameter in the `createTree` method is of type `RedBlackTreeOptions<K,
|
|
119
|
+
* V, R>`, which is a generic type with three type parameters `K`, `V`, and `R`. This parameter
|
|
120
|
+
* allows you to pass additional configuration options when creating a new Red-
|
|
121
|
+
* @returns A new instance of a RedBlackTree with the specified options and properties from the
|
|
122
|
+
* current object is being returned.
|
|
71
123
|
*/
|
|
72
|
-
createTree(options?:
|
|
124
|
+
createTree(options?: RedBlackTreeOptions<K, V, R>): RedBlackTree<K, V, R, RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>>;
|
|
73
125
|
/**
|
|
74
126
|
* Time Complexity: O(1)
|
|
75
127
|
* Space Complexity: O(1)
|
|
@@ -120,6 +172,27 @@ export declare class RedBlackTree<K = any, V = any, R = object, NODE extends Red
|
|
|
120
172
|
* balancing is needed.
|
|
121
173
|
*/
|
|
122
174
|
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
|
|
175
|
+
/**
|
|
176
|
+
* Time Complexity: O(n)
|
|
177
|
+
* Space Complexity: O(n)
|
|
178
|
+
*
|
|
179
|
+
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
|
|
180
|
+
* applying a callback to each entry in the original tree.
|
|
181
|
+
* @param callback - A function that will be called for each entry in the tree, with parameters
|
|
182
|
+
* representing the key, value, index, and the tree itself. It should return an entry for the new
|
|
183
|
+
* tree.
|
|
184
|
+
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
|
|
185
|
+
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
|
|
186
|
+
* Tree that will be created during the mapping process. These options could include things like
|
|
187
|
+
* custom comparators
|
|
188
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
189
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
190
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
191
|
+
* or
|
|
192
|
+
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
193
|
+
* provided callback function.
|
|
194
|
+
*/
|
|
195
|
+
map<MK, MV, MR>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: RedBlackTreeOptions<MK, MV, MR>, thisArg?: any): RedBlackTree<MK, MV, MR, RedBlackTreeNode<MK, MV, RedBlackTreeNodeNested<MK, MV>>>;
|
|
123
196
|
/**
|
|
124
197
|
* Time Complexity: O(1)
|
|
125
198
|
* Space Complexity: O(1)
|
|
@@ -37,15 +37,64 @@ 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
|
/**
|
|
43
92
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
44
93
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
45
94
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
46
|
-
* initialize the
|
|
95
|
+
* initialize the RedBlackTree with the provided elements.
|
|
47
96
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
48
|
-
* constructor. It is of type `
|
|
97
|
+
* constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for
|
|
49
98
|
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
50
99
|
* depend on the implementation
|
|
51
100
|
*/
|
|
@@ -81,13 +130,17 @@ class RedBlackTree extends bst_1.BST {
|
|
|
81
130
|
return new RedBlackTreeNode(key, this._isMapMode ? undefined : value, color);
|
|
82
131
|
}
|
|
83
132
|
/**
|
|
84
|
-
* The function
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
133
|
+
* The function `createTree` overrides the default implementation to create a Red-Black Tree with
|
|
134
|
+
* specified options in TypeScript.
|
|
135
|
+
* @param [options] - The `options` parameter in the `createTree` method is of type `RedBlackTreeOptions<K,
|
|
136
|
+
* V, R>`, which is a generic type with three type parameters `K`, `V`, and `R`. This parameter
|
|
137
|
+
* allows you to pass additional configuration options when creating a new Red-
|
|
138
|
+
* @returns A new instance of a RedBlackTree with the specified options and properties from the
|
|
139
|
+
* current object is being returned.
|
|
88
140
|
*/
|
|
141
|
+
// @ts-ignore
|
|
89
142
|
createTree(options) {
|
|
90
|
-
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
143
|
+
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn }, options));
|
|
91
144
|
}
|
|
92
145
|
/**
|
|
93
146
|
* Time Complexity: O(1)
|
|
@@ -102,41 +155,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
102
155
|
isNode(keyNodeEntryOrRaw) {
|
|
103
156
|
return keyNodeEntryOrRaw instanceof RedBlackTreeNode;
|
|
104
157
|
}
|
|
105
|
-
// /**
|
|
106
|
-
// * Time Complexity: O(1)
|
|
107
|
-
// * Space Complexity: O(1)
|
|
108
|
-
// */
|
|
109
|
-
//
|
|
110
|
-
// /**
|
|
111
|
-
// * Time Complexity: O(1)
|
|
112
|
-
// * Space Complexity: O(1)
|
|
113
|
-
// *
|
|
114
|
-
// * The function `keyValueNodeEntryRawToNodeAndValue` takes a key, value, or entry and returns a node if it is
|
|
115
|
-
// * valid, otherwise it returns undefined.
|
|
116
|
-
// * @param {BTNRep<K, V, NODE>} keyNodeEntryOrRaw - The key, value, or entry to convert.
|
|
117
|
-
// * @param {V} [value] - The value associated with the key (if `keyNodeEntryOrRaw` is a key).
|
|
118
|
-
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
119
|
-
// */
|
|
120
|
-
// override keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): NODE | undefined {
|
|
121
|
-
//
|
|
122
|
-
// if (keyNodeEntryOrRaw === null || keyNodeEntryOrRaw === undefined) return;
|
|
123
|
-
// if (this.isNode(keyNodeEntryOrRaw)) return keyNodeEntryOrRaw;
|
|
124
|
-
//
|
|
125
|
-
// if (this._toEntryFn) {
|
|
126
|
-
// const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
|
|
127
|
-
// if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'RED');
|
|
128
|
-
// }
|
|
129
|
-
//
|
|
130
|
-
// if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
131
|
-
// const [key, value] = keyNodeEntryOrRaw;
|
|
132
|
-
// if (key === undefined || key === null) return;
|
|
133
|
-
// else return this.createNode(key, value, 'RED');
|
|
134
|
-
// }
|
|
135
|
-
//
|
|
136
|
-
// if (this.isKey(keyNodeEntryOrRaw)) return this.createNode(keyNodeEntryOrRaw, value, 'RED');
|
|
137
|
-
//
|
|
138
|
-
// return ;
|
|
139
|
-
// }
|
|
140
158
|
/**
|
|
141
159
|
* Time Complexity: O(1)
|
|
142
160
|
* Space Complexity: O(1)
|
|
@@ -164,7 +182,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
164
182
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
165
183
|
*/
|
|
166
184
|
add(keyNodeEntryOrRaw, value) {
|
|
167
|
-
const [newNode, newValue] = this.
|
|
185
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
168
186
|
if (!this.isRealNode(newNode))
|
|
169
187
|
return false;
|
|
170
188
|
const insertStatus = this._insert(newNode);
|
|
@@ -259,6 +277,35 @@ class RedBlackTree extends bst_1.BST {
|
|
|
259
277
|
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
260
278
|
return results;
|
|
261
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* Time Complexity: O(n)
|
|
282
|
+
* Space Complexity: O(n)
|
|
283
|
+
*
|
|
284
|
+
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
|
|
285
|
+
* applying a callback to each entry in the original tree.
|
|
286
|
+
* @param callback - A function that will be called for each entry in the tree, with parameters
|
|
287
|
+
* representing the key, value, index, and the tree itself. It should return an entry for the new
|
|
288
|
+
* tree.
|
|
289
|
+
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
|
|
290
|
+
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
|
|
291
|
+
* Tree that will be created during the mapping process. These options could include things like
|
|
292
|
+
* custom comparators
|
|
293
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
294
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
295
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
296
|
+
* or
|
|
297
|
+
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
298
|
+
* provided callback function.
|
|
299
|
+
*/
|
|
300
|
+
// @ts-ignore
|
|
301
|
+
map(callback, options, thisArg) {
|
|
302
|
+
const newTree = new RedBlackTree([], options);
|
|
303
|
+
let index = 0;
|
|
304
|
+
for (const [key, value] of this) {
|
|
305
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
306
|
+
}
|
|
307
|
+
return newTree;
|
|
308
|
+
}
|
|
262
309
|
/**
|
|
263
310
|
* Time Complexity: O(1)
|
|
264
311
|
* Space Complexity: O(1)
|
|
@@ -323,7 +370,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
323
370
|
if (!parent) {
|
|
324
371
|
this._setRoot(node);
|
|
325
372
|
}
|
|
326
|
-
else if (node.key
|
|
373
|
+
else if (this._compare(node.key, parent.key) < 0) {
|
|
327
374
|
parent.left = node;
|
|
328
375
|
}
|
|
329
376
|
else {
|