avl-tree-typed 1.47.4 → 1.47.6
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/binary-tree/avl-tree.d.ts +8 -8
- package/dist/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/data-structures/binary-tree/bst.js +56 -40
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/data-structures/graph/abstract-graph.js +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/data-structures/hash/hash-map.js +10 -4
- package/dist/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/data-structures/hash/hash-table.js +50 -5
- package/dist/data-structures/heap/heap.d.ts +25 -22
- package/dist/data-structures/heap/heap.js +101 -41
- package/dist/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/data-structures/heap/max-heap.js +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/data-structures/heap/min-heap.js +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
- package/dist/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/data-structures/linked-list/singly-linked-list.js +65 -60
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/data-structures/queue/deque.d.ts +50 -49
- package/dist/data-structures/queue/deque.js +81 -71
- package/dist/data-structures/queue/queue.d.ts +46 -0
- package/dist/data-structures/queue/queue.js +80 -0
- package/dist/data-structures/stack/stack.d.ts +20 -6
- package/dist/data-structures/stack/stack.js +65 -8
- package/dist/data-structures/trie/trie.d.ts +5 -0
- package/dist/data-structures/trie/trie.js +47 -0
- package/dist/interfaces/binary-tree.d.ts +3 -1
- package/dist/types/common.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +27 -17
- package/src/data-structures/binary-tree/binary-tree.ts +114 -97
- package/src/data-structures/binary-tree/bst.ts +67 -47
- package/src/data-structures/binary-tree/rb-tree.ts +34 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +13 -7
- package/src/data-structures/hash/hash-table.ts +59 -9
- package/src/data-structures/heap/heap.ts +115 -46
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
- package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +86 -75
- package/src/data-structures/queue/queue.ts +88 -0
- package/src/data-structures/stack/stack.ts +75 -10
- package/src/data-structures/trie/trie.ts +53 -0
- package/src/interfaces/binary-tree.ts +13 -1
- package/src/types/common.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
export declare class HashTableNode<K, V> {
|
|
9
9
|
key: K;
|
|
10
10
|
value: V;
|
|
11
|
-
next: HashTableNode<K, V> |
|
|
11
|
+
next: HashTableNode<K, V> | undefined;
|
|
12
12
|
constructor(key: K, value: V);
|
|
13
13
|
}
|
|
14
14
|
import { HashFunction } from '../../types';
|
|
15
|
-
export declare class HashTable<K, V> {
|
|
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;
|
|
18
18
|
constructor(capacity?: number, hashFn?: HashFunction<K>);
|
|
@@ -20,8 +20,8 @@ export declare class HashTable<K, V> {
|
|
|
20
20
|
get capacity(): number;
|
|
21
21
|
protected _size: number;
|
|
22
22
|
get size(): number;
|
|
23
|
-
protected _buckets: Array<HashTableNode<K, V> |
|
|
24
|
-
get buckets(): Array<HashTableNode<K, V> |
|
|
23
|
+
protected _buckets: Array<HashTableNode<K, V> | undefined>;
|
|
24
|
+
get buckets(): Array<HashTableNode<K, V> | undefined>;
|
|
25
25
|
protected _hashFn: HashFunction<K>;
|
|
26
26
|
get hashFn(): HashFunction<K>;
|
|
27
27
|
/**
|
|
@@ -50,6 +50,11 @@ export declare class HashTable<K, V> {
|
|
|
50
50
|
* any value.
|
|
51
51
|
*/
|
|
52
52
|
delete(key: K): void;
|
|
53
|
+
[Symbol.iterator](): Generator<[K, V], void, undefined>;
|
|
54
|
+
forEach(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => void): void;
|
|
55
|
+
filter(predicate: (entry: [K, V], index: number, table: HashTable<K, V>) => boolean): HashTable<K, V>;
|
|
56
|
+
map<T>(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => T): HashTable<K, T>;
|
|
57
|
+
reduce<T>(callback: (accumulator: T, entry: [K, V], index: number, table: HashTable<K, V>) => T, initialValue: T): T;
|
|
53
58
|
/**
|
|
54
59
|
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
55
60
|
* capacity of the data structure.
|
|
@@ -12,7 +12,7 @@ class HashTableNode {
|
|
|
12
12
|
constructor(key, value) {
|
|
13
13
|
this.key = key;
|
|
14
14
|
this.value = value;
|
|
15
|
-
this.next =
|
|
15
|
+
this.next = undefined;
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
exports.HashTableNode = HashTableNode;
|
|
@@ -21,7 +21,7 @@ class HashTable {
|
|
|
21
21
|
this._hashFn = hashFn || this._defaultHashFn;
|
|
22
22
|
this._capacity = Math.max(capacity, HashTable.DEFAULT_CAPACITY);
|
|
23
23
|
this._size = 0;
|
|
24
|
-
this._buckets = new Array(this._capacity).fill(
|
|
24
|
+
this._buckets = new Array(this._capacity).fill(undefined);
|
|
25
25
|
}
|
|
26
26
|
get capacity() {
|
|
27
27
|
return this._capacity;
|
|
@@ -101,7 +101,7 @@ class HashTable {
|
|
|
101
101
|
delete(key) {
|
|
102
102
|
const index = this._hash(key);
|
|
103
103
|
let currentNode = this._buckets[index];
|
|
104
|
-
let prevNode =
|
|
104
|
+
let prevNode = undefined;
|
|
105
105
|
while (currentNode) {
|
|
106
106
|
if (currentNode.key === key) {
|
|
107
107
|
if (prevNode) {
|
|
@@ -111,13 +111,58 @@ class HashTable {
|
|
|
111
111
|
this._buckets[index] = currentNode.next;
|
|
112
112
|
}
|
|
113
113
|
this._size--;
|
|
114
|
-
currentNode.next =
|
|
114
|
+
currentNode.next = undefined; // Release memory
|
|
115
115
|
return;
|
|
116
116
|
}
|
|
117
117
|
prevNode = currentNode;
|
|
118
118
|
currentNode = currentNode.next;
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
|
+
*[Symbol.iterator]() {
|
|
122
|
+
for (const bucket of this._buckets) {
|
|
123
|
+
let currentNode = bucket;
|
|
124
|
+
while (currentNode) {
|
|
125
|
+
yield [currentNode.key, currentNode.value];
|
|
126
|
+
currentNode = currentNode.next;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
forEach(callback) {
|
|
131
|
+
let index = 0;
|
|
132
|
+
for (const entry of this) {
|
|
133
|
+
callback(entry, index, this);
|
|
134
|
+
index++;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
filter(predicate) {
|
|
138
|
+
const newTable = new HashTable();
|
|
139
|
+
let index = 0;
|
|
140
|
+
for (const [key, value] of this) {
|
|
141
|
+
if (predicate([key, value], index, this)) {
|
|
142
|
+
newTable.set(key, value);
|
|
143
|
+
}
|
|
144
|
+
index++;
|
|
145
|
+
}
|
|
146
|
+
return newTable;
|
|
147
|
+
}
|
|
148
|
+
map(callback) {
|
|
149
|
+
const newTable = new HashTable();
|
|
150
|
+
let index = 0;
|
|
151
|
+
for (const [key, value] of this) {
|
|
152
|
+
newTable.set(key, callback([key, value], index, this));
|
|
153
|
+
index++;
|
|
154
|
+
}
|
|
155
|
+
return newTable;
|
|
156
|
+
}
|
|
157
|
+
reduce(callback, initialValue) {
|
|
158
|
+
let accumulator = initialValue;
|
|
159
|
+
let index = 0;
|
|
160
|
+
for (const entry of this) {
|
|
161
|
+
accumulator = callback(accumulator, entry, index, this);
|
|
162
|
+
index++;
|
|
163
|
+
}
|
|
164
|
+
return accumulator;
|
|
165
|
+
}
|
|
121
166
|
/**
|
|
122
167
|
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
123
168
|
* capacity of the data structure.
|
|
@@ -208,7 +253,7 @@ class HashTable {
|
|
|
208
253
|
*/
|
|
209
254
|
_expand() {
|
|
210
255
|
const newCapacity = this._capacity * 2;
|
|
211
|
-
const newBuckets = new Array(newCapacity).fill(
|
|
256
|
+
const newBuckets = new Array(newCapacity).fill(undefined);
|
|
212
257
|
for (const bucket of this._buckets) {
|
|
213
258
|
let currentNode = bucket;
|
|
214
259
|
while (currentNode) {
|
|
@@ -5,15 +5,12 @@
|
|
|
5
5
|
* @license MIT License
|
|
6
6
|
*/
|
|
7
7
|
import type { Comparator, DFSOrderPattern } from '../../types';
|
|
8
|
+
import { HeapOptions } from "../../types";
|
|
8
9
|
export declare class Heap<E = any> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
elements?: E[];
|
|
12
|
-
});
|
|
10
|
+
options: HeapOptions<E>;
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
13
12
|
protected _elements: E[];
|
|
14
13
|
get elements(): E[];
|
|
15
|
-
protected _comparator: Comparator<E>;
|
|
16
|
-
get comparator(): Comparator<E>;
|
|
17
14
|
/**
|
|
18
15
|
* Get the size (number of elements) of the heap.
|
|
19
16
|
*/
|
|
@@ -26,10 +23,10 @@ export declare class Heap<E = any> {
|
|
|
26
23
|
/**
|
|
27
24
|
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
28
25
|
* @returns A new Heap instance.
|
|
26
|
+
* @param elements
|
|
29
27
|
* @param options
|
|
30
28
|
*/
|
|
31
|
-
static heapify<E>(options: {
|
|
32
|
-
elements: E[];
|
|
29
|
+
static heapify<E>(elements: Iterable<E>, options: {
|
|
33
30
|
comparator: Comparator<E>;
|
|
34
31
|
}): Heap<E>;
|
|
35
32
|
/**
|
|
@@ -147,7 +144,7 @@ export declare class Heap<E = any> {
|
|
|
147
144
|
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
148
145
|
* @returns An array containing elements traversed in the specified order.
|
|
149
146
|
*/
|
|
150
|
-
dfs(order
|
|
147
|
+
dfs(order?: DFSOrderPattern): E[];
|
|
151
148
|
/**
|
|
152
149
|
* Time Complexity: O(n)
|
|
153
150
|
* Space Complexity: O(n)
|
|
@@ -195,10 +192,20 @@ export declare class Heap<E = any> {
|
|
|
195
192
|
* Fix the entire heap to maintain heap properties.
|
|
196
193
|
*/
|
|
197
194
|
fix(): void;
|
|
195
|
+
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
196
|
+
forEach(callback: (element: E, index: number, heap: this) => void): void;
|
|
197
|
+
filter(predicate: (element: E, index: number, heap: Heap<E>) => boolean): Heap<E>;
|
|
198
|
+
map<T>(callback: (element: E, index: number, heap: Heap<E>) => T, comparator: Comparator<T>): Heap<T>;
|
|
199
|
+
reduce<T>(callback: (accumulator: T, currentValue: E, currentIndex: number, heap: Heap<E>) => T, initialValue: T): T;
|
|
198
200
|
/**
|
|
199
201
|
* Time Complexity: O(log n)
|
|
200
202
|
* Space Complexity: O(1)
|
|
201
203
|
*/
|
|
204
|
+
print(): void;
|
|
205
|
+
/**
|
|
206
|
+
* Time Complexity: O(n)
|
|
207
|
+
* Space Complexity: O(1)
|
|
208
|
+
*/
|
|
202
209
|
/**
|
|
203
210
|
* Time Complexity: O(log n)
|
|
204
211
|
* Space Complexity: O(1)
|
|
@@ -207,10 +214,6 @@ export declare class Heap<E = any> {
|
|
|
207
214
|
* @param index - The index of the newly added element.
|
|
208
215
|
*/
|
|
209
216
|
protected _bubbleUp(index: number): void;
|
|
210
|
-
/**
|
|
211
|
-
* Time Complexity: O(n)
|
|
212
|
-
* Space Complexity: O(1)
|
|
213
|
-
*/
|
|
214
217
|
/**
|
|
215
218
|
* Time Complexity: O(log n)
|
|
216
219
|
* Space Complexity: O(1)
|
|
@@ -344,18 +347,18 @@ export declare class FibonacciHeap<E> {
|
|
|
344
347
|
*/
|
|
345
348
|
merge(heapToMerge: FibonacciHeap<E>): void;
|
|
346
349
|
/**
|
|
347
|
-
*
|
|
348
|
-
* @param
|
|
349
|
-
* @param {E} b
|
|
350
|
+
* Create a new node.
|
|
351
|
+
* @param element
|
|
350
352
|
* @protected
|
|
351
353
|
*/
|
|
352
|
-
|
|
354
|
+
createNode(element: E): FibonacciHeapNode<E>;
|
|
353
355
|
/**
|
|
354
|
-
*
|
|
355
|
-
* @param
|
|
356
|
+
* Default comparator function used by the heap.
|
|
357
|
+
* @param {E} a
|
|
358
|
+
* @param {E} b
|
|
356
359
|
* @protected
|
|
357
360
|
*/
|
|
358
|
-
protected
|
|
361
|
+
protected _defaultComparator(a: E, b: E): number;
|
|
359
362
|
/**
|
|
360
363
|
* Time Complexity: O(1)
|
|
361
364
|
* Space Complexity: O(1)
|
|
@@ -394,7 +397,7 @@ export declare class FibonacciHeap<E> {
|
|
|
394
397
|
* @param x
|
|
395
398
|
* @protected
|
|
396
399
|
*/
|
|
397
|
-
protected
|
|
400
|
+
protected _link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
|
|
398
401
|
/**
|
|
399
402
|
* Time Complexity: O(n log n), where n is the number of elements in the heap.
|
|
400
403
|
* Space Complexity: O(n)
|
|
@@ -406,5 +409,5 @@ export declare class FibonacciHeap<E> {
|
|
|
406
409
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
407
410
|
* @protected
|
|
408
411
|
*/
|
|
409
|
-
protected
|
|
412
|
+
protected _consolidate(): void;
|
|
410
413
|
}
|
|
@@ -8,20 +8,34 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
|
|
10
10
|
class Heap {
|
|
11
|
-
constructor(options) {
|
|
11
|
+
constructor(elements, options) {
|
|
12
12
|
this._elements = [];
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
const defaultComparator = (a, b) => {
|
|
14
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
15
|
+
throw new Error('The a, b params of compare function must be number');
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return a - b;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
if (options) {
|
|
22
|
+
this.options = options;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
this.options = {
|
|
26
|
+
comparator: defaultComparator
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
if (elements) {
|
|
30
|
+
for (const el of elements) {
|
|
31
|
+
this.push(el);
|
|
32
|
+
}
|
|
33
|
+
// this.fix();
|
|
17
34
|
}
|
|
18
35
|
}
|
|
19
36
|
get elements() {
|
|
20
37
|
return this._elements;
|
|
21
38
|
}
|
|
22
|
-
get comparator() {
|
|
23
|
-
return this._comparator;
|
|
24
|
-
}
|
|
25
39
|
/**
|
|
26
40
|
* Get the size (number of elements) of the heap.
|
|
27
41
|
*/
|
|
@@ -39,10 +53,11 @@ class Heap {
|
|
|
39
53
|
/**
|
|
40
54
|
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
41
55
|
* @returns A new Heap instance.
|
|
56
|
+
* @param elements
|
|
42
57
|
* @param options
|
|
43
58
|
*/
|
|
44
|
-
static heapify(options) {
|
|
45
|
-
return new Heap(options);
|
|
59
|
+
static heapify(elements, options) {
|
|
60
|
+
return new Heap(elements, options);
|
|
46
61
|
}
|
|
47
62
|
/**
|
|
48
63
|
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
@@ -204,29 +219,30 @@ class Heap {
|
|
|
204
219
|
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
205
220
|
* @returns An array containing elements traversed in the specified order.
|
|
206
221
|
*/
|
|
207
|
-
dfs(order) {
|
|
222
|
+
dfs(order = 'pre') {
|
|
208
223
|
const result = [];
|
|
209
224
|
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
210
|
-
const
|
|
225
|
+
const _dfs = (index) => {
|
|
226
|
+
const left = 2 * index + 1, right = left + 1;
|
|
211
227
|
if (index < this.size) {
|
|
212
228
|
if (order === 'in') {
|
|
213
|
-
|
|
229
|
+
_dfs(left);
|
|
214
230
|
result.push(this.elements[index]);
|
|
215
|
-
|
|
231
|
+
_dfs(right);
|
|
216
232
|
}
|
|
217
233
|
else if (order === 'pre') {
|
|
218
234
|
result.push(this.elements[index]);
|
|
219
|
-
|
|
220
|
-
|
|
235
|
+
_dfs(left);
|
|
236
|
+
_dfs(right);
|
|
221
237
|
}
|
|
222
238
|
else if (order === 'post') {
|
|
223
|
-
|
|
224
|
-
|
|
239
|
+
_dfs(left);
|
|
240
|
+
_dfs(right);
|
|
225
241
|
result.push(this.elements[index]);
|
|
226
242
|
}
|
|
227
243
|
}
|
|
228
244
|
};
|
|
229
|
-
|
|
245
|
+
_dfs(0); // Traverse starting from the root node
|
|
230
246
|
return result;
|
|
231
247
|
}
|
|
232
248
|
/**
|
|
@@ -255,7 +271,7 @@ class Heap {
|
|
|
255
271
|
* @returns A new Heap instance containing the same elements.
|
|
256
272
|
*/
|
|
257
273
|
clone() {
|
|
258
|
-
const clonedHeap = new Heap(
|
|
274
|
+
const clonedHeap = new Heap([], this.options);
|
|
259
275
|
clonedHeap._elements = [...this.elements];
|
|
260
276
|
return clonedHeap;
|
|
261
277
|
}
|
|
@@ -294,10 +310,58 @@ class Heap {
|
|
|
294
310
|
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
295
311
|
this._sinkDown(i, this.elements.length >> 1);
|
|
296
312
|
}
|
|
313
|
+
*[Symbol.iterator]() {
|
|
314
|
+
for (const element of this.elements) {
|
|
315
|
+
yield element;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
forEach(callback) {
|
|
319
|
+
let index = 0;
|
|
320
|
+
for (const el of this) {
|
|
321
|
+
callback(el, index, this);
|
|
322
|
+
index++;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
filter(predicate) {
|
|
326
|
+
const filteredHeap = new Heap([], this.options);
|
|
327
|
+
let index = 0;
|
|
328
|
+
for (const el of this) {
|
|
329
|
+
if (predicate(el, index, this)) {
|
|
330
|
+
filteredHeap.push(el);
|
|
331
|
+
}
|
|
332
|
+
index++;
|
|
333
|
+
}
|
|
334
|
+
return filteredHeap;
|
|
335
|
+
}
|
|
336
|
+
map(callback, comparator) {
|
|
337
|
+
const mappedHeap = new Heap([], { comparator: comparator });
|
|
338
|
+
let index = 0;
|
|
339
|
+
for (const el of this) {
|
|
340
|
+
mappedHeap.add(callback(el, index, this));
|
|
341
|
+
index++;
|
|
342
|
+
}
|
|
343
|
+
return mappedHeap;
|
|
344
|
+
}
|
|
345
|
+
reduce(callback, initialValue) {
|
|
346
|
+
let accumulator = initialValue;
|
|
347
|
+
let index = 0;
|
|
348
|
+
for (const el of this) {
|
|
349
|
+
accumulator = callback(accumulator, el, index, this);
|
|
350
|
+
index++;
|
|
351
|
+
}
|
|
352
|
+
return accumulator;
|
|
353
|
+
}
|
|
297
354
|
/**
|
|
298
355
|
* Time Complexity: O(log n)
|
|
299
356
|
* Space Complexity: O(1)
|
|
300
357
|
*/
|
|
358
|
+
print() {
|
|
359
|
+
console.log([...this]);
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Time Complexity: O(n)
|
|
363
|
+
* Space Complexity: O(1)
|
|
364
|
+
*/
|
|
301
365
|
/**
|
|
302
366
|
* Time Complexity: O(log n)
|
|
303
367
|
* Space Complexity: O(1)
|
|
@@ -310,17 +374,13 @@ class Heap {
|
|
|
310
374
|
while (index > 0) {
|
|
311
375
|
const parent = (index - 1) >> 1;
|
|
312
376
|
const parentItem = this.elements[parent];
|
|
313
|
-
if (this.
|
|
377
|
+
if (this.options.comparator(parentItem, element) <= 0)
|
|
314
378
|
break;
|
|
315
379
|
this.elements[index] = parentItem;
|
|
316
380
|
index = parent;
|
|
317
381
|
}
|
|
318
382
|
this.elements[index] = element;
|
|
319
383
|
}
|
|
320
|
-
/**
|
|
321
|
-
* Time Complexity: O(n)
|
|
322
|
-
* Space Complexity: O(1)
|
|
323
|
-
*/
|
|
324
384
|
/**
|
|
325
385
|
* Time Complexity: O(log n)
|
|
326
386
|
* Space Complexity: O(1)
|
|
@@ -336,11 +396,11 @@ class Heap {
|
|
|
336
396
|
const right = left + 1;
|
|
337
397
|
let minItem = this.elements[left];
|
|
338
398
|
if (right < this.elements.length &&
|
|
339
|
-
this.
|
|
399
|
+
this.options.comparator(minItem, this.elements[right]) > 0) {
|
|
340
400
|
left = right;
|
|
341
401
|
minItem = this.elements[right];
|
|
342
402
|
}
|
|
343
|
-
if (this.
|
|
403
|
+
if (this.options.comparator(minItem, element) >= 0)
|
|
344
404
|
break;
|
|
345
405
|
this.elements[index] = minItem;
|
|
346
406
|
index = left;
|
|
@@ -361,7 +421,7 @@ class FibonacciHeap {
|
|
|
361
421
|
constructor(comparator) {
|
|
362
422
|
this._size = 0;
|
|
363
423
|
this.clear();
|
|
364
|
-
this._comparator = comparator || this.
|
|
424
|
+
this._comparator = comparator || this._defaultComparator;
|
|
365
425
|
if (typeof this.comparator !== 'function') {
|
|
366
426
|
throw new Error('FibonacciHeap constructor: given comparator should be a function.');
|
|
367
427
|
}
|
|
@@ -532,7 +592,7 @@ class FibonacciHeap {
|
|
|
532
592
|
}
|
|
533
593
|
else {
|
|
534
594
|
this._min = z.right;
|
|
535
|
-
this.
|
|
595
|
+
this._consolidate();
|
|
536
596
|
}
|
|
537
597
|
this._size--;
|
|
538
598
|
return z.element;
|
|
@@ -572,27 +632,27 @@ class FibonacciHeap {
|
|
|
572
632
|
// Clear the heap that was merged
|
|
573
633
|
heapToMerge.clear();
|
|
574
634
|
}
|
|
635
|
+
/**
|
|
636
|
+
* Create a new node.
|
|
637
|
+
* @param element
|
|
638
|
+
* @protected
|
|
639
|
+
*/
|
|
640
|
+
createNode(element) {
|
|
641
|
+
return new FibonacciHeapNode(element);
|
|
642
|
+
}
|
|
575
643
|
/**
|
|
576
644
|
* Default comparator function used by the heap.
|
|
577
645
|
* @param {E} a
|
|
578
646
|
* @param {E} b
|
|
579
647
|
* @protected
|
|
580
648
|
*/
|
|
581
|
-
|
|
649
|
+
_defaultComparator(a, b) {
|
|
582
650
|
if (a < b)
|
|
583
651
|
return -1;
|
|
584
652
|
if (a > b)
|
|
585
653
|
return 1;
|
|
586
654
|
return 0;
|
|
587
655
|
}
|
|
588
|
-
/**
|
|
589
|
-
* Create a new node.
|
|
590
|
-
* @param element
|
|
591
|
-
* @protected
|
|
592
|
-
*/
|
|
593
|
-
createNode(element) {
|
|
594
|
-
return new FibonacciHeapNode(element);
|
|
595
|
-
}
|
|
596
656
|
/**
|
|
597
657
|
* Time Complexity: O(1)
|
|
598
658
|
* Space Complexity: O(1)
|
|
@@ -648,7 +708,7 @@ class FibonacciHeap {
|
|
|
648
708
|
* @param x
|
|
649
709
|
* @protected
|
|
650
710
|
*/
|
|
651
|
-
|
|
711
|
+
_link(y, x) {
|
|
652
712
|
this.removeFromRoot(y);
|
|
653
713
|
y.left = y;
|
|
654
714
|
y.right = y;
|
|
@@ -667,7 +727,7 @@ class FibonacciHeap {
|
|
|
667
727
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
668
728
|
* @protected
|
|
669
729
|
*/
|
|
670
|
-
|
|
730
|
+
_consolidate() {
|
|
671
731
|
const A = new Array(this.size);
|
|
672
732
|
const elements = this.consumeLinkedList(this.root);
|
|
673
733
|
let x, y, d, t;
|
|
@@ -681,7 +741,7 @@ class FibonacciHeap {
|
|
|
681
741
|
x = y;
|
|
682
742
|
y = t;
|
|
683
743
|
}
|
|
684
|
-
this.
|
|
744
|
+
this._link(y, x);
|
|
685
745
|
A[d] = undefined;
|
|
686
746
|
d++;
|
|
687
747
|
}
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from './heap';
|
|
9
|
-
import type {
|
|
9
|
+
import type { HeapOptions } from '../../types';
|
|
10
10
|
export declare class MaxHeap<E = any> extends Heap<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
15
12
|
}
|
|
@@ -10,7 +10,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.MaxHeap = void 0;
|
|
11
11
|
const heap_1 = require("./heap");
|
|
12
12
|
class MaxHeap extends heap_1.Heap {
|
|
13
|
-
constructor(options = {
|
|
13
|
+
constructor(elements, options = {
|
|
14
14
|
comparator: (a, b) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -20,7 +20,7 @@ class MaxHeap extends heap_1.Heap {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
}) {
|
|
23
|
-
super(options);
|
|
23
|
+
super(elements, options);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
exports.MaxHeap = MaxHeap;
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from './heap';
|
|
9
|
-
import type {
|
|
9
|
+
import type { HeapOptions } from '../../types';
|
|
10
10
|
export declare class MinHeap<E = any> extends Heap<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
15
12
|
}
|
|
@@ -10,7 +10,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.MinHeap = void 0;
|
|
11
11
|
const heap_1 = require("./heap");
|
|
12
12
|
class MinHeap extends heap_1.Heap {
|
|
13
|
-
constructor(options = {
|
|
13
|
+
constructor(elements, options = {
|
|
14
14
|
comparator: (a, b) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -20,7 +20,7 @@ class MinHeap extends heap_1.Heap {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
}) {
|
|
23
|
-
super(options);
|
|
23
|
+
super(elements, options);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
exports.MinHeap = MinHeap;
|