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,11 +5,16 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
9
|
+
import { IterableEntryBase } from '../base';
|
|
9
10
|
import { isWeakKey, rangeCheck } from '../../utils';
|
|
10
|
-
import { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
11
|
-
import { IterableEntryBase } from "../base";
|
|
12
11
|
|
|
12
|
+
/**
|
|
13
|
+
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
|
14
|
+
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key.
|
|
15
|
+
* 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.
|
|
16
|
+
* 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time.
|
|
17
|
+
*/
|
|
13
18
|
export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
14
19
|
protected _store: { [key: string]: HashMapStoreItem<K, V> } = {};
|
|
15
20
|
protected _objMap: Map<object, V> = new Map();
|
|
@@ -30,7 +35,6 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
30
35
|
const { hashFn } = options;
|
|
31
36
|
if (hashFn) {
|
|
32
37
|
this._hashFn = hashFn;
|
|
33
|
-
|
|
34
38
|
}
|
|
35
39
|
}
|
|
36
40
|
if (elements) {
|
|
@@ -63,8 +67,7 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
63
67
|
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
64
68
|
* key in the data structure.
|
|
65
69
|
*/
|
|
66
|
-
set(key: K, value: V) {
|
|
67
|
-
|
|
70
|
+
set(key: K, value: V): boolean {
|
|
68
71
|
if (this._isObjKey(key)) {
|
|
69
72
|
if (!this._objMap.has(key)) {
|
|
70
73
|
this._size++;
|
|
@@ -78,6 +81,7 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
78
81
|
}
|
|
79
82
|
this._store[strKey] = { key, value };
|
|
80
83
|
}
|
|
84
|
+
return true;
|
|
81
85
|
}
|
|
82
86
|
|
|
83
87
|
/**
|
|
@@ -85,8 +89,10 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
85
89
|
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
|
|
86
90
|
* key-value pair is represented as an array with two elements: the key and the value.
|
|
87
91
|
*/
|
|
88
|
-
setMany(elements: Iterable<[K, V]>) {
|
|
89
|
-
|
|
92
|
+
setMany(elements: Iterable<[K, V]>): boolean[] {
|
|
93
|
+
const results: boolean[] = [];
|
|
94
|
+
for (const [key, value] of elements) results.push(this.set(key, value));
|
|
95
|
+
return results;
|
|
90
96
|
}
|
|
91
97
|
|
|
92
98
|
/**
|
|
@@ -210,6 +216,10 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
210
216
|
console.log([...this.entries()]);
|
|
211
217
|
}
|
|
212
218
|
|
|
219
|
+
put(key: K, value: V): boolean {
|
|
220
|
+
return this.set(key, value);
|
|
221
|
+
}
|
|
222
|
+
|
|
213
223
|
/**
|
|
214
224
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
215
225
|
* object map.
|
|
@@ -248,6 +258,11 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
248
258
|
}
|
|
249
259
|
}
|
|
250
260
|
|
|
261
|
+
/**
|
|
262
|
+
* 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.
|
|
263
|
+
* 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.
|
|
264
|
+
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
265
|
+
*/
|
|
251
266
|
export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
252
267
|
|
|
253
268
|
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {};
|
|
@@ -276,7 +291,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
276
291
|
this.set(el[0], el[1]);
|
|
277
292
|
}
|
|
278
293
|
}
|
|
279
|
-
|
|
280
294
|
}
|
|
281
295
|
|
|
282
296
|
protected _size = 0;
|
|
@@ -346,7 +360,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
346
360
|
* value associated with the key being set in the data structure.
|
|
347
361
|
* @returns the size of the data structure after the key-value pair has been set.
|
|
348
362
|
*/
|
|
349
|
-
set(key: K, value?: V) {
|
|
363
|
+
set(key: K, value?: V): boolean {
|
|
350
364
|
let node;
|
|
351
365
|
const isNewKey = !this.has(key); // Check if the key is new
|
|
352
366
|
|
|
@@ -388,7 +402,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
388
402
|
this._size++;
|
|
389
403
|
}
|
|
390
404
|
|
|
391
|
-
return
|
|
405
|
+
return true;
|
|
392
406
|
}
|
|
393
407
|
|
|
394
408
|
has(key: K): boolean {
|
|
@@ -401,11 +415,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
401
415
|
}
|
|
402
416
|
}
|
|
403
417
|
|
|
404
|
-
setMany(entries: Iterable<[K, V]>):
|
|
418
|
+
setMany(entries: Iterable<[K, V]>): boolean[] {
|
|
419
|
+
const results: boolean[] = [];
|
|
405
420
|
for (const entry of entries) {
|
|
406
421
|
const [key, value] = entry;
|
|
407
|
-
this.set(key, value);
|
|
422
|
+
results.push(this.set(key, value));
|
|
408
423
|
}
|
|
424
|
+
return results;
|
|
409
425
|
}
|
|
410
426
|
|
|
411
427
|
/**
|
|
@@ -444,13 +460,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
444
460
|
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
445
461
|
* where `K` is the key and `V` is the value.
|
|
446
462
|
*/
|
|
447
|
-
getAt(index: number) {
|
|
463
|
+
getAt(index: number): V | undefined {
|
|
448
464
|
rangeCheck(index, 0, this._size - 1);
|
|
449
465
|
let node = this._head;
|
|
450
466
|
while (index--) {
|
|
451
467
|
node = node.next;
|
|
452
468
|
}
|
|
453
|
-
return
|
|
469
|
+
return node.value;
|
|
454
470
|
}
|
|
455
471
|
|
|
456
472
|
/**
|
|
@@ -463,7 +479,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
463
479
|
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
464
480
|
* was not found.
|
|
465
481
|
*/
|
|
466
|
-
delete(key: K) {
|
|
482
|
+
delete(key: K): boolean {
|
|
467
483
|
let node;
|
|
468
484
|
|
|
469
485
|
if (isWeakKey(key)) {
|
|
@@ -504,14 +520,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
504
520
|
* deleted in the linked list.
|
|
505
521
|
* @returns The size of the list after deleting the element at the specified index.
|
|
506
522
|
*/
|
|
507
|
-
deleteAt(index: number) {
|
|
523
|
+
deleteAt(index: number): boolean {
|
|
508
524
|
rangeCheck(index, 0, this._size - 1);
|
|
509
525
|
let node = this._head;
|
|
510
526
|
while (index--) {
|
|
511
527
|
node = node.next;
|
|
512
528
|
}
|
|
513
|
-
this._deleteNode(node);
|
|
514
|
-
return this._size;
|
|
529
|
+
return this._deleteNode(node);
|
|
515
530
|
}
|
|
516
531
|
|
|
517
532
|
/**
|
|
@@ -522,7 +537,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
522
537
|
* @returns The method is returning a boolean value indicating whether the size of the object is 0 or
|
|
523
538
|
* not.
|
|
524
539
|
*/
|
|
525
|
-
isEmpty() {
|
|
540
|
+
isEmpty(): boolean {
|
|
526
541
|
return this._size === 0;
|
|
527
542
|
}
|
|
528
543
|
|
|
@@ -532,7 +547,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
532
547
|
*
|
|
533
548
|
* The `clear` function clears all the elements in a data structure and resets its properties.
|
|
534
549
|
*/
|
|
535
|
-
clear() {
|
|
550
|
+
clear(): void {
|
|
536
551
|
this._noObjMap = {};
|
|
537
552
|
this._size = 0;
|
|
538
553
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
@@ -612,8 +627,8 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
612
627
|
return mappedMap;
|
|
613
628
|
}
|
|
614
629
|
|
|
615
|
-
|
|
616
|
-
|
|
630
|
+
put(key: K, value: V): boolean {
|
|
631
|
+
return this.set(key, value);
|
|
617
632
|
}
|
|
618
633
|
|
|
619
634
|
/**
|
|
@@ -640,7 +655,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
640
655
|
* represents a node in a linked list. It contains a key-value pair and references to the previous
|
|
641
656
|
* and next nodes in the list.
|
|
642
657
|
*/
|
|
643
|
-
protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>) {
|
|
658
|
+
protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): boolean {
|
|
644
659
|
const { prev, next } = node;
|
|
645
660
|
prev.next = next;
|
|
646
661
|
next.prev = prev;
|
|
@@ -654,5 +669,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
654
669
|
}
|
|
655
670
|
|
|
656
671
|
this._size -= 1;
|
|
672
|
+
return true;
|
|
657
673
|
}
|
|
658
674
|
}
|
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
import type { HashFunction } from '../../types';
|
|
10
|
+
|
|
11
|
+
export class HashTableNode<K = any, V = any> {
|
|
10
12
|
key: K;
|
|
11
13
|
value: V;
|
|
12
14
|
next: HashTableNode<K, V> | undefined;
|
|
@@ -18,8 +20,6 @@ export class HashTableNode<K, V> {
|
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
import { HashFunction } from '../../types';
|
|
22
|
-
|
|
23
23
|
export class HashTable<K = any, V = any> {
|
|
24
24
|
protected static readonly DEFAULT_CAPACITY = 16;
|
|
25
25
|
protected static readonly LOAD_FACTOR = 0.75;
|
|
@@ -5,10 +5,21 @@
|
|
|
5
5
|
* @license MIT License
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type { Comparator, DFSOrderPattern, ElementCallback } from '../../types';
|
|
9
|
-
import {
|
|
10
|
-
import { IterableElementBase } from "../base";
|
|
8
|
+
import type { Comparator, DFSOrderPattern, ElementCallback, HeapOptions } from '../../types';
|
|
9
|
+
import { IterableElementBase } from '../base';
|
|
11
10
|
|
|
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
|
+
*/
|
|
12
23
|
export class Heap<E = any> extends IterableElementBase<E> {
|
|
13
24
|
options: HeapOptions<E>;
|
|
14
25
|
|
|
@@ -31,7 +42,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
31
42
|
|
|
32
43
|
if (elements) {
|
|
33
44
|
for (const el of elements) {
|
|
34
|
-
this.
|
|
45
|
+
this.add(el);
|
|
35
46
|
}
|
|
36
47
|
// this.fix();
|
|
37
48
|
}
|
|
@@ -80,26 +91,9 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
80
91
|
* Insert an element into the heap and maintain the heap properties.
|
|
81
92
|
* @param element - The element to be inserted.
|
|
82
93
|
*/
|
|
83
|
-
add(element: E):
|
|
84
|
-
return this.push(element);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
89
|
-
* Space Complexity: O(1)
|
|
90
|
-
*/
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
94
|
-
* Space Complexity: O(1)
|
|
95
|
-
*
|
|
96
|
-
* Insert an element into the heap and maintain the heap properties.
|
|
97
|
-
* @param element - The element to be inserted.
|
|
98
|
-
*/
|
|
99
|
-
push(element: E): Heap<E> {
|
|
94
|
+
add(element: E): boolean {
|
|
100
95
|
this._elements.push(element);
|
|
101
|
-
this._bubbleUp(this.elements.length - 1);
|
|
102
|
-
return this;
|
|
96
|
+
return this._bubbleUp(this.elements.length - 1);
|
|
103
97
|
}
|
|
104
98
|
|
|
105
99
|
/**
|
|
@@ -125,22 +119,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
125
119
|
return value;
|
|
126
120
|
}
|
|
127
121
|
|
|
128
|
-
/**
|
|
129
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
130
|
-
* Space Complexity: O(1)
|
|
131
|
-
*/
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
135
|
-
* Space Complexity: O(1)
|
|
136
|
-
*
|
|
137
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
138
|
-
* @returns The top element or undefined if the heap is empty.
|
|
139
|
-
*/
|
|
140
|
-
pop(): E | undefined {
|
|
141
|
-
return this.poll();
|
|
142
|
-
}
|
|
143
|
-
|
|
144
122
|
/**
|
|
145
123
|
* Peek at the top element of the heap without removing it.
|
|
146
124
|
* @returns The top element or undefined if the heap is empty.
|
|
@@ -153,14 +131,14 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
153
131
|
* Check if the heap is empty.
|
|
154
132
|
* @returns True if the heap is empty, otherwise false.
|
|
155
133
|
*/
|
|
156
|
-
isEmpty() {
|
|
134
|
+
isEmpty(): boolean {
|
|
157
135
|
return this.size === 0;
|
|
158
136
|
}
|
|
159
137
|
|
|
160
138
|
/**
|
|
161
139
|
* Reset the elements of the heap. Make the elements empty.
|
|
162
140
|
*/
|
|
163
|
-
clear() {
|
|
141
|
+
clear(): void {
|
|
164
142
|
this._elements = [];
|
|
165
143
|
}
|
|
166
144
|
|
|
@@ -176,9 +154,9 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
176
154
|
* Clear and add elements of the heap
|
|
177
155
|
* @param elements
|
|
178
156
|
*/
|
|
179
|
-
refill(elements: E[]) {
|
|
157
|
+
refill(elements: E[]): boolean[] {
|
|
180
158
|
this._elements = elements;
|
|
181
|
-
this.fix();
|
|
159
|
+
return this.fix();
|
|
182
160
|
}
|
|
183
161
|
|
|
184
162
|
/**
|
|
@@ -214,11 +192,11 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
214
192
|
* @returns The `delete` function is returning a boolean value. It returns `true` if the element was
|
|
215
193
|
* successfully deleted from the array, and `false` if the element was not found in the array.
|
|
216
194
|
*/
|
|
217
|
-
delete(element: E) {
|
|
195
|
+
delete(element: E): boolean {
|
|
218
196
|
const index = this.elements.indexOf(element);
|
|
219
197
|
if (index < 0) return false;
|
|
220
198
|
if (index === 0) {
|
|
221
|
-
this.
|
|
199
|
+
this.poll();
|
|
222
200
|
} else if (index === this.elements.length - 1) {
|
|
223
201
|
this.elements.pop();
|
|
224
202
|
} else {
|
|
@@ -337,8 +315,10 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
337
315
|
*
|
|
338
316
|
* Fix the entire heap to maintain heap properties.
|
|
339
317
|
*/
|
|
340
|
-
fix() {
|
|
341
|
-
|
|
318
|
+
fix(): boolean[] {
|
|
319
|
+
const results: boolean[] = [];
|
|
320
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--) results.push(this._sinkDown(i, this.elements.length >> 1));
|
|
321
|
+
return results;
|
|
342
322
|
}
|
|
343
323
|
|
|
344
324
|
/**
|
|
@@ -367,7 +347,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
367
347
|
let index = 0;
|
|
368
348
|
for (const current of this) {
|
|
369
349
|
if (callback.call(thisArg, current, index, this)) {
|
|
370
|
-
filteredList.
|
|
350
|
+
filteredList.add(current);
|
|
371
351
|
}
|
|
372
352
|
index++;
|
|
373
353
|
}
|
|
@@ -410,16 +390,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
410
390
|
return mappedHeap;
|
|
411
391
|
}
|
|
412
392
|
|
|
413
|
-
|
|
414
|
-
* Time Complexity: O(log n)
|
|
415
|
-
* Space Complexity: O(1)
|
|
416
|
-
*/
|
|
417
|
-
|
|
418
|
-
print(): void {
|
|
419
|
-
console.log([...this]);
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
protected* _getIterator() {
|
|
393
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
423
394
|
for (const element of this.elements) {
|
|
424
395
|
yield element;
|
|
425
396
|
}
|
|
@@ -437,7 +408,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
437
408
|
* Float operation to maintain heap properties after adding an element.
|
|
438
409
|
* @param index - The index of the newly added element.
|
|
439
410
|
*/
|
|
440
|
-
protected _bubbleUp(index: number) {
|
|
411
|
+
protected _bubbleUp(index: number): boolean {
|
|
441
412
|
const element = this.elements[index];
|
|
442
413
|
while (index > 0) {
|
|
443
414
|
const parent = (index - 1) >> 1;
|
|
@@ -447,6 +418,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
447
418
|
index = parent;
|
|
448
419
|
}
|
|
449
420
|
this.elements[index] = element;
|
|
421
|
+
return true;
|
|
450
422
|
}
|
|
451
423
|
|
|
452
424
|
/**
|
|
@@ -457,7 +429,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
457
429
|
* @param index - The index from which to start sinking.
|
|
458
430
|
* @param halfLength
|
|
459
431
|
*/
|
|
460
|
-
protected _sinkDown(index: number, halfLength: number) {
|
|
432
|
+
protected _sinkDown(index: number, halfLength: number): boolean {
|
|
461
433
|
const element = this.elements[index];
|
|
462
434
|
while (index < halfLength) {
|
|
463
435
|
let left = index << 1 | 1;
|
|
@@ -475,6 +447,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
475
447
|
index = left;
|
|
476
448
|
}
|
|
477
449
|
this.elements[index] = element;
|
|
450
|
+
return true;
|
|
478
451
|
}
|
|
479
452
|
}
|
|
480
453
|
|
|
@@ -5,10 +5,19 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
|
-
import { Heap } from './heap';
|
|
10
8
|
import type { HeapOptions } from '../../types';
|
|
9
|
+
import { Heap } from './heap';
|
|
11
10
|
|
|
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: The value of each parent node is greater 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
|
+
*/
|
|
12
21
|
export class MaxHeap<E = any> extends Heap<E> {
|
|
13
22
|
constructor(
|
|
14
23
|
elements?: Iterable<E>,
|
|
@@ -5,10 +5,19 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
|
-
import { Heap } from './heap';
|
|
10
8
|
import type { HeapOptions } from '../../types';
|
|
9
|
+
import { Heap } from './heap';
|
|
11
10
|
|
|
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: 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
|
+
*/
|
|
12
21
|
export class MinHeap<E = any> extends Heap<E> {
|
|
13
22
|
constructor(
|
|
14
23
|
elements?: Iterable<E>,
|