priority-queue-typed 1.54.3 → 2.0.0
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/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/data-structures/base/iterable-element-base.js +14 -11
- package/dist/data-structures/base/linear-base.d.ts +277 -0
- package/dist/data-structures/base/linear-base.js +552 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
- package/dist/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/data-structures/binary-tree/binary-tree.js +5 -5
- package/dist/data-structures/binary-tree/bst.js +11 -11
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/data-structures/binary-tree/tree-multi-map.js +210 -40
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/heap/heap.d.ts +3 -11
- package/dist/data-structures/heap/heap.js +0 -10
- package/dist/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +79 -75
- package/dist/data-structures/linked-list/singly-linked-list.js +217 -169
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/data-structures/queue/deque.d.ts +130 -91
- package/dist/data-structures/queue/deque.js +269 -169
- package/dist/data-structures/queue/queue.d.ts +84 -40
- package/dist/data-structures/queue/queue.js +134 -50
- package/dist/data-structures/stack/stack.d.ts +3 -11
- package/dist/data-structures/stack/stack.js +0 -10
- package/dist/data-structures/trie/trie.d.ts +4 -3
- package/dist/data-structures/trie/trie.js +3 -0
- package/dist/types/data-structures/base/base.d.ts +9 -4
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/types/data-structures/queue/queue.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
- package/src/data-structures/binary-tree/avl-tree.ts +64 -0
- package/src/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/data-structures/binary-tree/bst.ts +9 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +149 -63
- package/src/data-structures/stack/stack.ts +3 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
|
@@ -72,37 +72,40 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
72
72
|
* provided options.
|
|
73
73
|
*/
|
|
74
74
|
createTree(options) {
|
|
75
|
-
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
75
|
+
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse, isMapMode: this._isMapMode }, options));
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
78
78
|
* Time Complexity: O(1)
|
|
79
79
|
* Space Complexity: O(1)
|
|
80
80
|
*
|
|
81
|
-
* The
|
|
82
|
-
* specified key and
|
|
83
|
-
* @param {K} key - The `key` parameter
|
|
84
|
-
*
|
|
85
|
-
* @
|
|
86
|
-
*
|
|
81
|
+
* The `createNode` function in TypeScript overrides the default implementation to create a new
|
|
82
|
+
* AVLTreeMultiMapNode with a specified key and value array.
|
|
83
|
+
* @param {K} key - The `key` parameter represents the key of the node being created in the
|
|
84
|
+
* AVLTreeMultiMap.
|
|
85
|
+
* @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
|
|
86
|
+
* values associated with a specific key in the AVLTreeMultiMapNode. If no value is provided when
|
|
87
|
+
* calling the method, an empty array `[]` is used as the default value.
|
|
88
|
+
* @returns An AVLTreeMultiMapNode object is being returned, with the specified key and value. If the
|
|
89
|
+
* AVLTreeMultiMap is in map mode, an empty array is used as the value, otherwise the provided value
|
|
90
|
+
* array is used.
|
|
87
91
|
*/
|
|
88
|
-
createNode(key) {
|
|
89
|
-
return new AVLTreeMultiMapNode(key, []);
|
|
92
|
+
createNode(key, value = []) {
|
|
93
|
+
return new AVLTreeMultiMapNode(key, this._isMapMode ? [] : value);
|
|
90
94
|
}
|
|
91
95
|
/**
|
|
92
96
|
* Time Complexity: O(log n)
|
|
93
97
|
* Space Complexity: O(log n)
|
|
94
98
|
*
|
|
95
|
-
* The function `add` in TypeScript overrides the superclass method to add key-value pairs
|
|
96
|
-
*
|
|
97
|
-
* @param
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
* `values`
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
* addition operation was successful or not.
|
|
99
|
+
* The function `add` in this TypeScript code overrides the superclass method to add key-value pairs
|
|
100
|
+
* to an AVLTreeMultiMap, handling different input types and scenarios.
|
|
101
|
+
* @param [key] - The `key` parameter in the `override add` method represents the key of the entry to
|
|
102
|
+
* be added to the AVLTreeMultiMap. It can be of type `K`, which is the key type of the map. The key
|
|
103
|
+
* can be a single key value, a node of the AVLTree
|
|
104
|
+
* @param {V[]} [values] - The `values` parameter in the `add` method represents an array of values
|
|
105
|
+
* that you want to add to the AVLTreeMultiMap. It can contain one or more values associated with a
|
|
106
|
+
* specific key.
|
|
107
|
+
* @returns The `add` method is returning a boolean value, which indicates whether the operation was
|
|
108
|
+
* successful or not.
|
|
106
109
|
*/
|
|
107
110
|
add(keyNodeOrEntry, value) {
|
|
108
111
|
if (this.isRealNode(keyNodeOrEntry))
|
|
@@ -110,30 +113,40 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
110
113
|
const _commonAdd = (key, values) => {
|
|
111
114
|
if (key === undefined || key === null)
|
|
112
115
|
return false;
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
existingValues.push(value);
|
|
117
|
-
return true;
|
|
118
|
-
}
|
|
119
|
-
const existingNode = this.getNode(key);
|
|
120
|
-
if (this.isRealNode(existingNode)) {
|
|
121
|
-
if (existingValues === undefined) {
|
|
122
|
-
super.add(key, values);
|
|
123
|
-
return true;
|
|
124
|
-
}
|
|
125
|
-
if (values !== undefined) {
|
|
116
|
+
const _addToValues = () => {
|
|
117
|
+
const existingValues = this.get(key);
|
|
118
|
+
if (existingValues !== undefined && values !== undefined) {
|
|
126
119
|
for (const value of values)
|
|
127
120
|
existingValues.push(value);
|
|
128
121
|
return true;
|
|
129
122
|
}
|
|
123
|
+
return false;
|
|
124
|
+
};
|
|
125
|
+
const _addByNode = () => {
|
|
126
|
+
const existingNode = this.getNode(key);
|
|
127
|
+
if (this.isRealNode(existingNode)) {
|
|
128
|
+
const existingValues = this.get(existingNode);
|
|
129
|
+
if (existingValues === undefined) {
|
|
130
|
+
super.add(key, values);
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
if (values !== undefined) {
|
|
134
|
+
for (const value of values)
|
|
135
|
+
existingValues.push(value);
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
130
142
|
else {
|
|
131
|
-
return
|
|
143
|
+
return super.add(key, values);
|
|
132
144
|
}
|
|
145
|
+
};
|
|
146
|
+
if (this._isMapMode) {
|
|
147
|
+
return _addByNode() || _addToValues();
|
|
133
148
|
}
|
|
134
|
-
|
|
135
|
-
return super.add(key, values);
|
|
136
|
-
}
|
|
149
|
+
return _addToValues() || _addByNode();
|
|
137
150
|
};
|
|
138
151
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
139
152
|
const [key, values] = keyNodeOrEntry;
|
|
@@ -35,6 +35,70 @@ export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
35
35
|
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
|
36
36
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
37
37
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
38
|
+
* @example
|
|
39
|
+
* // Find elements in a range
|
|
40
|
+
* // In interval queries, AVL trees, with their strictly balanced structure and lower height, offer better query efficiency, making them ideal for frequent and high-performance interval queries. In contrast, Red-Black trees, with lower update costs, are more suitable for scenarios involving frequent insertions and deletions where the requirements for interval queries are less demanding.
|
|
41
|
+
* type Datum = { timestamp: Date; temperature: number };
|
|
42
|
+
* // Fixed dataset of CPU temperature readings
|
|
43
|
+
* const cpuData: Datum[] = [
|
|
44
|
+
* { timestamp: new Date('2024-12-02T00:00:00'), temperature: 55.1 },
|
|
45
|
+
* { timestamp: new Date('2024-12-02T00:01:00'), temperature: 56.3 },
|
|
46
|
+
* { timestamp: new Date('2024-12-02T00:02:00'), temperature: 54.8 },
|
|
47
|
+
* { timestamp: new Date('2024-12-02T00:03:00'), temperature: 57.2 },
|
|
48
|
+
* { timestamp: new Date('2024-12-02T00:04:00'), temperature: 58.0 },
|
|
49
|
+
* { timestamp: new Date('2024-12-02T00:05:00'), temperature: 59.4 },
|
|
50
|
+
* { timestamp: new Date('2024-12-02T00:06:00'), temperature: 60.1 },
|
|
51
|
+
* { timestamp: new Date('2024-12-02T00:07:00'), temperature: 61.3 },
|
|
52
|
+
* { timestamp: new Date('2024-12-02T00:08:00'), temperature: 62.0 },
|
|
53
|
+
* { timestamp: new Date('2024-12-02T00:09:00'), temperature: 63.5 },
|
|
54
|
+
* { timestamp: new Date('2024-12-02T00:10:00'), temperature: 64.0 },
|
|
55
|
+
* { timestamp: new Date('2024-12-02T00:11:00'), temperature: 62.8 },
|
|
56
|
+
* { timestamp: new Date('2024-12-02T00:12:00'), temperature: 61.5 },
|
|
57
|
+
* { timestamp: new Date('2024-12-02T00:13:00'), temperature: 60.2 },
|
|
58
|
+
* { timestamp: new Date('2024-12-02T00:14:00'), temperature: 59.8 },
|
|
59
|
+
* { timestamp: new Date('2024-12-02T00:15:00'), temperature: 58.6 },
|
|
60
|
+
* { timestamp: new Date('2024-12-02T00:16:00'), temperature: 57.4 },
|
|
61
|
+
* { timestamp: new Date('2024-12-02T00:17:00'), temperature: 56.2 },
|
|
62
|
+
* { timestamp: new Date('2024-12-02T00:18:00'), temperature: 55.7 },
|
|
63
|
+
* { timestamp: new Date('2024-12-02T00:19:00'), temperature: 54.5 },
|
|
64
|
+
* { timestamp: new Date('2024-12-02T00:20:00'), temperature: 53.2 },
|
|
65
|
+
* { timestamp: new Date('2024-12-02T00:21:00'), temperature: 52.8 },
|
|
66
|
+
* { timestamp: new Date('2024-12-02T00:22:00'), temperature: 51.9 },
|
|
67
|
+
* { timestamp: new Date('2024-12-02T00:23:00'), temperature: 50.5 },
|
|
68
|
+
* { timestamp: new Date('2024-12-02T00:24:00'), temperature: 49.8 },
|
|
69
|
+
* { timestamp: new Date('2024-12-02T00:25:00'), temperature: 48.7 },
|
|
70
|
+
* { timestamp: new Date('2024-12-02T00:26:00'), temperature: 47.5 },
|
|
71
|
+
* { timestamp: new Date('2024-12-02T00:27:00'), temperature: 46.3 },
|
|
72
|
+
* { timestamp: new Date('2024-12-02T00:28:00'), temperature: 45.9 },
|
|
73
|
+
* { timestamp: new Date('2024-12-02T00:29:00'), temperature: 45.0 }
|
|
74
|
+
* ];
|
|
75
|
+
*
|
|
76
|
+
* // Create an AVL tree to store CPU temperature data
|
|
77
|
+
* const cpuTemperatureTree = new AVLTree<Date, number, Datum>(cpuData, {
|
|
78
|
+
* toEntryFn: ({ timestamp, temperature }) => [timestamp, temperature]
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* // Query a specific time range (e.g., from 00:05 to 00:15)
|
|
82
|
+
* const rangeStart = new Date('2024-12-02T00:05:00');
|
|
83
|
+
* const rangeEnd = new Date('2024-12-02T00:15:00');
|
|
84
|
+
* const rangeResults = cpuTemperatureTree.rangeSearch([rangeStart, rangeEnd], node => ({
|
|
85
|
+
* minute: node ? node.key.getMinutes() : 0,
|
|
86
|
+
* temperature: cpuTemperatureTree.get(node ? node.key : undefined)
|
|
87
|
+
* }));
|
|
88
|
+
*
|
|
89
|
+
* console.log(rangeResults); // [
|
|
90
|
+
* // { minute: 5, temperature: 59.4 },
|
|
91
|
+
* // { minute: 6, temperature: 60.1 },
|
|
92
|
+
* // { minute: 7, temperature: 61.3 },
|
|
93
|
+
* // { minute: 8, temperature: 62 },
|
|
94
|
+
* // { minute: 9, temperature: 63.5 },
|
|
95
|
+
* // { minute: 10, temperature: 64 },
|
|
96
|
+
* // { minute: 11, temperature: 62.8 },
|
|
97
|
+
* // { minute: 12, temperature: 61.5 },
|
|
98
|
+
* // { minute: 13, temperature: 60.2 },
|
|
99
|
+
* // { minute: 14, temperature: 59.8 },
|
|
100
|
+
* // { minute: 15, temperature: 58.6 }
|
|
101
|
+
* // ]
|
|
38
102
|
*/
|
|
39
103
|
export declare class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends BST<K, V, R, MK, MV, MR> implements IBinaryTree<K, V, R, MK, MV, MR> {
|
|
40
104
|
/**
|
|
@@ -53,6 +53,70 @@ exports.AVLTreeNode = AVLTreeNode;
|
|
|
53
53
|
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
|
54
54
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
55
55
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
56
|
+
* @example
|
|
57
|
+
* // Find elements in a range
|
|
58
|
+
* // In interval queries, AVL trees, with their strictly balanced structure and lower height, offer better query efficiency, making them ideal for frequent and high-performance interval queries. In contrast, Red-Black trees, with lower update costs, are more suitable for scenarios involving frequent insertions and deletions where the requirements for interval queries are less demanding.
|
|
59
|
+
* type Datum = { timestamp: Date; temperature: number };
|
|
60
|
+
* // Fixed dataset of CPU temperature readings
|
|
61
|
+
* const cpuData: Datum[] = [
|
|
62
|
+
* { timestamp: new Date('2024-12-02T00:00:00'), temperature: 55.1 },
|
|
63
|
+
* { timestamp: new Date('2024-12-02T00:01:00'), temperature: 56.3 },
|
|
64
|
+
* { timestamp: new Date('2024-12-02T00:02:00'), temperature: 54.8 },
|
|
65
|
+
* { timestamp: new Date('2024-12-02T00:03:00'), temperature: 57.2 },
|
|
66
|
+
* { timestamp: new Date('2024-12-02T00:04:00'), temperature: 58.0 },
|
|
67
|
+
* { timestamp: new Date('2024-12-02T00:05:00'), temperature: 59.4 },
|
|
68
|
+
* { timestamp: new Date('2024-12-02T00:06:00'), temperature: 60.1 },
|
|
69
|
+
* { timestamp: new Date('2024-12-02T00:07:00'), temperature: 61.3 },
|
|
70
|
+
* { timestamp: new Date('2024-12-02T00:08:00'), temperature: 62.0 },
|
|
71
|
+
* { timestamp: new Date('2024-12-02T00:09:00'), temperature: 63.5 },
|
|
72
|
+
* { timestamp: new Date('2024-12-02T00:10:00'), temperature: 64.0 },
|
|
73
|
+
* { timestamp: new Date('2024-12-02T00:11:00'), temperature: 62.8 },
|
|
74
|
+
* { timestamp: new Date('2024-12-02T00:12:00'), temperature: 61.5 },
|
|
75
|
+
* { timestamp: new Date('2024-12-02T00:13:00'), temperature: 60.2 },
|
|
76
|
+
* { timestamp: new Date('2024-12-02T00:14:00'), temperature: 59.8 },
|
|
77
|
+
* { timestamp: new Date('2024-12-02T00:15:00'), temperature: 58.6 },
|
|
78
|
+
* { timestamp: new Date('2024-12-02T00:16:00'), temperature: 57.4 },
|
|
79
|
+
* { timestamp: new Date('2024-12-02T00:17:00'), temperature: 56.2 },
|
|
80
|
+
* { timestamp: new Date('2024-12-02T00:18:00'), temperature: 55.7 },
|
|
81
|
+
* { timestamp: new Date('2024-12-02T00:19:00'), temperature: 54.5 },
|
|
82
|
+
* { timestamp: new Date('2024-12-02T00:20:00'), temperature: 53.2 },
|
|
83
|
+
* { timestamp: new Date('2024-12-02T00:21:00'), temperature: 52.8 },
|
|
84
|
+
* { timestamp: new Date('2024-12-02T00:22:00'), temperature: 51.9 },
|
|
85
|
+
* { timestamp: new Date('2024-12-02T00:23:00'), temperature: 50.5 },
|
|
86
|
+
* { timestamp: new Date('2024-12-02T00:24:00'), temperature: 49.8 },
|
|
87
|
+
* { timestamp: new Date('2024-12-02T00:25:00'), temperature: 48.7 },
|
|
88
|
+
* { timestamp: new Date('2024-12-02T00:26:00'), temperature: 47.5 },
|
|
89
|
+
* { timestamp: new Date('2024-12-02T00:27:00'), temperature: 46.3 },
|
|
90
|
+
* { timestamp: new Date('2024-12-02T00:28:00'), temperature: 45.9 },
|
|
91
|
+
* { timestamp: new Date('2024-12-02T00:29:00'), temperature: 45.0 }
|
|
92
|
+
* ];
|
|
93
|
+
*
|
|
94
|
+
* // Create an AVL tree to store CPU temperature data
|
|
95
|
+
* const cpuTemperatureTree = new AVLTree<Date, number, Datum>(cpuData, {
|
|
96
|
+
* toEntryFn: ({ timestamp, temperature }) => [timestamp, temperature]
|
|
97
|
+
* });
|
|
98
|
+
*
|
|
99
|
+
* // Query a specific time range (e.g., from 00:05 to 00:15)
|
|
100
|
+
* const rangeStart = new Date('2024-12-02T00:05:00');
|
|
101
|
+
* const rangeEnd = new Date('2024-12-02T00:15:00');
|
|
102
|
+
* const rangeResults = cpuTemperatureTree.rangeSearch([rangeStart, rangeEnd], node => ({
|
|
103
|
+
* minute: node ? node.key.getMinutes() : 0,
|
|
104
|
+
* temperature: cpuTemperatureTree.get(node ? node.key : undefined)
|
|
105
|
+
* }));
|
|
106
|
+
*
|
|
107
|
+
* console.log(rangeResults); // [
|
|
108
|
+
* // { minute: 5, temperature: 59.4 },
|
|
109
|
+
* // { minute: 6, temperature: 60.1 },
|
|
110
|
+
* // { minute: 7, temperature: 61.3 },
|
|
111
|
+
* // { minute: 8, temperature: 62 },
|
|
112
|
+
* // { minute: 9, temperature: 63.5 },
|
|
113
|
+
* // { minute: 10, temperature: 64 },
|
|
114
|
+
* // { minute: 11, temperature: 62.8 },
|
|
115
|
+
* // { minute: 12, temperature: 61.5 },
|
|
116
|
+
* // { minute: 13, temperature: 60.2 },
|
|
117
|
+
* // { minute: 14, temperature: 59.8 },
|
|
118
|
+
* // { minute: 15, temperature: 58.6 }
|
|
119
|
+
* // ]
|
|
56
120
|
*/
|
|
57
121
|
class AVLTree extends bst_1.BST {
|
|
58
122
|
/**
|
|
@@ -451,7 +451,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
451
451
|
}
|
|
452
452
|
const queue = new queue_1.Queue([this._root]);
|
|
453
453
|
let potentialParent; // Record the parent node of the potential insertion location
|
|
454
|
-
while (queue.
|
|
454
|
+
while (queue.length > 0) {
|
|
455
455
|
const cur = queue.shift();
|
|
456
456
|
if (!cur)
|
|
457
457
|
continue;
|
|
@@ -1275,7 +1275,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1275
1275
|
startNode
|
|
1276
1276
|
]);
|
|
1277
1277
|
const dfs = (level) => {
|
|
1278
|
-
if (queue.
|
|
1278
|
+
if (queue.length === 0)
|
|
1279
1279
|
return;
|
|
1280
1280
|
const current = queue.shift();
|
|
1281
1281
|
ans.push(callback(current));
|
|
@@ -1297,8 +1297,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1297
1297
|
}
|
|
1298
1298
|
else {
|
|
1299
1299
|
const queue = new queue_1.Queue([startNode]);
|
|
1300
|
-
while (queue.
|
|
1301
|
-
const levelSize = queue.
|
|
1300
|
+
while (queue.length > 0) {
|
|
1301
|
+
const levelSize = queue.length;
|
|
1302
1302
|
for (let i = 0; i < levelSize; i++) {
|
|
1303
1303
|
const current = queue.shift();
|
|
1304
1304
|
ans.push(callback(current));
|
|
@@ -1358,7 +1358,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1358
1358
|
}
|
|
1359
1359
|
else {
|
|
1360
1360
|
const queue = new queue_1.Queue([startNode]);
|
|
1361
|
-
while (queue.
|
|
1361
|
+
while (queue.length > 0) {
|
|
1362
1362
|
const cur = queue.shift();
|
|
1363
1363
|
if (this.isRealNode(cur)) {
|
|
1364
1364
|
if (this.isLeaf(cur)) {
|
|
@@ -359,23 +359,22 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
359
359
|
return 0;
|
|
360
360
|
});
|
|
361
361
|
const _dfs = (arr) => {
|
|
362
|
-
var _a;
|
|
363
362
|
if (arr.length === 0)
|
|
364
363
|
return;
|
|
365
364
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
366
|
-
|
|
365
|
+
const { key, value } = arr[mid];
|
|
367
366
|
const { orgIndex } = arr[mid];
|
|
368
367
|
if (this.isRaw(key)) {
|
|
369
368
|
const entry = this._toEntryFn(key);
|
|
370
|
-
|
|
371
|
-
|
|
369
|
+
inserted[orgIndex] = this.add(entry);
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
inserted[orgIndex] = this.add(key, value);
|
|
372
373
|
}
|
|
373
|
-
inserted[orgIndex] = this.add(key, value);
|
|
374
374
|
_dfs(arr.slice(0, mid));
|
|
375
375
|
_dfs(arr.slice(mid + 1));
|
|
376
376
|
};
|
|
377
377
|
const _iterate = () => {
|
|
378
|
-
var _a;
|
|
379
378
|
const n = sorted.length;
|
|
380
379
|
const stack = [[0, n - 1]];
|
|
381
380
|
while (stack.length > 0) {
|
|
@@ -384,14 +383,15 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
384
383
|
const [l, r] = popped;
|
|
385
384
|
if (l <= r) {
|
|
386
385
|
const m = l + Math.floor((r - l) / 2);
|
|
387
|
-
|
|
386
|
+
const { key, value } = sorted[m];
|
|
388
387
|
const { orgIndex } = sorted[m];
|
|
389
388
|
if (this.isRaw(key)) {
|
|
390
389
|
const entry = this._toEntryFn(key);
|
|
391
|
-
|
|
392
|
-
|
|
390
|
+
inserted[orgIndex] = this.add(entry);
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
inserted[orgIndex] = this.add(key, value);
|
|
393
394
|
}
|
|
394
|
-
inserted[orgIndex] = this.add(key, value);
|
|
395
395
|
stack.push([m + 1, r]);
|
|
396
396
|
stack.push([l, m - 1]);
|
|
397
397
|
}
|
|
@@ -659,7 +659,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
659
659
|
}
|
|
660
660
|
else {
|
|
661
661
|
const queue = new queue_1.Queue([this._root]);
|
|
662
|
-
while (queue.
|
|
662
|
+
while (queue.length > 0) {
|
|
663
663
|
const cur = queue.shift();
|
|
664
664
|
if (this.isRealNode(cur)) {
|
|
665
665
|
const compared = this._compare(cur.key, targetKey);
|
|
@@ -19,7 +19,7 @@ export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode
|
|
|
19
19
|
* @param {V[]} value - The `value` parameter in the constructor represents an array of values of
|
|
20
20
|
* type `V`.
|
|
21
21
|
*/
|
|
22
|
-
constructor(key: K, value
|
|
22
|
+
constructor(key: K, value?: V[]);
|
|
23
23
|
_left?: TreeMultiMapNode<K, V> | null | undefined;
|
|
24
24
|
get left(): TreeMultiMapNode<K, V> | null | undefined;
|
|
25
25
|
set left(v: TreeMultiMapNode<K, V> | null | undefined);
|
|
@@ -30,11 +30,169 @@ export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode
|
|
|
30
30
|
/**
|
|
31
31
|
*
|
|
32
32
|
* @example
|
|
33
|
-
* //
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
33
|
+
* // players ranked by score with their equipment
|
|
34
|
+
* type Equipment = {
|
|
35
|
+
* name: string; // Equipment name
|
|
36
|
+
* quality: 'legendary' | 'epic' | 'rare' | 'common';
|
|
37
|
+
* level: number;
|
|
38
|
+
* };
|
|
39
|
+
*
|
|
40
|
+
* type Player = {
|
|
41
|
+
* name: string;
|
|
42
|
+
* score: number;
|
|
43
|
+
* equipments: Equipment[];
|
|
44
|
+
* };
|
|
45
|
+
*
|
|
46
|
+
* // Mock player data with their scores and equipment
|
|
47
|
+
* const players: Player[] = [
|
|
48
|
+
* {
|
|
49
|
+
* name: 'DragonSlayer',
|
|
50
|
+
* score: 8750,
|
|
51
|
+
* equipments: [
|
|
52
|
+
* { name: 'AWM', quality: 'legendary', level: 85 },
|
|
53
|
+
* { name: 'Level 3 Helmet', quality: 'epic', level: 80 },
|
|
54
|
+
* { name: 'Extended Quickdraw Mag', quality: 'rare', level: 75 },
|
|
55
|
+
* { name: 'Compensator', quality: 'epic', level: 78 },
|
|
56
|
+
* { name: 'Vertical Grip', quality: 'rare', level: 72 }
|
|
57
|
+
* ]
|
|
58
|
+
* },
|
|
59
|
+
* {
|
|
60
|
+
* name: 'ShadowNinja',
|
|
61
|
+
* score: 7200,
|
|
62
|
+
* equipments: [
|
|
63
|
+
* { name: 'M416', quality: 'epic', level: 75 },
|
|
64
|
+
* { name: 'Ghillie Suit', quality: 'rare', level: 70 },
|
|
65
|
+
* { name: 'Red Dot Sight', quality: 'common', level: 65 },
|
|
66
|
+
* { name: 'Extended QuickDraw Mag', quality: 'rare', level: 68 }
|
|
67
|
+
* ]
|
|
68
|
+
* },
|
|
69
|
+
* {
|
|
70
|
+
* name: 'RuneMaster',
|
|
71
|
+
* score: 9100,
|
|
72
|
+
* equipments: [
|
|
73
|
+
* { name: 'KAR98K', quality: 'legendary', level: 90 },
|
|
74
|
+
* { name: 'Level 3 Vest', quality: 'legendary', level: 85 },
|
|
75
|
+
* { name: 'Holographic Sight', quality: 'epic', level: 82 },
|
|
76
|
+
* { name: 'Suppressor', quality: 'legendary', level: 88 },
|
|
77
|
+
* { name: 'Level 3 Backpack', quality: 'epic', level: 80 }
|
|
78
|
+
* ]
|
|
79
|
+
* },
|
|
80
|
+
* {
|
|
81
|
+
* name: 'BattleKing',
|
|
82
|
+
* score: 8500,
|
|
83
|
+
* equipments: [
|
|
84
|
+
* { name: 'AUG', quality: 'epic', level: 82 },
|
|
85
|
+
* { name: 'Red Dot Sight', quality: 'rare', level: 75 },
|
|
86
|
+
* { name: 'Extended Mag', quality: 'common', level: 70 },
|
|
87
|
+
* { name: 'Tactical Stock', quality: 'rare', level: 76 }
|
|
88
|
+
* ]
|
|
89
|
+
* },
|
|
90
|
+
* {
|
|
91
|
+
* name: 'SniperElite',
|
|
92
|
+
* score: 7800,
|
|
93
|
+
* equipments: [
|
|
94
|
+
* { name: 'M24', quality: 'legendary', level: 88 },
|
|
95
|
+
* { name: 'Compensator', quality: 'epic', level: 80 },
|
|
96
|
+
* { name: 'Scope 8x', quality: 'legendary', level: 85 },
|
|
97
|
+
* { name: 'Level 2 Helmet', quality: 'rare', level: 75 }
|
|
98
|
+
* ]
|
|
99
|
+
* },
|
|
100
|
+
* {
|
|
101
|
+
* name: 'RushMaster',
|
|
102
|
+
* score: 7500,
|
|
103
|
+
* equipments: [
|
|
104
|
+
* { name: 'Vector', quality: 'rare', level: 72 },
|
|
105
|
+
* { name: 'Level 2 Helmet', quality: 'common', level: 65 },
|
|
106
|
+
* { name: 'Quickdraw Mag', quality: 'common', level: 60 },
|
|
107
|
+
* { name: 'Laser Sight', quality: 'rare', level: 68 }
|
|
108
|
+
* ]
|
|
109
|
+
* },
|
|
110
|
+
* {
|
|
111
|
+
* name: 'GhostWarrior',
|
|
112
|
+
* score: 8200,
|
|
113
|
+
* equipments: [
|
|
114
|
+
* { name: 'SCAR-L', quality: 'epic', level: 78 },
|
|
115
|
+
* { name: 'Extended Quickdraw Mag', quality: 'rare', level: 70 },
|
|
116
|
+
* { name: 'Holographic Sight', quality: 'epic', level: 75 },
|
|
117
|
+
* { name: 'Suppressor', quality: 'rare', level: 72 },
|
|
118
|
+
* { name: 'Vertical Grip', quality: 'common', level: 65 }
|
|
119
|
+
* ]
|
|
120
|
+
* },
|
|
121
|
+
* {
|
|
122
|
+
* name: 'DeathDealer',
|
|
123
|
+
* score: 7300,
|
|
124
|
+
* equipments: [
|
|
125
|
+
* { name: 'SKS', quality: 'epic', level: 76 },
|
|
126
|
+
* { name: 'Holographic Sight', quality: 'rare', level: 68 },
|
|
127
|
+
* { name: 'Extended Mag', quality: 'common', level: 65 }
|
|
128
|
+
* ]
|
|
129
|
+
* },
|
|
130
|
+
* {
|
|
131
|
+
* name: 'StormRider',
|
|
132
|
+
* score: 8900,
|
|
133
|
+
* equipments: [
|
|
134
|
+
* { name: 'MK14', quality: 'legendary', level: 92 },
|
|
135
|
+
* { name: 'Level 3 Backpack', quality: 'legendary', level: 85 },
|
|
136
|
+
* { name: 'Scope 8x', quality: 'epic', level: 80 },
|
|
137
|
+
* { name: 'Suppressor', quality: 'legendary', level: 88 },
|
|
138
|
+
* { name: 'Tactical Stock', quality: 'rare', level: 75 }
|
|
139
|
+
* ]
|
|
140
|
+
* },
|
|
141
|
+
* {
|
|
142
|
+
* name: 'CombatLegend',
|
|
143
|
+
* score: 7600,
|
|
144
|
+
* equipments: [
|
|
145
|
+
* { name: 'UMP45', quality: 'rare', level: 74 },
|
|
146
|
+
* { name: 'Level 2 Vest', quality: 'common', level: 67 },
|
|
147
|
+
* { name: 'Red Dot Sight', quality: 'common', level: 62 },
|
|
148
|
+
* { name: 'Extended Mag', quality: 'rare', level: 70 }
|
|
149
|
+
* ]
|
|
150
|
+
* }
|
|
151
|
+
* ];
|
|
152
|
+
*
|
|
153
|
+
* // Create a TreeMultiMap for player rankings
|
|
154
|
+
* const playerRankings = new TreeMultiMap<number, Equipment, Player>(players, {
|
|
155
|
+
* toEntryFn: ({ score, equipments }) => [score, equipments],
|
|
156
|
+
* isMapMode: false
|
|
157
|
+
* });
|
|
158
|
+
*
|
|
159
|
+
* const topPlayersEquipments = playerRankings.rangeSearch([8900, 10000], node => playerRankings.get(node));
|
|
160
|
+
* console.log(topPlayersEquipments); // [
|
|
161
|
+
* // [
|
|
162
|
+
* // {
|
|
163
|
+
* // name: 'MK14',
|
|
164
|
+
* // quality: 'legendary',
|
|
165
|
+
* // level: 92
|
|
166
|
+
* // },
|
|
167
|
+
* // { name: 'Level 3 Backpack', quality: 'legendary', level: 85 },
|
|
168
|
+
* // {
|
|
169
|
+
* // name: 'Scope 8x',
|
|
170
|
+
* // quality: 'epic',
|
|
171
|
+
* // level: 80
|
|
172
|
+
* // },
|
|
173
|
+
* // { name: 'Suppressor', quality: 'legendary', level: 88 },
|
|
174
|
+
* // {
|
|
175
|
+
* // name: 'Tactical Stock',
|
|
176
|
+
* // quality: 'rare',
|
|
177
|
+
* // level: 75
|
|
178
|
+
* // }
|
|
179
|
+
* // ],
|
|
180
|
+
* // [
|
|
181
|
+
* // { name: 'KAR98K', quality: 'legendary', level: 90 },
|
|
182
|
+
* // {
|
|
183
|
+
* // name: 'Level 3 Vest',
|
|
184
|
+
* // quality: 'legendary',
|
|
185
|
+
* // level: 85
|
|
186
|
+
* // },
|
|
187
|
+
* // { name: 'Holographic Sight', quality: 'epic', level: 82 },
|
|
188
|
+
* // {
|
|
189
|
+
* // name: 'Suppressor',
|
|
190
|
+
* // quality: 'legendary',
|
|
191
|
+
* // level: 88
|
|
192
|
+
* // },
|
|
193
|
+
* // { name: 'Level 3 Backpack', quality: 'epic', level: 80 }
|
|
194
|
+
* // ]
|
|
195
|
+
* // ]
|
|
38
196
|
*/
|
|
39
197
|
export declare class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends RedBlackTree<K, V[], R, MK, MV[], MR> implements IBinaryTree<K, V[], R, MK, MV, MR> {
|
|
40
198
|
/**
|
|
@@ -67,15 +225,18 @@ export declare class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = a
|
|
|
67
225
|
* Time Complexity: O(1)
|
|
68
226
|
* Space Complexity: O(1)
|
|
69
227
|
*
|
|
70
|
-
* The function `createNode` overrides the
|
|
71
|
-
*
|
|
72
|
-
* @param {K} key - The `key` parameter
|
|
73
|
-
*
|
|
74
|
-
* @
|
|
75
|
-
*
|
|
228
|
+
* The function `createNode` overrides the creation of a new TreeMultiMapNode with a specified key
|
|
229
|
+
* and value array.
|
|
230
|
+
* @param {K} key - The `key` parameter represents the key of the node being created in the
|
|
231
|
+
* `TreeMultiMap`.
|
|
232
|
+
* @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
|
|
233
|
+
* values associated with a specific key in the TreeMultiMap data structure.
|
|
234
|
+
* @returns A new instance of `TreeMultiMapNode<K, V>` is being returned with the specified key and
|
|
235
|
+
* value. If `_isMapMode` is true, an empty array is passed as the value, otherwise the provided
|
|
236
|
+
* value is used.
|
|
76
237
|
*/
|
|
77
|
-
createNode(key: K): TreeMultiMapNode<K, V>;
|
|
78
|
-
add(
|
|
238
|
+
createNode(key: K, value?: V[]): TreeMultiMapNode<K, V>;
|
|
239
|
+
add(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
|
|
79
240
|
add(key: K, value: V): boolean;
|
|
80
241
|
/**
|
|
81
242
|
* Time Complexity: O(log n)
|