stack-typed 1.53.6 → 1.53.8
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.d.ts +12 -0
- package/dist/common/index.js +28 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +9 -12
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +55 -20
- package/dist/data-structures/binary-tree/binary-tree.js +102 -68
- package/dist/data-structures/binary-tree/bst.d.ts +131 -37
- package/dist/data-structures/binary-tree/bst.js +222 -69
- 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} +53 -0
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +56 -3
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +7 -7
- package/dist/data-structures/hash/hash-map.d.ts +30 -0
- package/dist/data-structures/hash/hash-map.js +30 -0
- package/dist/data-structures/heap/heap.d.ts +26 -9
- package/dist/data-structures/heap/heap.js +37 -17
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +54 -9
- package/dist/data-structures/linked-list/doubly-linked-list.js +80 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +35 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +55 -11
- 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 +111 -6
- package/dist/data-structures/trie/trie.js +123 -14
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +25 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -11
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +110 -66
- package/src/data-structures/binary-tree/bst.ts +232 -72
- package/src/data-structures/binary-tree/index.ts +1 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +56 -3
- package/src/data-structures/binary-tree/tree-multi-map.ts +6 -6
- package/src/data-structures/hash/hash-map.ts +30 -0
- package/src/data-structures/heap/heap.ts +72 -49
- package/src/data-structures/linked-list/doubly-linked-list.ts +173 -105
- package/src/data-structures/linked-list/singly-linked-list.ts +61 -11
- 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 +123 -13
- package/src/index.ts +2 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +3 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
|
@@ -97,6 +97,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
|
+
* Time Complexity: O(1)
|
|
101
|
+
* Space Complexity: O(1)
|
|
102
|
+
*
|
|
100
103
|
* The function checks if a given element is an array with exactly two elements.
|
|
101
104
|
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
102
105
|
* data type.
|
|
@@ -107,6 +110,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
107
110
|
}
|
|
108
111
|
|
|
109
112
|
/**
|
|
113
|
+
* Time Complexity: O(1)
|
|
114
|
+
* Space Complexity: O(1)
|
|
115
|
+
*
|
|
110
116
|
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
111
117
|
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
112
118
|
*/
|
|
@@ -115,6 +121,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
115
121
|
}
|
|
116
122
|
|
|
117
123
|
/**
|
|
124
|
+
* Time Complexity: O(1)
|
|
125
|
+
* Space Complexity: O(1)
|
|
126
|
+
*
|
|
118
127
|
* The clear() function resets the state of an object by clearing its internal store, object map, and
|
|
119
128
|
* size.
|
|
120
129
|
*/
|
|
@@ -125,6 +134,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
125
134
|
}
|
|
126
135
|
|
|
127
136
|
/**
|
|
137
|
+
* Time Complexity: O(1)
|
|
138
|
+
* Space Complexity: O(1)
|
|
139
|
+
*
|
|
128
140
|
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if
|
|
129
141
|
* the key is not already present.
|
|
130
142
|
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It
|
|
@@ -150,6 +162,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
150
162
|
}
|
|
151
163
|
|
|
152
164
|
/**
|
|
165
|
+
* Time Complexity: O(k)
|
|
166
|
+
* Space Complexity: O(k)
|
|
167
|
+
*
|
|
153
168
|
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
154
169
|
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
155
170
|
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
@@ -175,6 +190,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
175
190
|
}
|
|
176
191
|
|
|
177
192
|
/**
|
|
193
|
+
* Time Complexity: O(1)
|
|
194
|
+
* Space Complexity: O(1)
|
|
195
|
+
*
|
|
178
196
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
179
197
|
* a string map.
|
|
180
198
|
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
@@ -192,6 +210,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
192
210
|
}
|
|
193
211
|
|
|
194
212
|
/**
|
|
213
|
+
* Time Complexity: O(1)
|
|
214
|
+
* Space Complexity: O(1)
|
|
215
|
+
*
|
|
195
216
|
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it
|
|
196
217
|
* is an object key or not.
|
|
197
218
|
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
|
|
@@ -207,6 +228,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
207
228
|
}
|
|
208
229
|
|
|
209
230
|
/**
|
|
231
|
+
* Time Complexity: O(1)
|
|
232
|
+
* Space Complexity: O(1)
|
|
233
|
+
*
|
|
210
234
|
* The `delete` function removes an element from a map-like data structure based on the provided key.
|
|
211
235
|
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the
|
|
212
236
|
* data structure.
|
|
@@ -579,6 +603,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
579
603
|
}
|
|
580
604
|
|
|
581
605
|
/**
|
|
606
|
+
* Time Complexity: O(k)
|
|
607
|
+
* Space Complexity: O(k)
|
|
608
|
+
*
|
|
582
609
|
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
|
|
583
610
|
* using a provided function, and sets each key-value pair in the current object, returning an array
|
|
584
611
|
* of booleans indicating the success of each set operation.
|
|
@@ -605,6 +632,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
605
632
|
}
|
|
606
633
|
|
|
607
634
|
/**
|
|
635
|
+
* Time Complexity: O(1)
|
|
636
|
+
* Space Complexity: O(1)
|
|
637
|
+
*
|
|
608
638
|
* The function checks if a given key exists in a map, using different logic depending on whether the
|
|
609
639
|
* key is a weak key or not.
|
|
610
640
|
* @param {K} key - The `key` parameter is the key that is being checked for existence in the map.
|
|
@@ -19,9 +19,9 @@ import { IterableElementBase } from '../base';
|
|
|
19
19
|
* 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
|
|
20
20
|
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
21
21
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
22
|
-
* @example
|
|
23
|
-
* // Use Heap to sort an array
|
|
24
|
-
*
|
|
22
|
+
* @example
|
|
23
|
+
* // Use Heap to sort an array
|
|
24
|
+
* function heapSort(arr: number[]): number[] {
|
|
25
25
|
* const heap = new Heap<number>(arr, { comparator: (a, b) => a - b });
|
|
26
26
|
* const sorted: number[] = [];
|
|
27
27
|
* while (!heap.isEmpty()) {
|
|
@@ -29,12 +29,12 @@ import { IterableElementBase } from '../base';
|
|
|
29
29
|
* }
|
|
30
30
|
* return sorted;
|
|
31
31
|
* }
|
|
32
|
-
*
|
|
32
|
+
*
|
|
33
33
|
* const array = [5, 3, 8, 4, 1, 2];
|
|
34
34
|
* console.log(heapSort(array)); // [1, 2, 3, 4, 5, 8]
|
|
35
|
-
* @example
|
|
36
|
-
* // Use Heap to solve top k problems
|
|
37
|
-
*
|
|
35
|
+
* @example
|
|
36
|
+
* // Use Heap to solve top k problems
|
|
37
|
+
* function topKElements(arr: number[], k: number): number[] {
|
|
38
38
|
* const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
|
|
39
39
|
* arr.forEach(num => {
|
|
40
40
|
* heap.add(num);
|
|
@@ -42,28 +42,28 @@ import { IterableElementBase } from '../base';
|
|
|
42
42
|
* });
|
|
43
43
|
* return heap.toArray();
|
|
44
44
|
* }
|
|
45
|
-
*
|
|
45
|
+
*
|
|
46
46
|
* const numbers = [10, 30, 20, 5, 15, 25];
|
|
47
47
|
* console.log(topKElements(numbers, 3)); // [15, 10, 5]
|
|
48
|
-
* @example
|
|
49
|
-
* // Use Heap to merge sorted sequences
|
|
50
|
-
*
|
|
48
|
+
* @example
|
|
49
|
+
* // Use Heap to merge sorted sequences
|
|
50
|
+
* function mergeSortedSequences(sequences: number[][]): number[] {
|
|
51
51
|
* const heap = new Heap<{ value: number; seqIndex: number; itemIndex: number }>([], {
|
|
52
52
|
* comparator: (a, b) => a.value - b.value // Min heap
|
|
53
53
|
* });
|
|
54
|
-
*
|
|
54
|
+
*
|
|
55
55
|
* // Initialize heap
|
|
56
56
|
* sequences.forEach((seq, seqIndex) => {
|
|
57
57
|
* if (seq.length) {
|
|
58
58
|
* heap.add({ value: seq[0], seqIndex, itemIndex: 0 });
|
|
59
59
|
* }
|
|
60
60
|
* });
|
|
61
|
-
*
|
|
61
|
+
*
|
|
62
62
|
* const merged: number[] = [];
|
|
63
63
|
* while (!heap.isEmpty()) {
|
|
64
64
|
* const { value, seqIndex, itemIndex } = heap.poll()!;
|
|
65
65
|
* merged.push(value);
|
|
66
|
-
*
|
|
66
|
+
*
|
|
67
67
|
* if (itemIndex + 1 < sequences[seqIndex].length) {
|
|
68
68
|
* heap.add({
|
|
69
69
|
* value: sequences[seqIndex][itemIndex + 1],
|
|
@@ -72,42 +72,42 @@ import { IterableElementBase } from '../base';
|
|
|
72
72
|
* });
|
|
73
73
|
* }
|
|
74
74
|
* }
|
|
75
|
-
*
|
|
75
|
+
*
|
|
76
76
|
* return merged;
|
|
77
77
|
* }
|
|
78
|
-
*
|
|
78
|
+
*
|
|
79
79
|
* const sequences = [
|
|
80
80
|
* [1, 4, 7],
|
|
81
81
|
* [2, 5, 8],
|
|
82
82
|
* [3, 6, 9]
|
|
83
83
|
* ];
|
|
84
84
|
* console.log(mergeSortedSequences(sequences)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
85
|
-
* @example
|
|
86
|
-
* // Use Heap to dynamically maintain the median
|
|
87
|
-
*
|
|
85
|
+
* @example
|
|
86
|
+
* // Use Heap to dynamically maintain the median
|
|
87
|
+
* class MedianFinder {
|
|
88
88
|
* private low: MaxHeap<number>; // Max heap, stores the smaller half
|
|
89
89
|
* private high: MinHeap<number>; // Min heap, stores the larger half
|
|
90
|
-
*
|
|
90
|
+
*
|
|
91
91
|
* constructor() {
|
|
92
92
|
* this.low = new MaxHeap<number>([]);
|
|
93
93
|
* this.high = new MinHeap<number>([]);
|
|
94
94
|
* }
|
|
95
|
-
*
|
|
95
|
+
*
|
|
96
96
|
* addNum(num: number): void {
|
|
97
97
|
* if (this.low.isEmpty() || num <= this.low.peek()!) this.low.add(num);
|
|
98
98
|
* else this.high.add(num);
|
|
99
|
-
*
|
|
99
|
+
*
|
|
100
100
|
* // Balance heaps
|
|
101
101
|
* if (this.low.size > this.high.size + 1) this.high.add(this.low.poll()!);
|
|
102
102
|
* else if (this.high.size > this.low.size) this.low.add(this.high.poll()!);
|
|
103
103
|
* }
|
|
104
|
-
*
|
|
104
|
+
*
|
|
105
105
|
* findMedian(): number {
|
|
106
106
|
* if (this.low.size === this.high.size) return (this.low.peek()! + this.high.peek()!) / 2;
|
|
107
107
|
* return this.low.peek()!;
|
|
108
108
|
* }
|
|
109
109
|
* }
|
|
110
|
-
*
|
|
110
|
+
*
|
|
111
111
|
* const medianFinder = new MedianFinder();
|
|
112
112
|
* medianFinder.addNum(10);
|
|
113
113
|
* console.log(medianFinder.findMedian()); // 10
|
|
@@ -119,42 +119,42 @@ import { IterableElementBase } from '../base';
|
|
|
119
119
|
* console.log(medianFinder.findMedian()); // 25
|
|
120
120
|
* medianFinder.addNum(50);
|
|
121
121
|
* console.log(medianFinder.findMedian()); // 30
|
|
122
|
-
* @example
|
|
123
|
-
* // Use Heap for load balancing
|
|
124
|
-
*
|
|
122
|
+
* @example
|
|
123
|
+
* // Use Heap for load balancing
|
|
124
|
+
* function loadBalance(requests: number[], servers: number): number[] {
|
|
125
125
|
* const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap
|
|
126
126
|
* const serverLoads = new Array(servers).fill(0);
|
|
127
|
-
*
|
|
127
|
+
*
|
|
128
128
|
* for (let i = 0; i < servers; i++) {
|
|
129
129
|
* serverHeap.add({ id: i, load: 0 });
|
|
130
130
|
* }
|
|
131
|
-
*
|
|
131
|
+
*
|
|
132
132
|
* requests.forEach(req => {
|
|
133
133
|
* const server = serverHeap.poll()!;
|
|
134
134
|
* serverLoads[server.id] += req;
|
|
135
135
|
* server.load += req;
|
|
136
136
|
* serverHeap.add(server); // The server after updating the load is re-entered into the heap
|
|
137
137
|
* });
|
|
138
|
-
*
|
|
138
|
+
*
|
|
139
139
|
* return serverLoads;
|
|
140
140
|
* }
|
|
141
|
-
*
|
|
141
|
+
*
|
|
142
142
|
* const requests = [5, 2, 8, 3, 7];
|
|
143
143
|
* console.log(loadBalance(requests, 3)); // [12, 8, 5]
|
|
144
|
-
* @example
|
|
145
|
-
* // Use Heap to schedule tasks
|
|
146
|
-
*
|
|
147
|
-
*
|
|
144
|
+
* @example
|
|
145
|
+
* // Use Heap to schedule tasks
|
|
146
|
+
* type Task = [string, number];
|
|
147
|
+
*
|
|
148
148
|
* function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {
|
|
149
149
|
* const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap
|
|
150
150
|
* const allocation = new Map<number, Task[]>();
|
|
151
|
-
*
|
|
151
|
+
*
|
|
152
152
|
* // Initialize the load on each machine
|
|
153
153
|
* for (let i = 0; i < machines; i++) {
|
|
154
154
|
* machineHeap.add({ id: i, load: 0 });
|
|
155
155
|
* allocation.set(i, []);
|
|
156
156
|
* }
|
|
157
|
-
*
|
|
157
|
+
*
|
|
158
158
|
* // Assign tasks
|
|
159
159
|
* tasks.forEach(([task, load]) => {
|
|
160
160
|
* const machine = machineHeap.poll()!;
|
|
@@ -162,10 +162,10 @@ import { IterableElementBase } from '../base';
|
|
|
162
162
|
* machine.load += load;
|
|
163
163
|
* machineHeap.add(machine); // The machine after updating the load is re-entered into the heap
|
|
164
164
|
* });
|
|
165
|
-
*
|
|
165
|
+
*
|
|
166
166
|
* return allocation;
|
|
167
167
|
* }
|
|
168
|
-
*
|
|
168
|
+
*
|
|
169
169
|
* const tasks: Task[] = [
|
|
170
170
|
* ['Task1', 3],
|
|
171
171
|
* ['Task2', 1],
|
|
@@ -207,12 +207,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
|
|
|
207
207
|
if (comparator) this._comparator = comparator;
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
-
|
|
211
|
-
for (const el of elements) {
|
|
212
|
-
if (this.toElementFn) this.add(this.toElementFn(el as R));
|
|
213
|
-
else this.add(el as E);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
210
|
+
this.addMany(elements);
|
|
216
211
|
}
|
|
217
212
|
|
|
218
213
|
protected _elements: E[] = [];
|
|
@@ -254,14 +249,42 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
|
|
|
254
249
|
* Time Complexity: O(log n)
|
|
255
250
|
* Space Complexity: O(1)
|
|
256
251
|
*
|
|
257
|
-
*
|
|
258
|
-
* @param element - The element to
|
|
252
|
+
* The add function pushes an element into an array and then triggers a bubble-up operation.
|
|
253
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the
|
|
254
|
+
* data structure.
|
|
255
|
+
* @returns The `add` method is returning a boolean value, which is the result of calling the
|
|
256
|
+
* `_bubbleUp` method with the index `this.elements.length - 1` as an argument.
|
|
259
257
|
*/
|
|
260
258
|
add(element: E): boolean {
|
|
261
|
-
this._elements.push(element);
|
|
259
|
+
this._elements.push(element as E);
|
|
262
260
|
return this._bubbleUp(this.elements.length - 1);
|
|
263
261
|
}
|
|
264
262
|
|
|
263
|
+
/**
|
|
264
|
+
* Time Complexity: O(k log n)
|
|
265
|
+
* Space Complexity: O(1)
|
|
266
|
+
*
|
|
267
|
+
* The `addMany` function iterates over elements and adds them to a collection, returning an array of
|
|
268
|
+
* boolean values indicating success or failure.
|
|
269
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `addMany` method is
|
|
270
|
+
* an iterable containing elements of type `E` or `R`. The method iterates over each element in the
|
|
271
|
+
* iterable and adds them to the data structure. If a transformation function `_toElementFn` is
|
|
272
|
+
* provided, it transforms the element
|
|
273
|
+
* @returns The `addMany` method returns an array of boolean values indicating whether each element
|
|
274
|
+
* in the input iterable was successfully added to the data structure.
|
|
275
|
+
*/
|
|
276
|
+
addMany(elements: Iterable<E> | Iterable<R>): boolean[] {
|
|
277
|
+
const ans: boolean[] = [];
|
|
278
|
+
for (const el of elements) {
|
|
279
|
+
if (this._toElementFn) {
|
|
280
|
+
ans.push(this.add(this._toElementFn(el as R)));
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
ans.push(this.add(el as E));
|
|
284
|
+
}
|
|
285
|
+
return ans;
|
|
286
|
+
}
|
|
287
|
+
|
|
265
288
|
/**
|
|
266
289
|
* Time Complexity: O(log n)
|
|
267
290
|
* Space Complexity: O(1)
|
|
@@ -473,7 +496,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
|
|
|
473
496
|
}
|
|
474
497
|
|
|
475
498
|
/**
|
|
476
|
-
* Time Complexity: O(n
|
|
499
|
+
* Time Complexity: O(n)
|
|
477
500
|
* Space Complexity: O(n)
|
|
478
501
|
*
|
|
479
502
|
* The `map` function creates a new heap by applying a callback function to each element of the
|