heap-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 +8 -105
- package/dist/cjs/index.cjs +403 -98
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +402 -97
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +403 -99
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +402 -98
- 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/heap-typed.js +400 -95
- package/dist/umd/heap-typed.js.map +1 -1
- package/dist/umd/heap-typed.min.js +1 -1
- package/dist/umd/heap-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
package/README.md
CHANGED
|
@@ -34,111 +34,6 @@ yarn add heap-typed
|
|
|
34
34
|
|
|
35
35
|
[//]: # (No deletion!!! Start of Example Replace Section)
|
|
36
36
|
|
|
37
|
-
### basic Heap creation and add operation
|
|
38
|
-
```typescript
|
|
39
|
-
// Create a min heap (default)
|
|
40
|
-
const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
41
|
-
|
|
42
|
-
// Verify size
|
|
43
|
-
console.log(minHeap.size); // 6;
|
|
44
|
-
|
|
45
|
-
// Add new element
|
|
46
|
-
minHeap.add(4);
|
|
47
|
-
console.log(minHeap.size); // 7;
|
|
48
|
-
|
|
49
|
-
// Min heap property: smallest element at root
|
|
50
|
-
const min = minHeap.peek();
|
|
51
|
-
console.log(min); // 1;
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### Heap with custom comparator (MaxHeap behavior)
|
|
55
|
-
```typescript
|
|
56
|
-
interface Task {
|
|
57
|
-
id: number;
|
|
58
|
-
priority: number;
|
|
59
|
-
name: string;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Custom comparator for max heap behavior (higher priority first)
|
|
63
|
-
const tasks: Task[] = [
|
|
64
|
-
{ id: 1, priority: 5, name: 'Email' },
|
|
65
|
-
{ id: 2, priority: 3, name: 'Chat' },
|
|
66
|
-
{ id: 3, priority: 8, name: 'Alert' }
|
|
67
|
-
];
|
|
68
|
-
|
|
69
|
-
const maxHeap = new Heap(tasks, {
|
|
70
|
-
comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
console.log(maxHeap.size); // 3;
|
|
74
|
-
|
|
75
|
-
// Peek returns highest priority task
|
|
76
|
-
const topTask = maxHeap.peek();
|
|
77
|
-
console.log(topTask?.priority); // 8;
|
|
78
|
-
console.log(topTask?.name); // 'Alert';
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
### Heap for event processing with priority
|
|
82
|
-
```typescript
|
|
83
|
-
interface Event {
|
|
84
|
-
id: number;
|
|
85
|
-
type: 'critical' | 'warning' | 'info';
|
|
86
|
-
timestamp: number;
|
|
87
|
-
message: string;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Custom priority: critical > warning > info
|
|
91
|
-
const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
92
|
-
|
|
93
|
-
const eventHeap = new Heap<Event>([], {
|
|
94
|
-
comparator: (a: Event, b: Event) => {
|
|
95
|
-
const priorityA = priorityMap[a.type];
|
|
96
|
-
const priorityB = priorityMap[b.type];
|
|
97
|
-
return priorityB - priorityA; // Higher priority first
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
// Add events in random order
|
|
102
|
-
eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
103
|
-
eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
104
|
-
eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
105
|
-
eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
106
|
-
eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
107
|
-
|
|
108
|
-
console.log(eventHeap.size); // 5;
|
|
109
|
-
|
|
110
|
-
// Process events by priority (critical first)
|
|
111
|
-
const processedOrder: Event[] = [];
|
|
112
|
-
while (eventHeap.size > 0) {
|
|
113
|
-
const event = eventHeap.poll();
|
|
114
|
-
if (event) {
|
|
115
|
-
processedOrder.push(event);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Verify critical events came first
|
|
120
|
-
console.log(processedOrder[0].type); // 'critical';
|
|
121
|
-
console.log(processedOrder[1].type); // 'critical';
|
|
122
|
-
console.log(processedOrder[2].type); // 'warning';
|
|
123
|
-
console.log(processedOrder[3].type); // 'info';
|
|
124
|
-
console.log(processedOrder[4].type); // 'info';
|
|
125
|
-
|
|
126
|
-
// Verify O(log n) operations
|
|
127
|
-
const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
128
|
-
|
|
129
|
-
// Add - O(log n)
|
|
130
|
-
newHeap.add(2);
|
|
131
|
-
console.log(newHeap.size); // 5;
|
|
132
|
-
|
|
133
|
-
// Poll - O(log n)
|
|
134
|
-
const removed = newHeap.poll();
|
|
135
|
-
console.log(removed); // 1;
|
|
136
|
-
|
|
137
|
-
// Peek - O(1)
|
|
138
|
-
const top = newHeap.peek();
|
|
139
|
-
console.log(top); // 2;
|
|
140
|
-
```
|
|
141
|
-
|
|
142
37
|
### Use Heap to solve top k problems
|
|
143
38
|
```typescript
|
|
144
39
|
function topKElements(arr: number[], k: number): number[] {
|
|
@@ -262,6 +157,14 @@ yarn add heap-typed
|
|
|
262
157
|
console.log(scheduleTasks(tasks, 2)); // expectedMap;
|
|
263
158
|
```
|
|
264
159
|
|
|
160
|
+
### Get all elements as array
|
|
161
|
+
```typescript
|
|
162
|
+
const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
163
|
+
const arr = heap.toArray();
|
|
164
|
+
console.log(arr.length); // 5;
|
|
165
|
+
console.log(arr.sort()); // [1, 2, 3, 4, 5];
|
|
166
|
+
```
|
|
167
|
+
|
|
265
168
|
[//]: # (No deletion!!! End of Example Replace Section)
|
|
266
169
|
|
|
267
170
|
|