max-priority-queue-typed 2.4.5 → 2.5.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/README.md +63 -0
- package/dist/cjs/index.cjs +400 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +399 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +400 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +399 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/max-priority-queue-typed.js +397 -116
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +416 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +272 -65
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +213 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
|
@@ -5,156 +5,222 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
protected
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
*
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
*
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
8
|
+
export type SegmentTreeOptions<E> = {
|
|
9
|
+
merger: (a: E, b: E) => E;
|
|
10
|
+
identity: E;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Generic Segment Tree with flat array internals.
|
|
14
|
+
*
|
|
15
|
+
* Supports any associative merge operation (sum, min, max, gcd, etc.).
|
|
16
|
+
* Reference: AtCoder Library segtree<S, op, e>.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const sumTree = SegmentTree.sum([1, 2, 3, 4, 5]);
|
|
21
|
+
* sumTree.query(1, 3); // 9 (2+3+4)
|
|
22
|
+
* sumTree.update(2, 10); // [1, 2, 10, 4, 5]
|
|
23
|
+
* sumTree.query(1, 3); // 16 (2+10+4)
|
|
24
|
+
*
|
|
25
|
+
* const minTree = SegmentTree.min([5, 2, 8, 1, 9]);
|
|
26
|
+
* minTree.query(0, 4); // 1
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare class SegmentTree<E = number> implements Iterable<E> {
|
|
30
|
+
protected readonly _merger: (a: E, b: E) => E;
|
|
31
|
+
protected readonly _identity: E;
|
|
32
|
+
protected _n: number;
|
|
33
|
+
protected _tree: E[];
|
|
34
|
+
protected _treeSize: number;
|
|
35
|
+
constructor(elements: E[], options: SegmentTreeOptions<E>);
|
|
36
|
+
/**
|
|
37
|
+
* Create a sum segment tree.
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* const st = SegmentTree.sum([1, 2, 3, 4, 5]);
|
|
41
|
+
* st.query(0, 2); // 6 (1+2+3)
|
|
42
|
+
* st.update(1, 10);
|
|
43
|
+
* st.query(0, 2); // 14 (1+10+3)
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
static sum(elements: number[]): SegmentTree<number>;
|
|
47
|
+
/**
|
|
48
|
+
* Create a min segment tree.
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
* @example
|
|
61
|
+
* // Temperature monitoring with range queries
|
|
62
|
+
* // Hourly temperatures for a day (24 readings)
|
|
63
|
+
* const temps = [18, 17, 16, 15, 16, 18, 21, 24, 27, 29, 31, 32, 33, 32, 31, 29, 27, 25, 23, 21, 20, 19, 18, 17];
|
|
64
|
+
* const tree = SegmentTree.sum(temps);
|
|
65
|
+
*
|
|
66
|
+
* // Average temperature during work hours (9-17)
|
|
67
|
+
* const workSum = tree.query(9, 17);
|
|
68
|
+
* console.log(workSum / 9); // toBeCloseTo;
|
|
69
|
+
*
|
|
70
|
+
* // Sum of morning temps (6-11)
|
|
71
|
+
* console.log(tree.query(6, 11)); // 164;
|
|
72
|
+
*/
|
|
73
|
+
static min(elements: number[]): SegmentTree<number>;
|
|
74
|
+
/**
|
|
75
|
+
* Create a max segment tree.
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* const st = SegmentTree.max([3, 1, 4, 1, 5]);
|
|
79
|
+
* st.query(1, 4); // 5
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
static max(elements: number[]): SegmentTree<number>;
|
|
83
|
+
/**
|
|
84
|
+
* Point update: set element at index to value.
|
|
85
|
+
* Time: O(log n)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
* @example
|
|
98
|
+
* // Dynamic range sum with updates
|
|
99
|
+
* // Monthly revenue data (in thousands)
|
|
100
|
+
* const revenue = [120, 95, 140, 110, 85, 130, 150, 100, 160, 125, 90, 175];
|
|
101
|
+
* const tree = SegmentTree.sum(revenue);
|
|
102
|
+
*
|
|
103
|
+
* // Q1 revenue (Jan-Mar)
|
|
104
|
+
* console.log(tree.query(0, 2)); // 355;
|
|
105
|
+
*
|
|
106
|
+
* // Update March revenue from 140 to 200
|
|
107
|
+
* tree.update(2, 200);
|
|
108
|
+
*
|
|
109
|
+
* // Q1 revenue after update
|
|
110
|
+
* console.log(tree.query(0, 2)); // 415;
|
|
111
|
+
*
|
|
112
|
+
* // H1 revenue (Jan-Jun)
|
|
113
|
+
* console.log(tree.query(0, 5)); // 740;
|
|
114
|
+
*/
|
|
115
|
+
update(index: number, value: E): void;
|
|
116
|
+
/**
|
|
117
|
+
* Range query: returns merger result over [start, end] (inclusive).
|
|
118
|
+
* Time: O(log n)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
* @example
|
|
131
|
+
* // Range sum query on an array
|
|
132
|
+
* const tree = SegmentTree.sum([1, 3, 5, 7, 9, 11]);
|
|
133
|
+
*
|
|
134
|
+
* // Query sum of range [1, 3] → 3 + 5 + 7 = 15
|
|
135
|
+
* console.log(tree.query(1, 3)); // 15;
|
|
136
|
+
*
|
|
137
|
+
* // Query entire range
|
|
138
|
+
* console.log(tree.query(0, 5)); // 36;
|
|
139
|
+
*
|
|
140
|
+
* // Query single element
|
|
141
|
+
* console.log(tree.query(2, 2)); // 5;
|
|
142
|
+
*/
|
|
143
|
+
query(start: number, end: number): E;
|
|
144
|
+
/**
|
|
145
|
+
* Get element at index.
|
|
146
|
+
* Time: O(1)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
* @example
|
|
159
|
+
* // Point access on segment tree
|
|
160
|
+
* const st = SegmentTree.sum([10, 20, 30, 40]);
|
|
161
|
+
* console.log(st.get(0)); // 10;
|
|
162
|
+
* console.log(st.get(2)); // 30;
|
|
163
|
+
*/
|
|
164
|
+
get(index: number): E;
|
|
165
|
+
/**
|
|
166
|
+
* Find the largest r such that predicate(query(left, r)) is true.
|
|
167
|
+
* Returns left-1 if predicate(identity) is false.
|
|
168
|
+
* Returns n-1 if predicate holds for the entire range [left, n-1].
|
|
169
|
+
* Time: O(log n)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
* @example
|
|
182
|
+
* // Find rightmost position where predicate holds
|
|
183
|
+
* // Prefix sums: find the rightmost index where prefix sum < 10
|
|
184
|
+
* const st = SegmentTree.sum([3, 1, 4, 1, 5]);
|
|
185
|
+
* // maxRight(0, sum => sum < 10) — prefix [3,4,8,9,14]
|
|
186
|
+
* // sum < 10 holds through index 3 (prefix=9), fails at 4 (prefix=14)
|
|
187
|
+
* const result = st.maxRight(0, sum => sum < 10);
|
|
188
|
+
* console.log(result); // 3;
|
|
189
|
+
*/
|
|
190
|
+
maxRight(left: number, predicate: (segValue: E) => boolean): number;
|
|
191
|
+
/**
|
|
192
|
+
* Find the smallest l such that predicate(query(l, right)) is true.
|
|
193
|
+
* Returns right+1 if predicate(identity) is false.
|
|
194
|
+
* Returns 0 if predicate holds for the entire range [0, right].
|
|
195
|
+
* Time: O(log n)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
* @example
|
|
208
|
+
* // Find leftmost position where predicate holds
|
|
209
|
+
* const st = SegmentTree.sum([3, 1, 4, 1, 5]);
|
|
210
|
+
* // minLeft(5, sum => sum < 7) — suffix sums from right
|
|
211
|
+
* // From right: [5]=5 < 7, [1,5]=6 < 7, [4,1,5]=10 ≥ 7
|
|
212
|
+
* const result = st.minLeft(5, sum => sum < 7);
|
|
213
|
+
* console.log(result); // 3;
|
|
214
|
+
*/
|
|
215
|
+
minLeft(right: number, predicate: (segValue: E) => boolean): number;
|
|
216
|
+
get size(): number;
|
|
217
|
+
isEmpty(): boolean;
|
|
218
|
+
clone(): SegmentTree<E>;
|
|
219
|
+
toArray(): E[];
|
|
220
|
+
/**
|
|
221
|
+
* Iterates over leaf values in index order.
|
|
222
|
+
*/
|
|
223
|
+
[Symbol.iterator](): IterableIterator<E>;
|
|
224
|
+
forEach(callback: (value: E, index: number) => void): void;
|
|
225
|
+
print(): void;
|
|
160
226
|
}
|