binary-tree-typed 2.4.4 → 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 +0 -84
- package/dist/cjs/index.cjs +965 -420
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +962 -417
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +965 -421
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +962 -418
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- 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 +439 -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 +217 -31
- 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/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -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 +313 -66
- 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/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/binary-tree-typed.js +959 -414
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +3 -3
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
- package/src/data-structures/binary-tree/binary-tree.ts +542 -121
- package/src/data-structures/binary-tree/bst.ts +346 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1292 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
- package/src/data-structures/binary-tree/tree-set.ts +1143 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +223 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +299 -59
- package/src/data-structures/hash/hash-map.ts +243 -79
- package/src/data-structures/heap/heap.ts +291 -102
- package/src/data-structures/heap/max-heap.ts +48 -3
- 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 +425 -22
- package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
- 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 +343 -68
- 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 +215 -44
- 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
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
|
@@ -23,105 +23,6 @@ import { IterableElementBase } from '../base';
|
|
|
23
23
|
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
24
24
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
25
25
|
* @example
|
|
26
|
-
* // basic Heap creation and add operation
|
|
27
|
-
* // Create a min heap (default)
|
|
28
|
-
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
29
|
-
*
|
|
30
|
-
* // Verify size
|
|
31
|
-
* console.log(minHeap.size); // 6;
|
|
32
|
-
*
|
|
33
|
-
* // Add new element
|
|
34
|
-
* minHeap.add(4);
|
|
35
|
-
* console.log(minHeap.size); // 7;
|
|
36
|
-
*
|
|
37
|
-
* // Min heap property: smallest element at root
|
|
38
|
-
* const min = minHeap.peek();
|
|
39
|
-
* console.log(min); // 1;
|
|
40
|
-
* @example
|
|
41
|
-
* // Heap with custom comparator (MaxHeap behavior)
|
|
42
|
-
* interface Task {
|
|
43
|
-
* id: number;
|
|
44
|
-
* priority: number;
|
|
45
|
-
* name: string;
|
|
46
|
-
* }
|
|
47
|
-
*
|
|
48
|
-
* // Custom comparator for max heap behavior (higher priority first)
|
|
49
|
-
* const tasks: Task[] = [
|
|
50
|
-
* { id: 1, priority: 5, name: 'Email' },
|
|
51
|
-
* { id: 2, priority: 3, name: 'Chat' },
|
|
52
|
-
* { id: 3, priority: 8, name: 'Alert' }
|
|
53
|
-
* ];
|
|
54
|
-
*
|
|
55
|
-
* const maxHeap = new Heap(tasks, {
|
|
56
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
57
|
-
* });
|
|
58
|
-
*
|
|
59
|
-
* console.log(maxHeap.size); // 3;
|
|
60
|
-
*
|
|
61
|
-
* // Peek returns highest priority task
|
|
62
|
-
* const topTask = maxHeap.peek();
|
|
63
|
-
* console.log(topTask?.priority); // 8;
|
|
64
|
-
* console.log(topTask?.name); // 'Alert';
|
|
65
|
-
* @example
|
|
66
|
-
* // Heap for event processing with priority
|
|
67
|
-
* interface Event {
|
|
68
|
-
* id: number;
|
|
69
|
-
* type: 'critical' | 'warning' | 'info';
|
|
70
|
-
* timestamp: number;
|
|
71
|
-
* message: string;
|
|
72
|
-
* }
|
|
73
|
-
*
|
|
74
|
-
* // Custom priority: critical > warning > info
|
|
75
|
-
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
76
|
-
*
|
|
77
|
-
* const eventHeap = new Heap<Event>([], {
|
|
78
|
-
* comparator: (a: Event, b: Event) => {
|
|
79
|
-
* const priorityA = priorityMap[a.type];
|
|
80
|
-
* const priorityB = priorityMap[b.type];
|
|
81
|
-
* return priorityB - priorityA; // Higher priority first
|
|
82
|
-
* }
|
|
83
|
-
* });
|
|
84
|
-
*
|
|
85
|
-
* // Add events in random order
|
|
86
|
-
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
87
|
-
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
88
|
-
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
89
|
-
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
90
|
-
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
91
|
-
*
|
|
92
|
-
* console.log(eventHeap.size); // 5;
|
|
93
|
-
*
|
|
94
|
-
* // Process events by priority (critical first)
|
|
95
|
-
* const processedOrder: Event[] = [];
|
|
96
|
-
* while (eventHeap.size > 0) {
|
|
97
|
-
* const event = eventHeap.poll();
|
|
98
|
-
* if (event) {
|
|
99
|
-
* processedOrder.push(event);
|
|
100
|
-
* }
|
|
101
|
-
* }
|
|
102
|
-
*
|
|
103
|
-
* // Verify critical events came first
|
|
104
|
-
* console.log(processedOrder[0].type); // 'critical';
|
|
105
|
-
* console.log(processedOrder[1].type); // 'critical';
|
|
106
|
-
* console.log(processedOrder[2].type); // 'warning';
|
|
107
|
-
* console.log(processedOrder[3].type); // 'info';
|
|
108
|
-
* console.log(processedOrder[4].type); // 'info';
|
|
109
|
-
*
|
|
110
|
-
* // Verify O(log n) operations
|
|
111
|
-
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
112
|
-
*
|
|
113
|
-
* // Add - O(log n)
|
|
114
|
-
* newHeap.add(2);
|
|
115
|
-
* console.log(newHeap.size); // 5;
|
|
116
|
-
*
|
|
117
|
-
* // Poll - O(log n)
|
|
118
|
-
* const removed = newHeap.poll();
|
|
119
|
-
* console.log(removed); // 1;
|
|
120
|
-
*
|
|
121
|
-
* // Peek - O(1)
|
|
122
|
-
* const top = newHeap.peek();
|
|
123
|
-
* console.log(top); // 2;
|
|
124
|
-
* @example
|
|
125
26
|
* // Use Heap to solve top k problems
|
|
126
27
|
* function topKElements(arr: number[], k: number): number[] {
|
|
127
28
|
* const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
|
|
@@ -236,6 +137,12 @@ import { IterableElementBase } from '../base';
|
|
|
236
137
|
* ['Task5', 4]
|
|
237
138
|
* ]);
|
|
238
139
|
* console.log(scheduleTasks(tasks, 2)); // expectedMap;
|
|
140
|
+
* @example
|
|
141
|
+
* // Get all elements as array
|
|
142
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
143
|
+
* const arr = heap.toArray();
|
|
144
|
+
* console.log(arr.length); // 5;
|
|
145
|
+
* console.log(arr.sort()); // [1, 2, 3, 4, 5];
|
|
239
146
|
*/
|
|
240
147
|
export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
241
148
|
protected _equals: (a: E, b: E) => boolean;
|
|
@@ -258,6 +165,26 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
258
165
|
* Get the number of elements.
|
|
259
166
|
* @remarks Time O(1), Space O(1)
|
|
260
167
|
* @returns Heap size.
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
* @example
|
|
180
|
+
* // Track heap capacity
|
|
181
|
+
* const heap = new Heap<number>();
|
|
182
|
+
* console.log(heap.size); // 0;
|
|
183
|
+
* heap.add(10);
|
|
184
|
+
* heap.add(20);
|
|
185
|
+
* console.log(heap.size); // 2;
|
|
186
|
+
* heap.poll();
|
|
187
|
+
* console.log(heap.size); // 1;
|
|
261
188
|
*/
|
|
262
189
|
get size(): number;
|
|
263
190
|
/**
|
|
@@ -292,6 +219,32 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
292
219
|
* @remarks Time O(1) amortized, Space O(1)
|
|
293
220
|
* @param element - Element to insert.
|
|
294
221
|
* @returns True.
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
* @example
|
|
234
|
+
* // basic Heap creation and add operation
|
|
235
|
+
* // Create a min heap (default)
|
|
236
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
237
|
+
*
|
|
238
|
+
* // Verify size
|
|
239
|
+
* console.log(minHeap.size); // 6;
|
|
240
|
+
*
|
|
241
|
+
* // Add new element
|
|
242
|
+
* minHeap.add(4);
|
|
243
|
+
* console.log(minHeap.size); // 7;
|
|
244
|
+
*
|
|
245
|
+
* // Min heap property: smallest element at root
|
|
246
|
+
* const min = minHeap.peek();
|
|
247
|
+
* console.log(min); // 1;
|
|
295
248
|
*/
|
|
296
249
|
add(element: E): boolean;
|
|
297
250
|
/**
|
|
@@ -299,30 +252,179 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
299
252
|
* @remarks Time O(N log N), Space O(1)
|
|
300
253
|
* @param elements - Iterable of elements or raw values.
|
|
301
254
|
* @returns Array of per-element success flags.
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
* @example
|
|
264
|
+
* // Add multiple elements
|
|
265
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
266
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
267
|
+
* console.log(heap.peek()); // 1;
|
|
268
|
+
* console.log(heap.size); // 4;
|
|
302
269
|
*/
|
|
303
270
|
addMany(elements: Iterable<E | R>): boolean[];
|
|
304
271
|
/**
|
|
305
272
|
* Remove and return the top element.
|
|
306
273
|
* @remarks Time O(log N), Space O(1)
|
|
307
274
|
* @returns Top element or undefined.
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
* @example
|
|
287
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
288
|
+
* interface Task {
|
|
289
|
+
* id: number;
|
|
290
|
+
* priority: number;
|
|
291
|
+
* name: string;
|
|
292
|
+
* }
|
|
293
|
+
*
|
|
294
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
295
|
+
* const tasks: Task[] = [
|
|
296
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
297
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
298
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
299
|
+
* ];
|
|
300
|
+
*
|
|
301
|
+
* const maxHeap = new Heap(tasks, {
|
|
302
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
303
|
+
* });
|
|
304
|
+
*
|
|
305
|
+
* console.log(maxHeap.size); // 3;
|
|
306
|
+
*
|
|
307
|
+
* // Peek returns highest priority task
|
|
308
|
+
* const topTask = maxHeap.peek();
|
|
309
|
+
* console.log(topTask?.priority); // 8;
|
|
310
|
+
* console.log(topTask?.name); // 'Alert';
|
|
308
311
|
*/
|
|
309
312
|
poll(): E | undefined;
|
|
310
313
|
/**
|
|
311
314
|
* Get the current top element without removing it.
|
|
312
315
|
* @remarks Time O(1), Space O(1)
|
|
313
316
|
* @returns Top element or undefined.
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
* @example
|
|
329
|
+
* // Heap for event processing with priority
|
|
330
|
+
* interface Event {
|
|
331
|
+
* id: number;
|
|
332
|
+
* type: 'critical' | 'warning' | 'info';
|
|
333
|
+
* timestamp: number;
|
|
334
|
+
* message: string;
|
|
335
|
+
* }
|
|
336
|
+
*
|
|
337
|
+
* // Custom priority: critical > warning > info
|
|
338
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
339
|
+
*
|
|
340
|
+
* const eventHeap = new Heap<Event>([], {
|
|
341
|
+
* comparator: (a: Event, b: Event) => {
|
|
342
|
+
* const priorityA = priorityMap[a.type];
|
|
343
|
+
* const priorityB = priorityMap[b.type];
|
|
344
|
+
* return priorityB - priorityA; // Higher priority first
|
|
345
|
+
* }
|
|
346
|
+
* });
|
|
347
|
+
*
|
|
348
|
+
* // Add events in random order
|
|
349
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
350
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
351
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
352
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
353
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
354
|
+
*
|
|
355
|
+
* console.log(eventHeap.size); // 5;
|
|
356
|
+
*
|
|
357
|
+
* // Process events by priority (critical first)
|
|
358
|
+
* const processedOrder: Event[] = [];
|
|
359
|
+
* while (eventHeap.size > 0) {
|
|
360
|
+
* const event = eventHeap.poll();
|
|
361
|
+
* if (event) {
|
|
362
|
+
* processedOrder.push(event);
|
|
363
|
+
* }
|
|
364
|
+
* }
|
|
365
|
+
*
|
|
366
|
+
* // Verify critical events came first
|
|
367
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
368
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
369
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
370
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
371
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
372
|
+
*
|
|
373
|
+
* // Verify O(log n) operations
|
|
374
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
375
|
+
*
|
|
376
|
+
* // Add - O(log n)
|
|
377
|
+
* newHeap.add(2);
|
|
378
|
+
* console.log(newHeap.size); // 5;
|
|
379
|
+
*
|
|
380
|
+
* // Poll - O(log n)
|
|
381
|
+
* const removed = newHeap.poll();
|
|
382
|
+
* console.log(removed); // 1;
|
|
383
|
+
*
|
|
384
|
+
* // Peek - O(1)
|
|
385
|
+
* const top = newHeap.peek();
|
|
386
|
+
* console.log(top); // 2;
|
|
314
387
|
*/
|
|
315
388
|
peek(): E | undefined;
|
|
316
389
|
/**
|
|
317
390
|
* Check whether the heap is empty.
|
|
318
391
|
* @remarks Time O(1), Space O(1)
|
|
319
392
|
* @returns True if size is 0.
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
* @example
|
|
403
|
+
* // Check if heap is empty
|
|
404
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
405
|
+
* console.log(heap.isEmpty()); // true;
|
|
406
|
+
* heap.add(1);
|
|
407
|
+
* console.log(heap.isEmpty()); // false;
|
|
320
408
|
*/
|
|
321
409
|
isEmpty(): boolean;
|
|
322
410
|
/**
|
|
323
411
|
* Remove all elements.
|
|
324
412
|
* @remarks Time O(1), Space O(1)
|
|
325
413
|
* @returns void
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
* @example
|
|
424
|
+
* // Remove all elements
|
|
425
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
426
|
+
* heap.clear();
|
|
427
|
+
* console.log(heap.isEmpty()); // true;
|
|
326
428
|
*/
|
|
327
429
|
clear(): void;
|
|
328
430
|
/**
|
|
@@ -337,6 +439,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
337
439
|
* @remarks Time O(N), Space O(1)
|
|
338
440
|
* @param element - Element to search for.
|
|
339
441
|
* @returns True if found.
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
* @example
|
|
445
|
+
* // Check element existence
|
|
446
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
447
|
+
* console.log(heap.has(1)); // true;
|
|
448
|
+
* console.log(heap.has(99)); // false;
|
|
340
449
|
*/
|
|
341
450
|
has(element: E): boolean;
|
|
342
451
|
/**
|
|
@@ -344,6 +453,19 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
344
453
|
* @remarks Time O(N), Space O(1)
|
|
345
454
|
* @param element - Element to delete.
|
|
346
455
|
* @returns True if an element was removed.
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
* @example
|
|
465
|
+
* // Remove specific element
|
|
466
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
467
|
+
* heap.delete(4);
|
|
468
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
347
469
|
*/
|
|
348
470
|
delete(element: E): boolean;
|
|
349
471
|
/**
|
|
@@ -365,6 +487,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
365
487
|
* @remarks Time O(N), Space O(H)
|
|
366
488
|
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
367
489
|
* @returns Array of visited elements.
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
* @example
|
|
493
|
+
* // Depth-first traversal
|
|
494
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
495
|
+
* const result = heap.dfs('IN');
|
|
496
|
+
* console.log(result.length); // 3;
|
|
368
497
|
*/
|
|
369
498
|
dfs(order?: DFSOrderPattern): E[];
|
|
370
499
|
/**
|
|
@@ -377,12 +506,44 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
377
506
|
* Return all elements in ascending order by repeatedly polling.
|
|
378
507
|
* @remarks Time O(N log N), Space O(N)
|
|
379
508
|
* @returns Sorted array of elements.
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
* @example
|
|
521
|
+
* // Sort elements using heap
|
|
522
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
523
|
+
* const sorted = heap.sort();
|
|
524
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
380
525
|
*/
|
|
381
526
|
sort(): E[];
|
|
382
527
|
/**
|
|
383
528
|
* Deep clone this heap.
|
|
384
529
|
* @remarks Time O(N), Space O(N)
|
|
385
530
|
* @returns A new heap with the same elements.
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
* @example
|
|
541
|
+
* // Create independent copy
|
|
542
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
543
|
+
* const copy = heap.clone();
|
|
544
|
+
* copy.poll();
|
|
545
|
+
* console.log(heap.size); // 3;
|
|
546
|
+
* console.log(copy.size); // 2;
|
|
386
547
|
*/
|
|
387
548
|
clone(): this;
|
|
388
549
|
/**
|
|
@@ -391,6 +552,20 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
391
552
|
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
392
553
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
393
554
|
* @returns A new heap with the kept elements.
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
* @example
|
|
565
|
+
* // Filter elements
|
|
566
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
567
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
568
|
+
* console.log(evens.size); // 2;
|
|
394
569
|
*/
|
|
395
570
|
filter(callback: ElementCallback<E, R, boolean>, thisArg?: unknown): this;
|
|
396
571
|
/**
|
|
@@ -402,6 +577,19 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
402
577
|
* @param options - Options for the output heap, including comparator for EM.
|
|
403
578
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
404
579
|
* @returns A new heap with mapped elements.
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
* @example
|
|
589
|
+
* // Transform elements
|
|
590
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
591
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
592
|
+
* console.log(doubled.peek()); // 2;
|
|
405
593
|
*/
|
|
406
594
|
map<EM, RM>(callback: ElementCallback<E, R, EM>, options: HeapOptions<EM, RM> & {
|
|
407
595
|
comparator: Comparator<EM>;
|
|
@@ -21,6 +21,52 @@ import { Heap } from './heap';
|
|
|
21
21
|
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
22
22
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
23
23
|
* @example
|
|
24
|
+
* // Find the K largest elements
|
|
25
|
+
* const data = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
|
|
26
|
+
* const heap = new MaxHeap(data);
|
|
27
|
+
*
|
|
28
|
+
* // Extract top 3 elements
|
|
29
|
+
* const top3 = [];
|
|
30
|
+
* for (let i = 0; i < 3; i++) {
|
|
31
|
+
* top3.push(heap.poll());
|
|
32
|
+
* }
|
|
33
|
+
* console.log(top3); // [9, 6, 5];
|
|
34
|
+
* @example
|
|
35
|
+
* // Priority-based task processing
|
|
36
|
+
* interface Task {
|
|
37
|
+
* name: string;
|
|
38
|
+
* priority: number;
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* const heap = new MaxHeap<Task>([], {
|
|
42
|
+
* comparator: (a, b) => b.priority - a.priority
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* heap.add({ name: 'Low priority', priority: 1 });
|
|
46
|
+
* heap.add({ name: 'Critical fix', priority: 10 });
|
|
47
|
+
* heap.add({ name: 'Medium task', priority: 5 });
|
|
48
|
+
*
|
|
49
|
+
* // Highest priority first
|
|
50
|
+
* console.log(heap.poll()?.name); // 'Critical fix';
|
|
51
|
+
* console.log(heap.poll()?.name); // 'Medium task';
|
|
52
|
+
* console.log(heap.poll()?.name); // 'Low priority';
|
|
53
|
+
* @example
|
|
54
|
+
* // Real-time top score tracking
|
|
55
|
+
* const scores = new MaxHeap<number>();
|
|
56
|
+
*
|
|
57
|
+
* // Stream of scores coming in
|
|
58
|
+
* for (const score of [72, 85, 91, 68, 95, 78, 88]) {
|
|
59
|
+
* scores.add(score);
|
|
60
|
+
* }
|
|
61
|
+
*
|
|
62
|
+
* // Current highest score without removing
|
|
63
|
+
* console.log(scores.peek()); // 95;
|
|
64
|
+
* console.log(scores.size); // 7;
|
|
65
|
+
*
|
|
66
|
+
* // Remove top 2 scores
|
|
67
|
+
* console.log(scores.poll()); // 95;
|
|
68
|
+
* console.log(scores.poll()); // 91;
|
|
69
|
+
* console.log(scores.peek()); // 88;
|
|
24
70
|
*/
|
|
25
71
|
export declare class MaxHeap<E = any, R = any> extends Heap<E, R> {
|
|
26
72
|
/**
|
|
@@ -22,6 +22,65 @@ import { Heap } from './heap';
|
|
|
22
22
|
* 7. Efficient Sorting Algorithms: For example, heap sort. MinHeap sort uses the properties of a heap to sort elements.
|
|
23
23
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
24
24
|
* @example
|
|
25
|
+
* // Merge K sorted arrays
|
|
26
|
+
* const arrays = [
|
|
27
|
+
* [1, 4, 7],
|
|
28
|
+
* [2, 5, 8],
|
|
29
|
+
* [3, 6, 9]
|
|
30
|
+
* ];
|
|
31
|
+
*
|
|
32
|
+
* // Use min heap to merge: track (value, arrayIndex, elementIndex)
|
|
33
|
+
* const heap = new MinHeap<[number, number, number]>([], {
|
|
34
|
+
* comparator: (a, b) => a[0] - b[0]
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // Initialize with first element of each array
|
|
38
|
+
* arrays.forEach((arr, i) => heap.add([arr[0], i, 0]));
|
|
39
|
+
*
|
|
40
|
+
* const merged: number[] = [];
|
|
41
|
+
* while (heap.size > 0) {
|
|
42
|
+
* const [val, arrIdx, elemIdx] = heap.poll()!;
|
|
43
|
+
* merged.push(val);
|
|
44
|
+
* if (elemIdx + 1 < arrays[arrIdx].length) {
|
|
45
|
+
* heap.add([arrays[arrIdx][elemIdx + 1], arrIdx, elemIdx + 1]);
|
|
46
|
+
* }
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* console.log(merged); // [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
50
|
+
* @example
|
|
51
|
+
* // Dijkstra-style shortest distance tracking
|
|
52
|
+
* // Simulating distance updates: (distance, nodeId)
|
|
53
|
+
* const heap = new MinHeap<[number, string]>([], {
|
|
54
|
+
* comparator: (a, b) => a[0] - b[0]
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
* heap.add([0, 'start']);
|
|
58
|
+
* heap.add([10, 'A']);
|
|
59
|
+
* heap.add([5, 'B']);
|
|
60
|
+
* heap.add([3, 'C']);
|
|
61
|
+
*
|
|
62
|
+
* // Process nearest node first
|
|
63
|
+
* console.log(heap.poll()); // [0, 'start'];
|
|
64
|
+
* console.log(heap.poll()); // [3, 'C'];
|
|
65
|
+
* console.log(heap.poll()); // [5, 'B'];
|
|
66
|
+
* console.log(heap.poll()); // [10, 'A'];
|
|
67
|
+
* @example
|
|
68
|
+
* // Running median with min heap (upper half)
|
|
69
|
+
* const upperHalf = new MinHeap<number>();
|
|
70
|
+
*
|
|
71
|
+
* // Add larger numbers to min heap
|
|
72
|
+
* for (const n of [5, 8, 3, 9, 1]) {
|
|
73
|
+
* upperHalf.add(n);
|
|
74
|
+
* }
|
|
75
|
+
*
|
|
76
|
+
* // Smallest of the upper half is always accessible
|
|
77
|
+
* console.log(upperHalf.peek()); // 1;
|
|
78
|
+
* console.log(upperHalf.size); // 5;
|
|
79
|
+
*
|
|
80
|
+
* // Remove smallest repeatedly
|
|
81
|
+
* console.log(upperHalf.poll()); // 1;
|
|
82
|
+
* console.log(upperHalf.poll()); // 3;
|
|
83
|
+
* console.log(upperHalf.peek()); // 5;
|
|
25
84
|
*/
|
|
26
85
|
export declare class MinHeap<E = any, R = any> extends Heap<E, R> {
|
|
27
86
|
/**
|