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
|
@@ -12,6 +12,32 @@ class Heap {
|
|
|
12
12
|
this.nodes = [];
|
|
13
13
|
this.comparator = comparator;
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Get the size (number of elements) of the heap.
|
|
17
|
+
*/
|
|
18
|
+
get size() {
|
|
19
|
+
return this.nodes.length;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
23
|
+
* @returns The last element or undefined if the heap is empty.
|
|
24
|
+
*/
|
|
25
|
+
get leaf() {
|
|
26
|
+
var _a;
|
|
27
|
+
return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : undefined;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
31
|
+
* @param nodes
|
|
32
|
+
* @param comparator - Comparison function.
|
|
33
|
+
* @returns A new Heap instance.
|
|
34
|
+
*/
|
|
35
|
+
static heapify(nodes, comparator) {
|
|
36
|
+
const binaryHeap = new Heap(comparator);
|
|
37
|
+
binaryHeap.nodes = [...nodes];
|
|
38
|
+
binaryHeap.fix(); // Fix heap properties
|
|
39
|
+
return binaryHeap;
|
|
40
|
+
}
|
|
15
41
|
/**
|
|
16
42
|
* Insert an element into the heap and maintain the heap properties.
|
|
17
43
|
* @param element - The element to be inserted.
|
|
@@ -51,54 +77,6 @@ class Heap {
|
|
|
51
77
|
pop() {
|
|
52
78
|
return this.poll();
|
|
53
79
|
}
|
|
54
|
-
/**
|
|
55
|
-
* Float operation to maintain heap properties after adding an element.
|
|
56
|
-
* @param index - The index of the newly added element.
|
|
57
|
-
*/
|
|
58
|
-
bubbleUp(index) {
|
|
59
|
-
const element = this.nodes[index];
|
|
60
|
-
while (index > 0) {
|
|
61
|
-
const parentIndex = Math.floor((index - 1) / 2);
|
|
62
|
-
const parent = this.nodes[parentIndex];
|
|
63
|
-
if (this.comparator(element, parent) < 0) {
|
|
64
|
-
this.nodes[index] = parent;
|
|
65
|
-
this.nodes[parentIndex] = element;
|
|
66
|
-
index = parentIndex;
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Sinking operation to maintain heap properties after removing the top element.
|
|
75
|
-
* @param index - The index from which to start sinking.
|
|
76
|
-
*/
|
|
77
|
-
sinkDown(index) {
|
|
78
|
-
const leftChildIndex = 2 * index + 1;
|
|
79
|
-
const rightChildIndex = 2 * index + 2;
|
|
80
|
-
const length = this.nodes.length;
|
|
81
|
-
let targetIndex = index;
|
|
82
|
-
if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
|
|
83
|
-
targetIndex = leftChildIndex;
|
|
84
|
-
}
|
|
85
|
-
if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
|
|
86
|
-
targetIndex = rightChildIndex;
|
|
87
|
-
}
|
|
88
|
-
if (targetIndex !== index) {
|
|
89
|
-
const temp = this.nodes[index];
|
|
90
|
-
this.nodes[index] = this.nodes[targetIndex];
|
|
91
|
-
this.nodes[targetIndex] = temp;
|
|
92
|
-
this.sinkDown(targetIndex);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Fix the entire heap to maintain heap properties.
|
|
97
|
-
*/
|
|
98
|
-
fix() {
|
|
99
|
-
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
100
|
-
this.sinkDown(i);
|
|
101
|
-
}
|
|
102
80
|
/**
|
|
103
81
|
* Peek at the top element of the heap without removing it.
|
|
104
82
|
* @returns The top element or undefined if the heap is empty.
|
|
@@ -109,20 +87,6 @@ class Heap {
|
|
|
109
87
|
}
|
|
110
88
|
return this.nodes[0];
|
|
111
89
|
}
|
|
112
|
-
/**
|
|
113
|
-
* Get the size (number of elements) of the heap.
|
|
114
|
-
*/
|
|
115
|
-
get size() {
|
|
116
|
-
return this.nodes.length;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
120
|
-
* @returns The last element or undefined if the heap is empty.
|
|
121
|
-
*/
|
|
122
|
-
get leaf() {
|
|
123
|
-
var _a;
|
|
124
|
-
return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : undefined;
|
|
125
|
-
}
|
|
126
90
|
/**
|
|
127
91
|
* Check if the heap is empty.
|
|
128
92
|
* @returns True if the heap is empty, otherwise false.
|
|
@@ -216,16 +180,52 @@ class Heap {
|
|
|
216
180
|
return visitedNode;
|
|
217
181
|
}
|
|
218
182
|
/**
|
|
219
|
-
*
|
|
220
|
-
* @param
|
|
221
|
-
* @param comparator - Comparison function.
|
|
222
|
-
* @returns A new Heap instance.
|
|
183
|
+
* Float operation to maintain heap properties after adding an element.
|
|
184
|
+
* @param index - The index of the newly added element.
|
|
223
185
|
*/
|
|
224
|
-
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
186
|
+
bubbleUp(index) {
|
|
187
|
+
const element = this.nodes[index];
|
|
188
|
+
while (index > 0) {
|
|
189
|
+
const parentIndex = Math.floor((index - 1) / 2);
|
|
190
|
+
const parent = this.nodes[parentIndex];
|
|
191
|
+
if (this.comparator(element, parent) < 0) {
|
|
192
|
+
this.nodes[index] = parent;
|
|
193
|
+
this.nodes[parentIndex] = element;
|
|
194
|
+
index = parentIndex;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
203
|
+
* @param index - The index from which to start sinking.
|
|
204
|
+
*/
|
|
205
|
+
sinkDown(index) {
|
|
206
|
+
const leftChildIndex = 2 * index + 1;
|
|
207
|
+
const rightChildIndex = 2 * index + 2;
|
|
208
|
+
const length = this.nodes.length;
|
|
209
|
+
let targetIndex = index;
|
|
210
|
+
if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
|
|
211
|
+
targetIndex = leftChildIndex;
|
|
212
|
+
}
|
|
213
|
+
if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
|
|
214
|
+
targetIndex = rightChildIndex;
|
|
215
|
+
}
|
|
216
|
+
if (targetIndex !== index) {
|
|
217
|
+
const temp = this.nodes[index];
|
|
218
|
+
this.nodes[index] = this.nodes[targetIndex];
|
|
219
|
+
this.nodes[targetIndex] = temp;
|
|
220
|
+
this.sinkDown(targetIndex);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Fix the entire heap to maintain heap properties.
|
|
225
|
+
*/
|
|
226
|
+
fix() {
|
|
227
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
228
|
+
this.sinkDown(i);
|
|
229
229
|
}
|
|
230
230
|
}
|
|
231
231
|
exports.Heap = Heap;
|
|
@@ -246,19 +246,6 @@ class FibonacciHeap {
|
|
|
246
246
|
throw new Error('FibonacciHeap constructor: given comparator should be a function.');
|
|
247
247
|
}
|
|
248
248
|
}
|
|
249
|
-
/**
|
|
250
|
-
* Default comparator function used by the heap.
|
|
251
|
-
* @param {E} a
|
|
252
|
-
* @param {E} b
|
|
253
|
-
* @protected
|
|
254
|
-
*/
|
|
255
|
-
defaultComparator(a, b) {
|
|
256
|
-
if (a < b)
|
|
257
|
-
return -1;
|
|
258
|
-
if (a > b)
|
|
259
|
-
return 1;
|
|
260
|
-
return 0;
|
|
261
|
-
}
|
|
262
249
|
/**
|
|
263
250
|
* Get the size (number of elements) of the heap.
|
|
264
251
|
* @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
|
|
@@ -268,29 +255,6 @@ class FibonacciHeap {
|
|
|
268
255
|
this.min = undefined;
|
|
269
256
|
this.size = 0;
|
|
270
257
|
}
|
|
271
|
-
/**
|
|
272
|
-
* Create a new node.
|
|
273
|
-
* @param element
|
|
274
|
-
* @protected
|
|
275
|
-
*/
|
|
276
|
-
createNode(element) {
|
|
277
|
-
return new FibonacciHeapNode(element);
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Merge the given node with the root list.
|
|
281
|
-
* @param node - The node to be merged.
|
|
282
|
-
*/
|
|
283
|
-
mergeWithRoot(node) {
|
|
284
|
-
if (!this.root) {
|
|
285
|
-
this.root = node;
|
|
286
|
-
}
|
|
287
|
-
else {
|
|
288
|
-
node.right = this.root.right;
|
|
289
|
-
node.left = this.root;
|
|
290
|
-
this.root.right.left = node;
|
|
291
|
-
this.root.right = node;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
258
|
/**
|
|
295
259
|
* O(1) time operation.
|
|
296
260
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -351,20 +315,6 @@ class FibonacciHeap {
|
|
|
351
315
|
}
|
|
352
316
|
return nodes;
|
|
353
317
|
}
|
|
354
|
-
/**
|
|
355
|
-
* O(log n) time operation.
|
|
356
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
357
|
-
* @param node - The node to be removed.
|
|
358
|
-
* @protected
|
|
359
|
-
*/
|
|
360
|
-
removeFromRoot(node) {
|
|
361
|
-
if (this.root === node)
|
|
362
|
-
this.root = node.right;
|
|
363
|
-
if (node.left)
|
|
364
|
-
node.left.right = node.right;
|
|
365
|
-
if (node.right)
|
|
366
|
-
node.right.left = node.left;
|
|
367
|
-
}
|
|
368
318
|
/**
|
|
369
319
|
* O(log n) time operation.
|
|
370
320
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -382,52 +332,6 @@ class FibonacciHeap {
|
|
|
382
332
|
parent.child.right = node;
|
|
383
333
|
}
|
|
384
334
|
}
|
|
385
|
-
/**
|
|
386
|
-
* O(log n) time operation.
|
|
387
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
388
|
-
* @param y
|
|
389
|
-
* @param x
|
|
390
|
-
* @protected
|
|
391
|
-
*/
|
|
392
|
-
link(y, x) {
|
|
393
|
-
this.removeFromRoot(y);
|
|
394
|
-
y.left = y;
|
|
395
|
-
y.right = y;
|
|
396
|
-
this.mergeWithChild(x, y);
|
|
397
|
-
x.degree++;
|
|
398
|
-
y.parent = x;
|
|
399
|
-
}
|
|
400
|
-
/**
|
|
401
|
-
* O(log n) time operation.
|
|
402
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
403
|
-
* @protected
|
|
404
|
-
*/
|
|
405
|
-
consolidate() {
|
|
406
|
-
const A = new Array(this.size);
|
|
407
|
-
const nodes = this.consumeLinkedList(this.root);
|
|
408
|
-
let x, y, d, t;
|
|
409
|
-
for (const node of nodes) {
|
|
410
|
-
x = node;
|
|
411
|
-
d = x.degree;
|
|
412
|
-
while (A[d]) {
|
|
413
|
-
y = A[d];
|
|
414
|
-
if (this.comparator(x.element, y.element) > 0) {
|
|
415
|
-
t = x;
|
|
416
|
-
x = y;
|
|
417
|
-
y = t;
|
|
418
|
-
}
|
|
419
|
-
this.link(y, x);
|
|
420
|
-
A[d] = undefined;
|
|
421
|
-
d++;
|
|
422
|
-
}
|
|
423
|
-
A[d] = x;
|
|
424
|
-
}
|
|
425
|
-
for (let i = 0; i < this.size; i++) {
|
|
426
|
-
if (A[i] && this.comparator(A[i].element, this.min.element) <= 0) {
|
|
427
|
-
this.min = A[i];
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
335
|
/**
|
|
432
336
|
* O(log n) time operation.
|
|
433
337
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -493,5 +397,101 @@ class FibonacciHeap {
|
|
|
493
397
|
// Clear the heap that was merged
|
|
494
398
|
heapToMerge.clear();
|
|
495
399
|
}
|
|
400
|
+
/**
|
|
401
|
+
* Default comparator function used by the heap.
|
|
402
|
+
* @param {E} a
|
|
403
|
+
* @param {E} b
|
|
404
|
+
* @protected
|
|
405
|
+
*/
|
|
406
|
+
defaultComparator(a, b) {
|
|
407
|
+
if (a < b)
|
|
408
|
+
return -1;
|
|
409
|
+
if (a > b)
|
|
410
|
+
return 1;
|
|
411
|
+
return 0;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Create a new node.
|
|
415
|
+
* @param element
|
|
416
|
+
* @protected
|
|
417
|
+
*/
|
|
418
|
+
createNode(element) {
|
|
419
|
+
return new FibonacciHeapNode(element);
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Merge the given node with the root list.
|
|
423
|
+
* @param node - The node to be merged.
|
|
424
|
+
*/
|
|
425
|
+
mergeWithRoot(node) {
|
|
426
|
+
if (!this.root) {
|
|
427
|
+
this.root = node;
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
node.right = this.root.right;
|
|
431
|
+
node.left = this.root;
|
|
432
|
+
this.root.right.left = node;
|
|
433
|
+
this.root.right = node;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* O(log n) time operation.
|
|
438
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
439
|
+
* @param node - The node to be removed.
|
|
440
|
+
* @protected
|
|
441
|
+
*/
|
|
442
|
+
removeFromRoot(node) {
|
|
443
|
+
if (this.root === node)
|
|
444
|
+
this.root = node.right;
|
|
445
|
+
if (node.left)
|
|
446
|
+
node.left.right = node.right;
|
|
447
|
+
if (node.right)
|
|
448
|
+
node.right.left = node.left;
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* O(log n) time operation.
|
|
452
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
453
|
+
* @param y
|
|
454
|
+
* @param x
|
|
455
|
+
* @protected
|
|
456
|
+
*/
|
|
457
|
+
link(y, x) {
|
|
458
|
+
this.removeFromRoot(y);
|
|
459
|
+
y.left = y;
|
|
460
|
+
y.right = y;
|
|
461
|
+
this.mergeWithChild(x, y);
|
|
462
|
+
x.degree++;
|
|
463
|
+
y.parent = x;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* O(log n) time operation.
|
|
467
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
468
|
+
* @protected
|
|
469
|
+
*/
|
|
470
|
+
consolidate() {
|
|
471
|
+
const A = new Array(this.size);
|
|
472
|
+
const nodes = this.consumeLinkedList(this.root);
|
|
473
|
+
let x, y, d, t;
|
|
474
|
+
for (const node of nodes) {
|
|
475
|
+
x = node;
|
|
476
|
+
d = x.degree;
|
|
477
|
+
while (A[d]) {
|
|
478
|
+
y = A[d];
|
|
479
|
+
if (this.comparator(x.element, y.element) > 0) {
|
|
480
|
+
t = x;
|
|
481
|
+
x = y;
|
|
482
|
+
y = t;
|
|
483
|
+
}
|
|
484
|
+
this.link(y, x);
|
|
485
|
+
A[d] = undefined;
|
|
486
|
+
d++;
|
|
487
|
+
}
|
|
488
|
+
A[d] = x;
|
|
489
|
+
}
|
|
490
|
+
for (let i = 0; i < this.size; i++) {
|
|
491
|
+
if (A[i] && this.comparator(A[i].element, this.min.element) <= 0) {
|
|
492
|
+
this.min = A[i];
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
496
|
}
|
|
497
497
|
exports.FibonacciHeap = FibonacciHeap;
|
|
@@ -35,6 +35,7 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
35
35
|
set tail(value: DoublyLinkedListNode<E> | null);
|
|
36
36
|
private _length;
|
|
37
37
|
get length(): number;
|
|
38
|
+
get size(): number;
|
|
38
39
|
/**
|
|
39
40
|
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
40
41
|
* given array.
|
|
@@ -98,7 +99,6 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
98
99
|
* @returns The method `peekLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
|
|
99
100
|
*/
|
|
100
101
|
peekLast(): E | undefined;
|
|
101
|
-
get size(): number;
|
|
102
102
|
/**
|
|
103
103
|
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
|
|
104
104
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -63,6 +63,9 @@ class DoublyLinkedList {
|
|
|
63
63
|
get length() {
|
|
64
64
|
return this._length;
|
|
65
65
|
}
|
|
66
|
+
get size() {
|
|
67
|
+
return this.length;
|
|
68
|
+
}
|
|
66
69
|
/**
|
|
67
70
|
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
68
71
|
* given array.
|
|
@@ -198,9 +201,6 @@ class DoublyLinkedList {
|
|
|
198
201
|
var _a;
|
|
199
202
|
return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.val;
|
|
200
203
|
}
|
|
201
|
-
get size() {
|
|
202
|
-
return this.length;
|
|
203
|
-
}
|
|
204
204
|
/**
|
|
205
205
|
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
|
|
206
206
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -12,18 +12,6 @@ export declare class SkipListNode<K, V> {
|
|
|
12
12
|
constructor(key: K, value: V, level: number);
|
|
13
13
|
}
|
|
14
14
|
export declare class SkipList<K, V> {
|
|
15
|
-
get probability(): number;
|
|
16
|
-
set probability(value: number);
|
|
17
|
-
get maxLevel(): number;
|
|
18
|
-
set maxLevel(value: number);
|
|
19
|
-
get level(): number;
|
|
20
|
-
set level(value: number);
|
|
21
|
-
get head(): SkipListNode<K, V>;
|
|
22
|
-
set head(value: SkipListNode<K, V>);
|
|
23
|
-
private _head;
|
|
24
|
-
private _level;
|
|
25
|
-
private _maxLevel;
|
|
26
|
-
private _probability;
|
|
27
15
|
/**
|
|
28
16
|
* The constructor initializes a SkipList with a specified maximum level and probability.
|
|
29
17
|
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines
|
|
@@ -32,11 +20,18 @@ export declare class SkipList<K, V> {
|
|
|
32
20
|
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
33
21
|
*/
|
|
34
22
|
constructor(maxLevel?: number, probability?: number);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
23
|
+
private _head;
|
|
24
|
+
get head(): SkipListNode<K, V>;
|
|
25
|
+
set head(value: SkipListNode<K, V>);
|
|
26
|
+
private _level;
|
|
27
|
+
get level(): number;
|
|
28
|
+
set level(value: number);
|
|
29
|
+
private _maxLevel;
|
|
30
|
+
get maxLevel(): number;
|
|
31
|
+
set maxLevel(value: number);
|
|
32
|
+
private _probability;
|
|
33
|
+
get probability(): number;
|
|
34
|
+
set probability(value: number);
|
|
40
35
|
/**
|
|
41
36
|
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
42
37
|
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
@@ -58,4 +53,9 @@ export declare class SkipList<K, V> {
|
|
|
58
53
|
* skip list, and `false` if the key was not found in the skip list.
|
|
59
54
|
*/
|
|
60
55
|
delete(key: K): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* The function "randomLevel" generates a random level based on a given probability and maximum level.
|
|
58
|
+
* @returns the level, which is a number.
|
|
59
|
+
*/
|
|
60
|
+
private randomLevel;
|
|
61
61
|
}
|
|
@@ -17,30 +17,6 @@ class SkipListNode {
|
|
|
17
17
|
}
|
|
18
18
|
exports.SkipListNode = SkipListNode;
|
|
19
19
|
class SkipList {
|
|
20
|
-
get probability() {
|
|
21
|
-
return this._probability;
|
|
22
|
-
}
|
|
23
|
-
set probability(value) {
|
|
24
|
-
this._probability = value;
|
|
25
|
-
}
|
|
26
|
-
get maxLevel() {
|
|
27
|
-
return this._maxLevel;
|
|
28
|
-
}
|
|
29
|
-
set maxLevel(value) {
|
|
30
|
-
this._maxLevel = value;
|
|
31
|
-
}
|
|
32
|
-
get level() {
|
|
33
|
-
return this._level;
|
|
34
|
-
}
|
|
35
|
-
set level(value) {
|
|
36
|
-
this._level = value;
|
|
37
|
-
}
|
|
38
|
-
get head() {
|
|
39
|
-
return this._head;
|
|
40
|
-
}
|
|
41
|
-
set head(value) {
|
|
42
|
-
this._head = value;
|
|
43
|
-
}
|
|
44
20
|
/**
|
|
45
21
|
* The constructor initializes a SkipList with a specified maximum level and probability.
|
|
46
22
|
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines
|
|
@@ -54,16 +30,29 @@ class SkipList {
|
|
|
54
30
|
this._maxLevel = maxLevel;
|
|
55
31
|
this._probability = probability;
|
|
56
32
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
33
|
+
get head() {
|
|
34
|
+
return this._head;
|
|
35
|
+
}
|
|
36
|
+
set head(value) {
|
|
37
|
+
this._head = value;
|
|
38
|
+
}
|
|
39
|
+
get level() {
|
|
40
|
+
return this._level;
|
|
41
|
+
}
|
|
42
|
+
set level(value) {
|
|
43
|
+
this._level = value;
|
|
44
|
+
}
|
|
45
|
+
get maxLevel() {
|
|
46
|
+
return this._maxLevel;
|
|
47
|
+
}
|
|
48
|
+
set maxLevel(value) {
|
|
49
|
+
this._maxLevel = value;
|
|
50
|
+
}
|
|
51
|
+
get probability() {
|
|
52
|
+
return this._probability;
|
|
53
|
+
}
|
|
54
|
+
set probability(value) {
|
|
55
|
+
this._probability = value;
|
|
67
56
|
}
|
|
68
57
|
/**
|
|
69
58
|
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
@@ -138,5 +127,16 @@ class SkipList {
|
|
|
138
127
|
}
|
|
139
128
|
return false;
|
|
140
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* The function "randomLevel" generates a random level based on a given probability and maximum level.
|
|
132
|
+
* @returns the level, which is a number.
|
|
133
|
+
*/
|
|
134
|
+
randomLevel() {
|
|
135
|
+
let level = 1;
|
|
136
|
+
while (Math.random() < this.probability && level < this.maxLevel) {
|
|
137
|
+
level++;
|
|
138
|
+
}
|
|
139
|
+
return level;
|
|
140
|
+
}
|
|
141
141
|
}
|
|
142
142
|
exports.SkipList = SkipList;
|
|
@@ -31,13 +31,6 @@ export declare class Matrix2D {
|
|
|
31
31
|
* array of numbers.
|
|
32
32
|
*/
|
|
33
33
|
get m(): number[][];
|
|
34
|
-
/**
|
|
35
|
-
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
36
|
-
* _matrix array.
|
|
37
|
-
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
38
|
-
* the first column of the matrix.
|
|
39
|
-
*/
|
|
40
|
-
toVector(): Vector2D;
|
|
41
34
|
/**
|
|
42
35
|
* The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
43
36
|
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
@@ -104,5 +97,12 @@ export declare class Matrix2D {
|
|
|
104
97
|
* @returns The method is returning a new instance of the Matrix2D class.
|
|
105
98
|
*/
|
|
106
99
|
static translate(vector: Vector2D): Matrix2D;
|
|
100
|
+
/**
|
|
101
|
+
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
102
|
+
* _matrix array.
|
|
103
|
+
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
104
|
+
* the first column of the matrix.
|
|
105
|
+
*/
|
|
106
|
+
toVector(): Vector2D;
|
|
107
107
|
}
|
|
108
108
|
export default Matrix2D;
|
|
@@ -59,15 +59,6 @@ class Matrix2D {
|
|
|
59
59
|
get m() {
|
|
60
60
|
return this._matrix;
|
|
61
61
|
}
|
|
62
|
-
/**
|
|
63
|
-
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
64
|
-
* _matrix array.
|
|
65
|
-
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
66
|
-
* the first column of the matrix.
|
|
67
|
-
*/
|
|
68
|
-
toVector() {
|
|
69
|
-
return new vector2d_1.default(this._matrix[0][0], this._matrix[1][0]);
|
|
70
|
-
}
|
|
71
62
|
/**
|
|
72
63
|
* The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
73
64
|
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
@@ -198,6 +189,15 @@ class Matrix2D {
|
|
|
198
189
|
[0, 0, vector.w]
|
|
199
190
|
]);
|
|
200
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
194
|
+
* _matrix array.
|
|
195
|
+
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
196
|
+
* the first column of the matrix.
|
|
197
|
+
*/
|
|
198
|
+
toVector() {
|
|
199
|
+
return new vector2d_1.default(this._matrix[0][0], this._matrix[1][0]);
|
|
200
|
+
}
|
|
201
201
|
}
|
|
202
202
|
exports.Matrix2D = Matrix2D;
|
|
203
203
|
exports.default = Matrix2D;
|
|
@@ -25,11 +25,11 @@ export declare class TrieNode {
|
|
|
25
25
|
* Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
|
|
26
26
|
*/
|
|
27
27
|
export declare class Trie {
|
|
28
|
+
private readonly _caseSensitive;
|
|
28
29
|
constructor(words?: string[], caseSensitive?: boolean);
|
|
29
30
|
protected _root: TrieNode;
|
|
30
31
|
get root(): TrieNode;
|
|
31
32
|
set root(v: TrieNode);
|
|
32
|
-
private readonly _caseSensitive;
|
|
33
33
|
/**
|
|
34
34
|
* Add a word to the Trie structure.
|
|
35
35
|
* @param {string} word - The word to add.
|
|
@@ -42,7 +42,6 @@ export declare class Trie {
|
|
|
42
42
|
* @returns {boolean} True if the word is present in the Trie.
|
|
43
43
|
*/
|
|
44
44
|
has(word: string): boolean;
|
|
45
|
-
private _caseProcess;
|
|
46
45
|
/**
|
|
47
46
|
* Remove a word from the Trie structure.
|
|
48
47
|
* @param{string} word - The word to delete.
|
|
@@ -81,4 +80,5 @@ export declare class Trie {
|
|
|
81
80
|
* @returns {string[]} an array of strings.
|
|
82
81
|
*/
|
|
83
82
|
getWords(prefix?: string, max?: number): string[];
|
|
83
|
+
private _caseProcess;
|
|
84
84
|
}
|
|
@@ -92,12 +92,6 @@ class Trie {
|
|
|
92
92
|
}
|
|
93
93
|
return cur.isEnd;
|
|
94
94
|
}
|
|
95
|
-
_caseProcess(str) {
|
|
96
|
-
if (!this._caseSensitive) {
|
|
97
|
-
str = str.toLowerCase(); // Convert str to lowercase if case-insensitive
|
|
98
|
-
}
|
|
99
|
-
return str;
|
|
100
|
-
}
|
|
101
95
|
/**
|
|
102
96
|
* Remove a word from the Trie structure.
|
|
103
97
|
* @param{string} word - The word to delete.
|
|
@@ -264,5 +258,11 @@ class Trie {
|
|
|
264
258
|
dfs(startNode, prefix);
|
|
265
259
|
return words;
|
|
266
260
|
}
|
|
261
|
+
_caseProcess(str) {
|
|
262
|
+
if (!this._caseSensitive) {
|
|
263
|
+
str = str.toLowerCase(); // Convert str to lowercase if case-insensitive
|
|
264
|
+
}
|
|
265
|
+
return str;
|
|
266
|
+
}
|
|
267
267
|
}
|
|
268
268
|
exports.Trie = Trie;
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
export * from 'data-
|
|
9
|
-
export * from '
|
|
10
|
-
export * from '
|
|
8
|
+
export * from './data-structures/heap';
|
|
9
|
+
export * from './types/data-structures/heap';
|
|
10
|
+
export * from './types/helpers';
|