min-heap-typed 1.49.0 → 1.49.2
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-base.d.ts +11 -0
- package/dist/data-structures/base/iterable-base.js +21 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +10 -12
- package/dist/data-structures/binary-tree/avl-tree.js +3 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +58 -58
- package/dist/data-structures/binary-tree/binary-tree.js +6 -6
- package/dist/data-structures/binary-tree/bst.d.ts +15 -15
- package/dist/data-structures/binary-tree/bst.js +3 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -11
- package/dist/data-structures/binary-tree/rb-tree.js +5 -6
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +14 -14
- package/dist/data-structures/binary-tree/tree-multimap.js +3 -3
- package/dist/data-structures/graph/abstract-graph.d.ts +9 -3
- package/dist/data-structures/graph/abstract-graph.js +27 -31
- package/dist/data-structures/graph/directed-graph.d.ts +8 -1
- package/dist/data-structures/graph/directed-graph.js +1 -8
- package/dist/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +8 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -8
- package/dist/data-structures/hash/hash-map.d.ts +22 -10
- package/dist/data-structures/hash/hash-map.js +28 -16
- package/dist/data-structures/hash/hash-table.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +20 -38
- package/dist/data-structures/heap/heap.js +22 -42
- package/dist/data-structures/heap/max-heap.d.ts +11 -1
- package/dist/data-structures/heap/max-heap.js +10 -7
- package/dist/data-structures/heap/min-heap.d.ts +11 -1
- package/dist/data-structures/heap/min-heap.js +10 -7
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +95 -95
- package/dist/data-structures/linked-list/doubly-linked-list.js +132 -136
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -23
- package/dist/data-structures/linked-list/singly-linked-list.js +42 -49
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/priority-queue.d.ts +9 -1
- package/dist/data-structures/priority-queue/priority-queue.js +8 -7
- package/dist/data-structures/queue/deque.d.ts +76 -80
- package/dist/data-structures/queue/deque.js +106 -122
- package/dist/data-structures/queue/queue.d.ts +30 -16
- package/dist/data-structures/queue/queue.js +31 -24
- package/dist/data-structures/stack/stack.d.ts +17 -8
- package/dist/data-structures/stack/stack.js +9 -9
- package/dist/data-structures/trie/trie.d.ts +14 -5
- package/dist/data-structures/trie/trie.js +13 -13
- package/dist/interfaces/binary-tree.d.ts +4 -4
- package/dist/types/common.d.ts +32 -8
- package/dist/types/common.js +22 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -22
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/binary-tree/avl-tree.ts +14 -14
- package/src/data-structures/binary-tree/binary-tree.ts +74 -77
- package/src/data-structures/binary-tree/bst.ts +18 -17
- package/src/data-structures/binary-tree/rb-tree.ts +17 -18
- package/src/data-structures/binary-tree/tree-multimap.ts +22 -20
- package/src/data-structures/graph/abstract-graph.ts +35 -25
- package/src/data-structures/graph/directed-graph.ts +2 -2
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +40 -24
- package/src/data-structures/hash/hash-table.ts +3 -3
- package/src/data-structures/heap/heap.ts +33 -60
- package/src/data-structures/heap/max-heap.ts +11 -2
- package/src/data-structures/heap/min-heap.ts +11 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +147 -145
- package/src/data-structures/linked-list/singly-linked-list.ts +52 -52
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +9 -2
- package/src/data-structures/queue/deque.ts +129 -144
- package/src/data-structures/queue/queue.ts +37 -26
- package/src/data-structures/stack/stack.ts +20 -14
- package/src/data-structures/trie/trie.ts +18 -13
- package/src/interfaces/binary-tree.ts +5 -5
- package/src/types/common.ts +37 -12
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
- package/src/types/data-structures/binary-tree/bst.ts +0 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
|
@@ -5,8 +5,14 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
9
|
-
import { IterableEntryBase } from
|
|
8
|
+
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
9
|
+
import { IterableEntryBase } from '../base';
|
|
10
|
+
/**
|
|
11
|
+
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
|
12
|
+
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key.
|
|
13
|
+
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
14
|
+
* 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time.
|
|
15
|
+
*/
|
|
10
16
|
export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
11
17
|
protected _store: {
|
|
12
18
|
[key: string]: HashMapStoreItem<K, V>;
|
|
@@ -36,13 +42,13 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
36
42
|
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
37
43
|
* key in the data structure.
|
|
38
44
|
*/
|
|
39
|
-
set(key: K, value: V):
|
|
45
|
+
set(key: K, value: V): boolean;
|
|
40
46
|
/**
|
|
41
47
|
* The function "setMany" sets multiple key-value pairs in a map.
|
|
42
48
|
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
|
|
43
49
|
* key-value pair is represented as an array with two elements: the key and the value.
|
|
44
50
|
*/
|
|
45
|
-
setMany(elements: Iterable<[K, V]>):
|
|
51
|
+
setMany(elements: Iterable<[K, V]>): boolean[];
|
|
46
52
|
/**
|
|
47
53
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
48
54
|
* a string map.
|
|
@@ -108,6 +114,7 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
108
114
|
*/
|
|
109
115
|
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
|
|
110
116
|
print(): void;
|
|
117
|
+
put(key: K, value: V): boolean;
|
|
111
118
|
/**
|
|
112
119
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
113
120
|
* object map.
|
|
@@ -117,6 +124,11 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
117
124
|
protected _isObjKey(key: any): key is (object | ((...args: any[]) => any));
|
|
118
125
|
protected _getNoObjKey(key: K): string;
|
|
119
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which elements are inserted. Therefore, when you traverse it, elements will be returned in the order they were inserted into the map.
|
|
129
|
+
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of elements through the linked list.
|
|
130
|
+
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
131
|
+
*/
|
|
120
132
|
export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
121
133
|
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>;
|
|
122
134
|
protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>;
|
|
@@ -167,9 +179,9 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
167
179
|
* value associated with the key being set in the data structure.
|
|
168
180
|
* @returns the size of the data structure after the key-value pair has been set.
|
|
169
181
|
*/
|
|
170
|
-
set(key: K, value?: V):
|
|
182
|
+
set(key: K, value?: V): boolean;
|
|
171
183
|
has(key: K): boolean;
|
|
172
|
-
setMany(entries: Iterable<[K, V]>):
|
|
184
|
+
setMany(entries: Iterable<[K, V]>): boolean[];
|
|
173
185
|
/**
|
|
174
186
|
* Time Complexity: O(1)
|
|
175
187
|
* Space Complexity: O(1)
|
|
@@ -195,7 +207,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
195
207
|
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
196
208
|
* where `K` is the key and `V` is the value.
|
|
197
209
|
*/
|
|
198
|
-
getAt(index: number):
|
|
210
|
+
getAt(index: number): V | undefined;
|
|
199
211
|
/**
|
|
200
212
|
* Time Complexity: O(1)
|
|
201
213
|
* Space Complexity: O(1)
|
|
@@ -216,7 +228,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
216
228
|
* deleted in the linked list.
|
|
217
229
|
* @returns The size of the list after deleting the element at the specified index.
|
|
218
230
|
*/
|
|
219
|
-
deleteAt(index: number):
|
|
231
|
+
deleteAt(index: number): boolean;
|
|
220
232
|
/**
|
|
221
233
|
* Time Complexity: O(1)
|
|
222
234
|
* Space Complexity: O(1)
|
|
@@ -276,7 +288,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
276
288
|
* function.
|
|
277
289
|
*/
|
|
278
290
|
map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
|
|
279
|
-
|
|
291
|
+
put(key: K, value: V): boolean;
|
|
280
292
|
/**
|
|
281
293
|
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
282
294
|
* Space Complexity: O(1)
|
|
@@ -294,5 +306,5 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
294
306
|
* represents a node in a linked list. It contains a key-value pair and references to the previous
|
|
295
307
|
* and next nodes in the list.
|
|
296
308
|
*/
|
|
297
|
-
protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>):
|
|
309
|
+
protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): boolean;
|
|
298
310
|
}
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* data-structure-typed
|
|
4
|
-
*
|
|
5
|
-
* @author Tyler Zeng
|
|
6
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
-
* @license MIT License
|
|
8
|
-
*/
|
|
9
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
3
|
exports.LinkedHashMap = exports.HashMap = void 0;
|
|
11
|
-
const utils_1 = require("../../utils");
|
|
12
4
|
const base_1 = require("../base");
|
|
5
|
+
const utils_1 = require("../../utils");
|
|
6
|
+
/**
|
|
7
|
+
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
|
8
|
+
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key.
|
|
9
|
+
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
10
|
+
* 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time.
|
|
11
|
+
*/
|
|
13
12
|
class HashMap extends base_1.IterableEntryBase {
|
|
14
13
|
/**
|
|
15
14
|
* The constructor function initializes a new instance of a class with optional elements and options.
|
|
@@ -69,6 +68,7 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
69
68
|
}
|
|
70
69
|
this._store[strKey] = { key, value };
|
|
71
70
|
}
|
|
71
|
+
return true;
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
74
74
|
* The function "setMany" sets multiple key-value pairs in a map.
|
|
@@ -76,8 +76,10 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
76
76
|
* key-value pair is represented as an array with two elements: the key and the value.
|
|
77
77
|
*/
|
|
78
78
|
setMany(elements) {
|
|
79
|
+
const results = [];
|
|
79
80
|
for (const [key, value] of elements)
|
|
80
|
-
this.set(key, value);
|
|
81
|
+
results.push(this.set(key, value));
|
|
82
|
+
return results;
|
|
81
83
|
}
|
|
82
84
|
/**
|
|
83
85
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
@@ -195,6 +197,9 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
195
197
|
print() {
|
|
196
198
|
console.log([...this.entries()]);
|
|
197
199
|
}
|
|
200
|
+
put(key, value) {
|
|
201
|
+
return this.set(key, value);
|
|
202
|
+
}
|
|
198
203
|
/**
|
|
199
204
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
200
205
|
* object map.
|
|
@@ -230,6 +235,11 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
230
235
|
}
|
|
231
236
|
}
|
|
232
237
|
exports.HashMap = HashMap;
|
|
238
|
+
/**
|
|
239
|
+
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which elements are inserted. Therefore, when you traverse it, elements will be returned in the order they were inserted into the map.
|
|
240
|
+
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of elements through the linked list.
|
|
241
|
+
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
242
|
+
*/
|
|
233
243
|
class LinkedHashMap extends base_1.IterableEntryBase {
|
|
234
244
|
constructor(elements, options = {
|
|
235
245
|
hashFn: (key) => String(key),
|
|
@@ -353,7 +363,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
353
363
|
this._sentinel.prev = node;
|
|
354
364
|
this._size++;
|
|
355
365
|
}
|
|
356
|
-
return
|
|
366
|
+
return true;
|
|
357
367
|
}
|
|
358
368
|
has(key) {
|
|
359
369
|
if ((0, utils_1.isWeakKey)(key)) {
|
|
@@ -366,10 +376,12 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
366
376
|
}
|
|
367
377
|
}
|
|
368
378
|
setMany(entries) {
|
|
379
|
+
const results = [];
|
|
369
380
|
for (const entry of entries) {
|
|
370
381
|
const [key, value] = entry;
|
|
371
|
-
this.set(key, value);
|
|
382
|
+
results.push(this.set(key, value));
|
|
372
383
|
}
|
|
384
|
+
return results;
|
|
373
385
|
}
|
|
374
386
|
/**
|
|
375
387
|
* Time Complexity: O(1)
|
|
@@ -413,7 +425,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
413
425
|
while (index--) {
|
|
414
426
|
node = node.next;
|
|
415
427
|
}
|
|
416
|
-
return
|
|
428
|
+
return node.value;
|
|
417
429
|
}
|
|
418
430
|
/**
|
|
419
431
|
* Time Complexity: O(1)
|
|
@@ -466,8 +478,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
466
478
|
while (index--) {
|
|
467
479
|
node = node.next;
|
|
468
480
|
}
|
|
469
|
-
this._deleteNode(node);
|
|
470
|
-
return this._size;
|
|
481
|
+
return this._deleteNode(node);
|
|
471
482
|
}
|
|
472
483
|
/**
|
|
473
484
|
* Time Complexity: O(1)
|
|
@@ -560,8 +571,8 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
560
571
|
}
|
|
561
572
|
return mappedMap;
|
|
562
573
|
}
|
|
563
|
-
|
|
564
|
-
|
|
574
|
+
put(key, value) {
|
|
575
|
+
return this.set(key, value);
|
|
565
576
|
}
|
|
566
577
|
/**
|
|
567
578
|
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
@@ -597,6 +608,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
597
608
|
this._tail = prev;
|
|
598
609
|
}
|
|
599
610
|
this._size -= 1;
|
|
611
|
+
return true;
|
|
600
612
|
}
|
|
601
613
|
}
|
|
602
614
|
exports.LinkedHashMap = LinkedHashMap;
|
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
import type { HashFunction } from '../../types';
|
|
9
|
+
export declare class HashTableNode<K = any, V = any> {
|
|
9
10
|
key: K;
|
|
10
11
|
value: V;
|
|
11
12
|
next: HashTableNode<K, V> | undefined;
|
|
12
13
|
constructor(key: K, value: V);
|
|
13
14
|
}
|
|
14
|
-
import { HashFunction } from '../../types';
|
|
15
15
|
export declare class HashTable<K = any, V = any> {
|
|
16
16
|
protected static readonly DEFAULT_CAPACITY = 16;
|
|
17
17
|
protected static readonly LOAD_FACTOR = 0.75;
|
|
@@ -4,9 +4,20 @@
|
|
|
4
4
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
5
5
|
* @license MIT License
|
|
6
6
|
*/
|
|
7
|
-
import type { Comparator, DFSOrderPattern, ElementCallback } from '../../types';
|
|
8
|
-
import {
|
|
9
|
-
|
|
7
|
+
import type { Comparator, DFSOrderPattern, ElementCallback, HeapOptions } from '../../types';
|
|
8
|
+
import { IterableElementBase } from '../base';
|
|
9
|
+
/**
|
|
10
|
+
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
|
11
|
+
* 2. Heap Properties: Each node in a heap follows a specific order property, which varies depending on the type of heap:
|
|
12
|
+
* Max Heap: The value of each parent node is greater than or equal to the value of its children.
|
|
13
|
+
* Min Heap: The value of each parent node is less than or equal to the value of its children.
|
|
14
|
+
* 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
|
|
15
|
+
* 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
|
16
|
+
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
|
17
|
+
* 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.
|
|
18
|
+
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
19
|
+
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
20
|
+
*/
|
|
10
21
|
export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
11
22
|
options: HeapOptions<E>;
|
|
12
23
|
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
@@ -41,19 +52,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
41
52
|
* Insert an element into the heap and maintain the heap properties.
|
|
42
53
|
* @param element - The element to be inserted.
|
|
43
54
|
*/
|
|
44
|
-
add(element: E):
|
|
45
|
-
/**
|
|
46
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
47
|
-
* Space Complexity: O(1)
|
|
48
|
-
*/
|
|
49
|
-
/**
|
|
50
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
51
|
-
* Space Complexity: O(1)
|
|
52
|
-
*
|
|
53
|
-
* Insert an element into the heap and maintain the heap properties.
|
|
54
|
-
* @param element - The element to be inserted.
|
|
55
|
-
*/
|
|
56
|
-
push(element: E): Heap<E>;
|
|
55
|
+
add(element: E): boolean;
|
|
57
56
|
/**
|
|
58
57
|
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
59
58
|
* Space Complexity: O(1)
|
|
@@ -66,18 +65,6 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
66
65
|
* @returns The top element or undefined if the heap is empty.
|
|
67
66
|
*/
|
|
68
67
|
poll(): E | undefined;
|
|
69
|
-
/**
|
|
70
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
71
|
-
* Space Complexity: O(1)
|
|
72
|
-
*/
|
|
73
|
-
/**
|
|
74
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
75
|
-
* Space Complexity: O(1)
|
|
76
|
-
*
|
|
77
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
78
|
-
* @returns The top element or undefined if the heap is empty.
|
|
79
|
-
*/
|
|
80
|
-
pop(): E | undefined;
|
|
81
68
|
/**
|
|
82
69
|
* Peek at the top element of the heap without removing it.
|
|
83
70
|
* @returns The top element or undefined if the heap is empty.
|
|
@@ -103,7 +90,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
103
90
|
* Clear and add elements of the heap
|
|
104
91
|
* @param elements
|
|
105
92
|
*/
|
|
106
|
-
refill(elements: E[]):
|
|
93
|
+
refill(elements: E[]): boolean[];
|
|
107
94
|
/**
|
|
108
95
|
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
109
96
|
* Space Complexity: O(1)
|
|
@@ -192,7 +179,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
192
179
|
*
|
|
193
180
|
* Fix the entire heap to maintain heap properties.
|
|
194
181
|
*/
|
|
195
|
-
fix():
|
|
182
|
+
fix(): boolean[];
|
|
196
183
|
/**
|
|
197
184
|
* Time Complexity: O(n)
|
|
198
185
|
* Space Complexity: O(n)
|
|
@@ -239,12 +226,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
239
226
|
* original Heap.
|
|
240
227
|
*/
|
|
241
228
|
map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T>;
|
|
242
|
-
|
|
243
|
-
* Time Complexity: O(log n)
|
|
244
|
-
* Space Complexity: O(1)
|
|
245
|
-
*/
|
|
246
|
-
print(): void;
|
|
247
|
-
protected _getIterator(): Generator<E, void, unknown>;
|
|
229
|
+
protected _getIterator(): IterableIterator<E>;
|
|
248
230
|
/**
|
|
249
231
|
* Time Complexity: O(n)
|
|
250
232
|
* Space Complexity: O(1)
|
|
@@ -256,7 +238,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
256
238
|
* Float operation to maintain heap properties after adding an element.
|
|
257
239
|
* @param index - The index of the newly added element.
|
|
258
240
|
*/
|
|
259
|
-
protected _bubbleUp(index: number):
|
|
241
|
+
protected _bubbleUp(index: number): boolean;
|
|
260
242
|
/**
|
|
261
243
|
* Time Complexity: O(log n)
|
|
262
244
|
* Space Complexity: O(1)
|
|
@@ -265,7 +247,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
265
247
|
* @param index - The index from which to start sinking.
|
|
266
248
|
* @param halfLength
|
|
267
249
|
*/
|
|
268
|
-
protected _sinkDown(index: number, halfLength: number):
|
|
250
|
+
protected _sinkDown(index: number, halfLength: number): boolean;
|
|
269
251
|
}
|
|
270
252
|
export declare class FibonacciHeapNode<E> {
|
|
271
253
|
element: E;
|
|
@@ -8,6 +8,18 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
|
|
10
10
|
const base_1 = require("../base");
|
|
11
|
+
/**
|
|
12
|
+
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
|
13
|
+
* 2. Heap Properties: Each node in a heap follows a specific order property, which varies depending on the type of heap:
|
|
14
|
+
* Max Heap: The value of each parent node is greater than or equal to the value of its children.
|
|
15
|
+
* Min Heap: The value of each parent node is less than or equal to the value of its children.
|
|
16
|
+
* 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
|
|
17
|
+
* 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
|
18
|
+
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
|
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
|
+
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
21
|
+
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
22
|
+
*/
|
|
11
23
|
class Heap extends base_1.IterableElementBase {
|
|
12
24
|
constructor(elements, options) {
|
|
13
25
|
super();
|
|
@@ -30,7 +42,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
30
42
|
}
|
|
31
43
|
if (elements) {
|
|
32
44
|
for (const el of elements) {
|
|
33
|
-
this.
|
|
45
|
+
this.add(el);
|
|
34
46
|
}
|
|
35
47
|
// this.fix();
|
|
36
48
|
}
|
|
@@ -73,23 +85,8 @@ class Heap extends base_1.IterableElementBase {
|
|
|
73
85
|
* @param element - The element to be inserted.
|
|
74
86
|
*/
|
|
75
87
|
add(element) {
|
|
76
|
-
return this.push(element);
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
80
|
-
* Space Complexity: O(1)
|
|
81
|
-
*/
|
|
82
|
-
/**
|
|
83
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
84
|
-
* Space Complexity: O(1)
|
|
85
|
-
*
|
|
86
|
-
* Insert an element into the heap and maintain the heap properties.
|
|
87
|
-
* @param element - The element to be inserted.
|
|
88
|
-
*/
|
|
89
|
-
push(element) {
|
|
90
88
|
this._elements.push(element);
|
|
91
|
-
this._bubbleUp(this.elements.length - 1);
|
|
92
|
-
return this;
|
|
89
|
+
return this._bubbleUp(this.elements.length - 1);
|
|
93
90
|
}
|
|
94
91
|
/**
|
|
95
92
|
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
@@ -113,20 +110,6 @@ class Heap extends base_1.IterableElementBase {
|
|
|
113
110
|
}
|
|
114
111
|
return value;
|
|
115
112
|
}
|
|
116
|
-
/**
|
|
117
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
118
|
-
* Space Complexity: O(1)
|
|
119
|
-
*/
|
|
120
|
-
/**
|
|
121
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
122
|
-
* Space Complexity: O(1)
|
|
123
|
-
*
|
|
124
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
125
|
-
* @returns The top element or undefined if the heap is empty.
|
|
126
|
-
*/
|
|
127
|
-
pop() {
|
|
128
|
-
return this.poll();
|
|
129
|
-
}
|
|
130
113
|
/**
|
|
131
114
|
* Peek at the top element of the heap without removing it.
|
|
132
115
|
* @returns The top element or undefined if the heap is empty.
|
|
@@ -160,7 +143,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
160
143
|
*/
|
|
161
144
|
refill(elements) {
|
|
162
145
|
this._elements = elements;
|
|
163
|
-
this.fix();
|
|
146
|
+
return this.fix();
|
|
164
147
|
}
|
|
165
148
|
/**
|
|
166
149
|
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
@@ -197,7 +180,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
197
180
|
if (index < 0)
|
|
198
181
|
return false;
|
|
199
182
|
if (index === 0) {
|
|
200
|
-
this.
|
|
183
|
+
this.poll();
|
|
201
184
|
}
|
|
202
185
|
else if (index === this.elements.length - 1) {
|
|
203
186
|
this.elements.pop();
|
|
@@ -309,8 +292,10 @@ class Heap extends base_1.IterableElementBase {
|
|
|
309
292
|
* Fix the entire heap to maintain heap properties.
|
|
310
293
|
*/
|
|
311
294
|
fix() {
|
|
295
|
+
const results = [];
|
|
312
296
|
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
313
|
-
this._sinkDown(i, this.elements.length >> 1);
|
|
297
|
+
results.push(this._sinkDown(i, this.elements.length >> 1));
|
|
298
|
+
return results;
|
|
314
299
|
}
|
|
315
300
|
/**
|
|
316
301
|
* Time Complexity: O(n)
|
|
@@ -337,7 +322,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
337
322
|
let index = 0;
|
|
338
323
|
for (const current of this) {
|
|
339
324
|
if (callback.call(thisArg, current, index, this)) {
|
|
340
|
-
filteredList.
|
|
325
|
+
filteredList.add(current);
|
|
341
326
|
}
|
|
342
327
|
index++;
|
|
343
328
|
}
|
|
@@ -376,13 +361,6 @@ class Heap extends base_1.IterableElementBase {
|
|
|
376
361
|
}
|
|
377
362
|
return mappedHeap;
|
|
378
363
|
}
|
|
379
|
-
/**
|
|
380
|
-
* Time Complexity: O(log n)
|
|
381
|
-
* Space Complexity: O(1)
|
|
382
|
-
*/
|
|
383
|
-
print() {
|
|
384
|
-
console.log([...this]);
|
|
385
|
-
}
|
|
386
364
|
*_getIterator() {
|
|
387
365
|
for (const element of this.elements) {
|
|
388
366
|
yield element;
|
|
@@ -410,6 +388,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
410
388
|
index = parent;
|
|
411
389
|
}
|
|
412
390
|
this.elements[index] = element;
|
|
391
|
+
return true;
|
|
413
392
|
}
|
|
414
393
|
/**
|
|
415
394
|
* Time Complexity: O(log n)
|
|
@@ -436,6 +415,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
436
415
|
index = left;
|
|
437
416
|
}
|
|
438
417
|
this.elements[index] = element;
|
|
418
|
+
return true;
|
|
439
419
|
}
|
|
440
420
|
}
|
|
441
421
|
exports.Heap = Heap;
|
|
@@ -5,8 +5,18 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { Heap } from './heap';
|
|
9
8
|
import type { HeapOptions } from '../../types';
|
|
9
|
+
import { Heap } from './heap';
|
|
10
|
+
/**
|
|
11
|
+
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
|
12
|
+
* 2. Heap Properties: The value of each parent node is greater than or equal to the value of its children.
|
|
13
|
+
* 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
|
|
14
|
+
* 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
|
15
|
+
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
|
16
|
+
* 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.
|
|
17
|
+
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
18
|
+
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
19
|
+
*/
|
|
10
20
|
export declare class MaxHeap<E = any> extends Heap<E> {
|
|
11
21
|
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
12
22
|
}
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* data-structure-typed
|
|
4
|
-
*
|
|
5
|
-
* @author Kirk Qi
|
|
6
|
-
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
7
|
-
* @license MIT License
|
|
8
|
-
*/
|
|
9
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
3
|
exports.MaxHeap = void 0;
|
|
11
4
|
const heap_1 = require("./heap");
|
|
5
|
+
/**
|
|
6
|
+
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
|
7
|
+
* 2. Heap Properties: The value of each parent node is greater than or equal to the value of its children.
|
|
8
|
+
* 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
|
|
9
|
+
* 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
|
10
|
+
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
|
11
|
+
* 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.
|
|
12
|
+
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
13
|
+
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
14
|
+
*/
|
|
12
15
|
class MaxHeap extends heap_1.Heap {
|
|
13
16
|
constructor(elements, options = {
|
|
14
17
|
comparator: (a, b) => {
|
|
@@ -5,8 +5,18 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { Heap } from './heap';
|
|
9
8
|
import type { HeapOptions } from '../../types';
|
|
9
|
+
import { Heap } from './heap';
|
|
10
|
+
/**
|
|
11
|
+
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
|
12
|
+
* 2. Heap Properties: The value of each parent node is less than or equal to the value of its children.
|
|
13
|
+
* 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
|
|
14
|
+
* 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
|
15
|
+
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
|
16
|
+
* 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.
|
|
17
|
+
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
18
|
+
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
19
|
+
*/
|
|
10
20
|
export declare class MinHeap<E = any> extends Heap<E> {
|
|
11
21
|
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
12
22
|
}
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* data-structure-typed
|
|
4
|
-
*
|
|
5
|
-
* @author Kirk Qi
|
|
6
|
-
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
7
|
-
* @license MIT License
|
|
8
|
-
*/
|
|
9
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
3
|
exports.MinHeap = void 0;
|
|
11
4
|
const heap_1 = require("./heap");
|
|
5
|
+
/**
|
|
6
|
+
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
|
7
|
+
* 2. Heap Properties: The value of each parent node is less than or equal to the value of its children.
|
|
8
|
+
* 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
|
|
9
|
+
* 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
|
10
|
+
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
|
11
|
+
* 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.
|
|
12
|
+
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
13
|
+
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
14
|
+
*/
|
|
12
15
|
class MinHeap extends heap_1.Heap {
|
|
13
16
|
constructor(elements, options = {
|
|
14
17
|
comparator: (a, b) => {
|