heap-typed 1.38.0 → 1.38.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/binary-tree/avl-tree.d.ts +9 -9
- package/dist/data-structures/binary-tree/avl-tree.js +22 -22
- package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -31
- package/dist/data-structures/binary-tree/binary-tree.js +32 -32
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -9
- package/dist/data-structures/binary-tree/tree-multiset.js +23 -23
- package/dist/data-structures/hash/hash-map.d.ts +25 -25
- package/dist/data-structures/hash/hash-map.js +59 -59
- package/dist/data-structures/hash/hash-table.d.ts +34 -34
- package/dist/data-structures/hash/hash-table.js +99 -99
- package/dist/data-structures/heap/heap.d.ts +66 -66
- package/dist/data-structures/heap/heap.js +167 -167
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +3 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +17 -17
- package/dist/data-structures/linked-list/skip-linked-list.js +34 -34
- package/dist/data-structures/matrix/matrix2d.d.ts +7 -7
- package/dist/data-structures/matrix/matrix2d.js +9 -9
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +6 -6
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/package.json +1 -4
- package/src/data-structures/binary-tree/avl-tree.ts +28 -28
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +57 -57
- package/src/data-structures/binary-tree/bst.ts +4 -0
- package/src/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multiset.ts +30 -31
- package/src/data-structures/graph/abstract-graph.ts +10 -11
- package/src/data-structures/graph/directed-graph.ts +1 -2
- package/src/data-structures/graph/undirected-graph.ts +4 -5
- package/src/data-structures/hash/hash-map.ts +82 -76
- package/src/data-structures/hash/hash-table.ts +112 -109
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +182 -181
- package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +45 -38
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/matrix2d.ts +10 -10
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/queue/deque.ts +5 -4
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +9 -9
- package/src/index.ts +3 -3
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/helpers.ts +5 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -15,6 +15,34 @@ export class Heap<E> {
|
|
|
15
15
|
this.comparator = comparator;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Get the size (number of elements) of the heap.
|
|
20
|
+
*/
|
|
21
|
+
get size(): number {
|
|
22
|
+
return this.nodes.length;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
27
|
+
* @returns The last element or undefined if the heap is empty.
|
|
28
|
+
*/
|
|
29
|
+
get leaf(): E | undefined {
|
|
30
|
+
return this.nodes[this.size - 1] ?? undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
35
|
+
* @param nodes
|
|
36
|
+
* @param comparator - Comparison function.
|
|
37
|
+
* @returns A new Heap instance.
|
|
38
|
+
*/
|
|
39
|
+
static heapify<E>(nodes: E[], comparator: Comparator<E>): Heap<E> {
|
|
40
|
+
const binaryHeap = new Heap<E>(comparator);
|
|
41
|
+
binaryHeap.nodes = [...nodes];
|
|
42
|
+
binaryHeap.fix(); // Fix heap properties
|
|
43
|
+
return binaryHeap;
|
|
44
|
+
}
|
|
45
|
+
|
|
18
46
|
/**
|
|
19
47
|
* Insert an element into the heap and maintain the heap properties.
|
|
20
48
|
* @param element - The element to be inserted.
|
|
@@ -59,57 +87,6 @@ export class Heap<E> {
|
|
|
59
87
|
return this.poll();
|
|
60
88
|
}
|
|
61
89
|
|
|
62
|
-
/**
|
|
63
|
-
* Float operation to maintain heap properties after adding an element.
|
|
64
|
-
* @param index - The index of the newly added element.
|
|
65
|
-
*/
|
|
66
|
-
protected bubbleUp(index: number): void {
|
|
67
|
-
const element = this.nodes[index];
|
|
68
|
-
while (index > 0) {
|
|
69
|
-
const parentIndex = Math.floor((index - 1) / 2);
|
|
70
|
-
const parent = this.nodes[parentIndex];
|
|
71
|
-
if (this.comparator(element, parent) < 0) {
|
|
72
|
-
this.nodes[index] = parent;
|
|
73
|
-
this.nodes[parentIndex] = element;
|
|
74
|
-
index = parentIndex;
|
|
75
|
-
} else {
|
|
76
|
-
break;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Sinking operation to maintain heap properties after removing the top element.
|
|
83
|
-
* @param index - The index from which to start sinking.
|
|
84
|
-
*/
|
|
85
|
-
protected sinkDown(index: number): void {
|
|
86
|
-
const leftChildIndex = 2 * index + 1;
|
|
87
|
-
const rightChildIndex = 2 * index + 2;
|
|
88
|
-
const length = this.nodes.length;
|
|
89
|
-
let targetIndex = index;
|
|
90
|
-
|
|
91
|
-
if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
|
|
92
|
-
targetIndex = leftChildIndex;
|
|
93
|
-
}
|
|
94
|
-
if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
|
|
95
|
-
targetIndex = rightChildIndex;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (targetIndex !== index) {
|
|
99
|
-
const temp = this.nodes[index];
|
|
100
|
-
this.nodes[index] = this.nodes[targetIndex];
|
|
101
|
-
this.nodes[targetIndex] = temp;
|
|
102
|
-
this.sinkDown(targetIndex);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Fix the entire heap to maintain heap properties.
|
|
108
|
-
*/
|
|
109
|
-
protected fix() {
|
|
110
|
-
for (let i = Math.floor(this.size / 2); i >= 0; i--) this.sinkDown(i);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
90
|
/**
|
|
114
91
|
* Peek at the top element of the heap without removing it.
|
|
115
92
|
* @returns The top element or undefined if the heap is empty.
|
|
@@ -121,21 +98,6 @@ export class Heap<E> {
|
|
|
121
98
|
return this.nodes[0];
|
|
122
99
|
}
|
|
123
100
|
|
|
124
|
-
/**
|
|
125
|
-
* Get the size (number of elements) of the heap.
|
|
126
|
-
*/
|
|
127
|
-
get size(): number {
|
|
128
|
-
return this.nodes.length;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
133
|
-
* @returns The last element or undefined if the heap is empty.
|
|
134
|
-
*/
|
|
135
|
-
get leaf(): E | undefined {
|
|
136
|
-
return this.nodes[this.size - 1] ?? undefined;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
101
|
/**
|
|
140
102
|
* Check if the heap is empty.
|
|
141
103
|
* @returns True if the heap is empty, otherwise false.
|
|
@@ -238,16 +200,54 @@ export class Heap<E> {
|
|
|
238
200
|
}
|
|
239
201
|
|
|
240
202
|
/**
|
|
241
|
-
*
|
|
242
|
-
* @param
|
|
243
|
-
* @param comparator - Comparison function.
|
|
244
|
-
* @returns A new Heap instance.
|
|
203
|
+
* Float operation to maintain heap properties after adding an element.
|
|
204
|
+
* @param index - The index of the newly added element.
|
|
245
205
|
*/
|
|
246
|
-
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
206
|
+
protected bubbleUp(index: number): void {
|
|
207
|
+
const element = this.nodes[index];
|
|
208
|
+
while (index > 0) {
|
|
209
|
+
const parentIndex = Math.floor((index - 1) / 2);
|
|
210
|
+
const parent = this.nodes[parentIndex];
|
|
211
|
+
if (this.comparator(element, parent) < 0) {
|
|
212
|
+
this.nodes[index] = parent;
|
|
213
|
+
this.nodes[parentIndex] = element;
|
|
214
|
+
index = parentIndex;
|
|
215
|
+
} else {
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
223
|
+
* @param index - The index from which to start sinking.
|
|
224
|
+
*/
|
|
225
|
+
protected sinkDown(index: number): void {
|
|
226
|
+
const leftChildIndex = 2 * index + 1;
|
|
227
|
+
const rightChildIndex = 2 * index + 2;
|
|
228
|
+
const length = this.nodes.length;
|
|
229
|
+
let targetIndex = index;
|
|
230
|
+
|
|
231
|
+
if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
|
|
232
|
+
targetIndex = leftChildIndex;
|
|
233
|
+
}
|
|
234
|
+
if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
|
|
235
|
+
targetIndex = rightChildIndex;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (targetIndex !== index) {
|
|
239
|
+
const temp = this.nodes[index];
|
|
240
|
+
this.nodes[index] = this.nodes[targetIndex];
|
|
241
|
+
this.nodes[targetIndex] = temp;
|
|
242
|
+
this.sinkDown(targetIndex);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Fix the entire heap to maintain heap properties.
|
|
248
|
+
*/
|
|
249
|
+
protected fix() {
|
|
250
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--) this.sinkDown(i);
|
|
251
251
|
}
|
|
252
252
|
}
|
|
253
253
|
|
|
@@ -259,6 +259,7 @@ export class FibonacciHeapNode<E> {
|
|
|
259
259
|
child?: FibonacciHeapNode<E>;
|
|
260
260
|
parent?: FibonacciHeapNode<E>;
|
|
261
261
|
marked: boolean;
|
|
262
|
+
|
|
262
263
|
constructor(element: E, degree = 0) {
|
|
263
264
|
this.element = element;
|
|
264
265
|
this.degree = degree;
|
|
@@ -268,8 +269,8 @@ export class FibonacciHeapNode<E> {
|
|
|
268
269
|
|
|
269
270
|
export class FibonacciHeap<E> {
|
|
270
271
|
root?: FibonacciHeapNode<E>;
|
|
271
|
-
protected min?: FibonacciHeapNode<E>;
|
|
272
272
|
size: number = 0;
|
|
273
|
+
protected min?: FibonacciHeapNode<E>;
|
|
273
274
|
protected readonly comparator: Comparator<E>;
|
|
274
275
|
|
|
275
276
|
constructor(comparator?: Comparator<E>) {
|
|
@@ -281,18 +282,6 @@ export class FibonacciHeap<E> {
|
|
|
281
282
|
}
|
|
282
283
|
}
|
|
283
284
|
|
|
284
|
-
/**
|
|
285
|
-
* Default comparator function used by the heap.
|
|
286
|
-
* @param {E} a
|
|
287
|
-
* @param {E} b
|
|
288
|
-
* @protected
|
|
289
|
-
*/
|
|
290
|
-
protected defaultComparator(a: E, b: E): number {
|
|
291
|
-
if (a < b) return -1;
|
|
292
|
-
if (a > b) return 1;
|
|
293
|
-
return 0;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
285
|
/**
|
|
297
286
|
* Get the size (number of elements) of the heap.
|
|
298
287
|
* @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
|
|
@@ -303,30 +292,6 @@ export class FibonacciHeap<E> {
|
|
|
303
292
|
this.size = 0;
|
|
304
293
|
}
|
|
305
294
|
|
|
306
|
-
/**
|
|
307
|
-
* Create a new node.
|
|
308
|
-
* @param element
|
|
309
|
-
* @protected
|
|
310
|
-
*/
|
|
311
|
-
protected createNode(element: E): FibonacciHeapNode<E> {
|
|
312
|
-
return new FibonacciHeapNode<E>(element);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* Merge the given node with the root list.
|
|
317
|
-
* @param node - The node to be merged.
|
|
318
|
-
*/
|
|
319
|
-
protected mergeWithRoot(node: FibonacciHeapNode<E>): void {
|
|
320
|
-
if (!this.root) {
|
|
321
|
-
this.root = node;
|
|
322
|
-
} else {
|
|
323
|
-
node.right = this.root.right;
|
|
324
|
-
node.left = this.root;
|
|
325
|
-
this.root.right!.left = node;
|
|
326
|
-
this.root.right = node;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
295
|
/**
|
|
331
296
|
* O(1) time operation.
|
|
332
297
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -394,18 +359,6 @@ export class FibonacciHeap<E> {
|
|
|
394
359
|
return nodes;
|
|
395
360
|
}
|
|
396
361
|
|
|
397
|
-
/**
|
|
398
|
-
* O(log n) time operation.
|
|
399
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
400
|
-
* @param node - The node to be removed.
|
|
401
|
-
* @protected
|
|
402
|
-
*/
|
|
403
|
-
protected removeFromRoot(node: FibonacciHeapNode<E>): void {
|
|
404
|
-
if (this.root === node) this.root = node.right;
|
|
405
|
-
if (node.left) node.left.right = node.right;
|
|
406
|
-
if (node.right) node.right.left = node.left;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
362
|
/**
|
|
410
363
|
* O(log n) time operation.
|
|
411
364
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -423,63 +376,6 @@ export class FibonacciHeap<E> {
|
|
|
423
376
|
}
|
|
424
377
|
}
|
|
425
378
|
|
|
426
|
-
/**
|
|
427
|
-
* O(log n) time operation.
|
|
428
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
429
|
-
* @param y
|
|
430
|
-
* @param x
|
|
431
|
-
* @protected
|
|
432
|
-
*/
|
|
433
|
-
protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void {
|
|
434
|
-
this.removeFromRoot(y);
|
|
435
|
-
y.left = y;
|
|
436
|
-
y.right = y;
|
|
437
|
-
this.mergeWithChild(x, y);
|
|
438
|
-
x.degree++;
|
|
439
|
-
y.parent = x;
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* O(log n) time operation.
|
|
444
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
445
|
-
* @protected
|
|
446
|
-
*/
|
|
447
|
-
protected consolidate(): void {
|
|
448
|
-
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this.size);
|
|
449
|
-
const nodes = this.consumeLinkedList(this.root);
|
|
450
|
-
let x: FibonacciHeapNode<E> | undefined,
|
|
451
|
-
y: FibonacciHeapNode<E> | undefined,
|
|
452
|
-
d: number,
|
|
453
|
-
t: FibonacciHeapNode<E> | undefined;
|
|
454
|
-
|
|
455
|
-
for (const node of nodes) {
|
|
456
|
-
x = node;
|
|
457
|
-
d = x.degree;
|
|
458
|
-
|
|
459
|
-
while (A[d]) {
|
|
460
|
-
y = A[d] as FibonacciHeapNode<E>;
|
|
461
|
-
|
|
462
|
-
if (this.comparator(x.element, y.element) > 0) {
|
|
463
|
-
t = x;
|
|
464
|
-
x = y;
|
|
465
|
-
y = t;
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
this.link(y, x);
|
|
469
|
-
A[d] = undefined;
|
|
470
|
-
d++;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
A[d] = x;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
for (let i = 0; i < this.size; i++) {
|
|
477
|
-
if (A[i] && this.comparator(A[i]!.element, this.min!.element) <= 0) {
|
|
478
|
-
this.min = A[i]!;
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
|
|
483
379
|
/**
|
|
484
380
|
* O(log n) time operation.
|
|
485
381
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -557,4 +453,109 @@ export class FibonacciHeap<E> {
|
|
|
557
453
|
// Clear the heap that was merged
|
|
558
454
|
heapToMerge.clear();
|
|
559
455
|
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Default comparator function used by the heap.
|
|
459
|
+
* @param {E} a
|
|
460
|
+
* @param {E} b
|
|
461
|
+
* @protected
|
|
462
|
+
*/
|
|
463
|
+
protected defaultComparator(a: E, b: E): number {
|
|
464
|
+
if (a < b) return -1;
|
|
465
|
+
if (a > b) return 1;
|
|
466
|
+
return 0;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Create a new node.
|
|
471
|
+
* @param element
|
|
472
|
+
* @protected
|
|
473
|
+
*/
|
|
474
|
+
protected createNode(element: E): FibonacciHeapNode<E> {
|
|
475
|
+
return new FibonacciHeapNode<E>(element);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Merge the given node with the root list.
|
|
480
|
+
* @param node - The node to be merged.
|
|
481
|
+
*/
|
|
482
|
+
protected mergeWithRoot(node: FibonacciHeapNode<E>): void {
|
|
483
|
+
if (!this.root) {
|
|
484
|
+
this.root = node;
|
|
485
|
+
} else {
|
|
486
|
+
node.right = this.root.right;
|
|
487
|
+
node.left = this.root;
|
|
488
|
+
this.root.right!.left = node;
|
|
489
|
+
this.root.right = node;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* O(log n) time operation.
|
|
495
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
496
|
+
* @param node - The node to be removed.
|
|
497
|
+
* @protected
|
|
498
|
+
*/
|
|
499
|
+
protected removeFromRoot(node: FibonacciHeapNode<E>): void {
|
|
500
|
+
if (this.root === node) this.root = node.right;
|
|
501
|
+
if (node.left) node.left.right = node.right;
|
|
502
|
+
if (node.right) node.right.left = node.left;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* O(log n) time operation.
|
|
507
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
508
|
+
* @param y
|
|
509
|
+
* @param x
|
|
510
|
+
* @protected
|
|
511
|
+
*/
|
|
512
|
+
protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void {
|
|
513
|
+
this.removeFromRoot(y);
|
|
514
|
+
y.left = y;
|
|
515
|
+
y.right = y;
|
|
516
|
+
this.mergeWithChild(x, y);
|
|
517
|
+
x.degree++;
|
|
518
|
+
y.parent = x;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* O(log n) time operation.
|
|
523
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
524
|
+
* @protected
|
|
525
|
+
*/
|
|
526
|
+
protected consolidate(): void {
|
|
527
|
+
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this.size);
|
|
528
|
+
const nodes = this.consumeLinkedList(this.root);
|
|
529
|
+
let x: FibonacciHeapNode<E> | undefined,
|
|
530
|
+
y: FibonacciHeapNode<E> | undefined,
|
|
531
|
+
d: number,
|
|
532
|
+
t: FibonacciHeapNode<E> | undefined;
|
|
533
|
+
|
|
534
|
+
for (const node of nodes) {
|
|
535
|
+
x = node;
|
|
536
|
+
d = x.degree;
|
|
537
|
+
|
|
538
|
+
while (A[d]) {
|
|
539
|
+
y = A[d] as FibonacciHeapNode<E>;
|
|
540
|
+
|
|
541
|
+
if (this.comparator(x.element, y.element) > 0) {
|
|
542
|
+
t = x;
|
|
543
|
+
x = y;
|
|
544
|
+
y = t;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
this.link(y, x);
|
|
548
|
+
A[d] = undefined;
|
|
549
|
+
d++;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
A[d] = x;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
for (let i = 0; i < this.size; i++) {
|
|
556
|
+
if (A[i] && this.comparator(A[i]!.element, this.min!.element) <= 0) {
|
|
557
|
+
this.min = A[i]!;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
560
561
|
}
|
|
@@ -84,6 +84,10 @@ export class DoublyLinkedList<E = any> {
|
|
|
84
84
|
return this._length;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
+
get size(): number {
|
|
88
|
+
return this.length;
|
|
89
|
+
}
|
|
90
|
+
|
|
87
91
|
/**
|
|
88
92
|
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
89
93
|
* given array.
|
|
@@ -222,10 +226,6 @@ export class DoublyLinkedList<E = any> {
|
|
|
222
226
|
return this.tail?.val;
|
|
223
227
|
}
|
|
224
228
|
|
|
225
|
-
get size(): number {
|
|
226
|
-
return this.length;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
229
|
/**
|
|
230
230
|
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
|
|
231
231
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -19,20 +19,32 @@ export class SkipListNode<K, V> {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export class SkipList<K, V> {
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
/**
|
|
23
|
+
* The constructor initializes a SkipList with a specified maximum level and probability.
|
|
24
|
+
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines
|
|
25
|
+
* the maximum number of levels that can be created in the skip list.
|
|
26
|
+
* @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher
|
|
27
|
+
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
28
|
+
*/
|
|
29
|
+
constructor(maxLevel = 16, probability = 0.5) {
|
|
30
|
+
this._head = new SkipListNode<K, V>(null as any, null as any, maxLevel);
|
|
31
|
+
this._level = 0;
|
|
32
|
+
this._maxLevel = maxLevel;
|
|
33
|
+
this._probability = probability;
|
|
24
34
|
}
|
|
25
35
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return this._maxLevel;
|
|
36
|
+
private _head: SkipListNode<K, V>;
|
|
37
|
+
|
|
38
|
+
get head(): SkipListNode<K, V> {
|
|
39
|
+
return this._head;
|
|
31
40
|
}
|
|
32
41
|
|
|
33
|
-
set
|
|
34
|
-
this.
|
|
42
|
+
set head(value: SkipListNode<K, V>) {
|
|
43
|
+
this._head = value;
|
|
35
44
|
}
|
|
45
|
+
|
|
46
|
+
private _level: number;
|
|
47
|
+
|
|
36
48
|
get level(): number {
|
|
37
49
|
return this._level;
|
|
38
50
|
}
|
|
@@ -40,42 +52,25 @@ export class SkipList<K, V> {
|
|
|
40
52
|
set level(value: number) {
|
|
41
53
|
this._level = value;
|
|
42
54
|
}
|
|
43
|
-
|
|
44
|
-
|
|
55
|
+
|
|
56
|
+
private _maxLevel: number;
|
|
57
|
+
|
|
58
|
+
get maxLevel(): number {
|
|
59
|
+
return this._maxLevel;
|
|
45
60
|
}
|
|
46
61
|
|
|
47
|
-
set
|
|
48
|
-
this.
|
|
62
|
+
set maxLevel(value: number) {
|
|
63
|
+
this._maxLevel = value;
|
|
49
64
|
}
|
|
50
|
-
|
|
51
|
-
private _level: number;
|
|
52
|
-
private _maxLevel: number;
|
|
65
|
+
|
|
53
66
|
private _probability: number;
|
|
54
67
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines
|
|
58
|
-
* the maximum number of levels that can be created in the skip list.
|
|
59
|
-
* @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher
|
|
60
|
-
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
61
|
-
*/
|
|
62
|
-
constructor(maxLevel = 16, probability = 0.5) {
|
|
63
|
-
this._head = new SkipListNode<K, V>(null as any, null as any, maxLevel);
|
|
64
|
-
this._level = 0;
|
|
65
|
-
this._maxLevel = maxLevel;
|
|
66
|
-
this._probability = probability;
|
|
68
|
+
get probability(): number {
|
|
69
|
+
return this._probability;
|
|
67
70
|
}
|
|
68
71
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
* @returns the level, which is a number.
|
|
72
|
-
*/
|
|
73
|
-
private randomLevel(): number {
|
|
74
|
-
let level = 1;
|
|
75
|
-
while (Math.random() < this.probability && level < this.maxLevel) {
|
|
76
|
-
level++;
|
|
77
|
-
}
|
|
78
|
-
return level;
|
|
72
|
+
set probability(value: number) {
|
|
73
|
+
this._probability = value;
|
|
79
74
|
}
|
|
80
75
|
|
|
81
76
|
/**
|
|
@@ -163,4 +158,16 @@ export class SkipList<K, V> {
|
|
|
163
158
|
|
|
164
159
|
return false;
|
|
165
160
|
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The function "randomLevel" generates a random level based on a given probability and maximum level.
|
|
164
|
+
* @returns the level, which is a number.
|
|
165
|
+
*/
|
|
166
|
+
private randomLevel(): number {
|
|
167
|
+
let level = 1;
|
|
168
|
+
while (Math.random() < this.probability && level < this.maxLevel) {
|
|
169
|
+
level++;
|
|
170
|
+
}
|
|
171
|
+
return level;
|
|
172
|
+
}
|
|
166
173
|
}
|
|
@@ -14,7 +14,7 @@ export class MatrixNTI2D<V = any> {
|
|
|
14
14
|
* given initial value or 0 if not provided.
|
|
15
15
|
* @param options - An object containing the following properties:
|
|
16
16
|
*/
|
|
17
|
-
constructor(options: {row: number; col: number; initialVal?: V}) {
|
|
17
|
+
constructor(options: { row: number; col: number; initialVal?: V }) {
|
|
18
18
|
const {row, col, initialVal} = options;
|
|
19
19
|
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
20
20
|
}
|
|
@@ -58,16 +58,6 @@ export class Matrix2D {
|
|
|
58
58
|
return this._matrix;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
/**
|
|
62
|
-
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
63
|
-
* _matrix array.
|
|
64
|
-
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
65
|
-
* the first column of the matrix.
|
|
66
|
-
*/
|
|
67
|
-
toVector(): Vector2D {
|
|
68
|
-
return new Vector2D(this._matrix[0][0], this._matrix[1][0]);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
61
|
/**
|
|
72
62
|
* The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
73
63
|
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
@@ -208,6 +198,16 @@ export class Matrix2D {
|
|
|
208
198
|
[0, 0, vector.w]
|
|
209
199
|
]);
|
|
210
200
|
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
204
|
+
* _matrix array.
|
|
205
|
+
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
206
|
+
* the first column of the matrix.
|
|
207
|
+
*/
|
|
208
|
+
toVector(): Vector2D {
|
|
209
|
+
return new Vector2D(this._matrix[0][0], this._matrix[1][0]);
|
|
210
|
+
}
|
|
211
211
|
}
|
|
212
212
|
|
|
213
213
|
export default Matrix2D;
|
|
@@ -9,7 +9,8 @@ import {DoublyLinkedList} from '../linked-list';
|
|
|
9
9
|
|
|
10
10
|
// O(n) time complexity of obtaining the value
|
|
11
11
|
// O(1) time complexity of adding at the beginning and the end
|
|
12
|
-
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
12
|
+
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
13
|
+
}
|
|
13
14
|
|
|
14
15
|
// O(1) time complexity of obtaining the value
|
|
15
16
|
// O(n) time complexity of adding at the beginning and the end
|
|
@@ -19,9 +20,9 @@ export class ObjectDeque<E = number> {
|
|
|
19
20
|
if (capacity !== undefined) this._capacity = capacity;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
private _nodes: {[key: number]: E} = {};
|
|
23
|
+
private _nodes: { [key: number]: E } = {};
|
|
23
24
|
|
|
24
|
-
get nodes(): {[p: number]: E} {
|
|
25
|
+
get nodes(): { [p: number]: E } {
|
|
25
26
|
return this._nodes;
|
|
26
27
|
}
|
|
27
28
|
|
|
@@ -156,7 +157,7 @@ export class ObjectDeque<E = number> {
|
|
|
156
157
|
return this._size <= 0;
|
|
157
158
|
}
|
|
158
159
|
|
|
159
|
-
protected _seNodes(value: {[p: number]: E}) {
|
|
160
|
+
protected _seNodes(value: { [p: number]: E }) {
|
|
160
161
|
this._nodes = value;
|
|
161
162
|
}
|
|
162
163
|
|