data-structure-typed 1.36.7 → 1.36.8

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.
@@ -8,18 +8,28 @@ import type { Comparator } from '../../types';
8
8
  import { DFSOrderPattern } from '../../types';
9
9
  export declare class Heap<E> {
10
10
  protected nodes: E[];
11
- private readonly comparator;
11
+ protected readonly comparator: Comparator<E>;
12
12
  constructor(comparator: Comparator<E>);
13
13
  /**
14
14
  * Insert an element into the heap and maintain the heap properties.
15
- * @param value - The element to be inserted.
15
+ * @param element - The element to be inserted.
16
16
  */
17
- add(value: E): Heap<E>;
17
+ add(element: E): Heap<E>;
18
+ /**
19
+ * Insert an element into the heap and maintain the heap properties.
20
+ * @param element - The element to be inserted.
21
+ */
22
+ push(element: E): Heap<E>;
18
23
  /**
19
24
  * Remove and return the top element (smallest or largest element) from the heap.
20
- * @returns The top element or null if the heap is empty.
25
+ * @returns The top element or undefined if the heap is empty.
21
26
  */
22
- poll(): E | null;
27
+ poll(): E | undefined;
28
+ /**
29
+ * Remove and return the top element (smallest or largest element) from the heap.
30
+ * @returns The top element or undefined if the heap is empty.
31
+ */
32
+ pop(): E | undefined;
23
33
  /**
24
34
  * Float operation to maintain heap properties after adding an element.
25
35
  * @param index - The index of the newly added element.
@@ -36,18 +46,18 @@ export declare class Heap<E> {
36
46
  protected fix(): void;
37
47
  /**
38
48
  * Peek at the top element of the heap without removing it.
39
- * @returns The top element or null if the heap is empty.
49
+ * @returns The top element or undefined if the heap is empty.
40
50
  */
41
- peek(): E | null;
51
+ peek(): E | undefined;
42
52
  /**
43
53
  * Get the size (number of elements) of the heap.
44
54
  */
45
55
  get size(): number;
46
56
  /**
47
57
  * Get the last element in the heap, which is not necessarily a leaf node.
48
- * @returns The last element or null if the heap is empty.
58
+ * @returns The last element or undefined if the heap is empty.
49
59
  */
50
- get leaf(): E | null;
60
+ get leaf(): E | undefined;
51
61
  /**
52
62
  * Check if the heap is empty.
53
63
  * @returns True if the heap is empty, otherwise false.
@@ -64,10 +74,10 @@ export declare class Heap<E> {
64
74
  refill(nodes: E[]): void;
65
75
  /**
66
76
  * Use a comparison function to check whether a binary heap contains a specific element.
67
- * @param value - the element to check.
77
+ * @param element - the element to check.
68
78
  * @returns Returns true if the specified element is contained; otherwise, returns false.
69
79
  */
70
- has(value: E): boolean;
80
+ has(element: E): boolean;
71
81
  /**
72
82
  * Depth-first search (DFS) method, different traversal orders can be selected。
73
83
  * @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
@@ -98,3 +108,118 @@ export declare class Heap<E> {
98
108
  */
99
109
  static heapify<E>(nodes: E[], comparator: Comparator<E>): Heap<E>;
100
110
  }
111
+ export declare class FibonacciHeapNode<E> {
112
+ element: E;
113
+ degree: number;
114
+ left?: FibonacciHeapNode<E>;
115
+ right?: FibonacciHeapNode<E>;
116
+ child?: FibonacciHeapNode<E>;
117
+ parent?: FibonacciHeapNode<E>;
118
+ marked: boolean;
119
+ constructor(element: E, degree?: number);
120
+ }
121
+ export declare class FibonacciHeap<E> {
122
+ root?: FibonacciHeapNode<E>;
123
+ protected min?: FibonacciHeapNode<E>;
124
+ size: number;
125
+ protected readonly comparator: Comparator<E>;
126
+ constructor(comparator?: Comparator<E>);
127
+ /**
128
+ * Default comparator function used by the heap.
129
+ * @param {E} a
130
+ * @param {E} b
131
+ * @protected
132
+ */
133
+ protected defaultComparator(a: E, b: E): number;
134
+ /**
135
+ * Get the size (number of elements) of the heap.
136
+ * @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
137
+ */
138
+ clear(): void;
139
+ /**
140
+ * Create a new node.
141
+ * @param element
142
+ * @protected
143
+ */
144
+ protected createNode(element: E): FibonacciHeapNode<E>;
145
+ /**
146
+ * Merge the given node with the root list.
147
+ * @param node - The node to be merged.
148
+ */
149
+ protected mergeWithRoot(node: FibonacciHeapNode<E>): void;
150
+ /**
151
+ * O(1) time operation.
152
+ * Insert an element into the heap and maintain the heap properties.
153
+ * @param element
154
+ * @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
155
+ */
156
+ add(element: E): FibonacciHeap<E>;
157
+ /**
158
+ * O(1) time operation.
159
+ * Insert an element into the heap and maintain the heap properties.
160
+ * @param element
161
+ * @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
162
+ */
163
+ push(element: E): FibonacciHeap<E>;
164
+ /**
165
+ * O(1) time operation.
166
+ * Peek at the top element of the heap without removing it.
167
+ * @returns The top element or undefined if the heap is empty.
168
+ * @protected
169
+ */
170
+ peek(): E | undefined;
171
+ /**
172
+ * O(1) time operation.
173
+ * Get the size (number of elements) of the heap.
174
+ * @param {FibonacciHeapNode<E>} head - The head of the linked list.
175
+ * @protected
176
+ * @returns FibonacciHeapNode<E>[] - An array containing the nodes of the linked list.
177
+ */
178
+ consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[];
179
+ /**
180
+ * O(log n) time operation.
181
+ * Remove and return the top element (smallest or largest element) from the heap.
182
+ * @param node - The node to be removed.
183
+ * @protected
184
+ */
185
+ protected removeFromRoot(node: FibonacciHeapNode<E>): void;
186
+ /**
187
+ * O(log n) time operation.
188
+ * Remove and return the top element (smallest or largest element) from the heap.
189
+ * @param parent
190
+ * @param node
191
+ */
192
+ mergeWithChild(parent: FibonacciHeapNode<E>, node: FibonacciHeapNode<E>): void;
193
+ /**
194
+ * O(log n) time operation.
195
+ * Remove and return the top element (smallest or largest element) from the heap.
196
+ * @param y
197
+ * @param x
198
+ * @protected
199
+ */
200
+ protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
201
+ /**
202
+ * O(log n) time operation.
203
+ * Remove and return the top element (smallest or largest element) from the heap.
204
+ * @protected
205
+ */
206
+ protected consolidate(): void;
207
+ /**
208
+ * O(log n) time operation.
209
+ * Remove and return the top element (smallest or largest element) from the heap.
210
+ * @returns The top element or undefined if the heap is empty.
211
+ */
212
+ poll(): E | undefined;
213
+ /**
214
+ * O(log n) time operation.
215
+ * Remove and return the top element (smallest or largest element) from the heap.
216
+ * @returns The top element or undefined if the heap is empty.
217
+ */
218
+ pop(): E | undefined;
219
+ /**
220
+ * O(log n) time operation.
221
+ * merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
222
+ * @param heapToMerge
223
+ */
224
+ merge(heapToMerge: FibonacciHeap<E>): void;
225
+ }
@@ -11,20 +11,27 @@ export class Heap {
11
11
  }
12
12
  /**
13
13
  * Insert an element into the heap and maintain the heap properties.
14
- * @param value - The element to be inserted.
14
+ * @param element - The element to be inserted.
15
15
  */
16
- add(value) {
17
- this.nodes.push(value);
16
+ add(element) {
17
+ return this.push(element);
18
+ }
19
+ /**
20
+ * Insert an element into the heap and maintain the heap properties.
21
+ * @param element - The element to be inserted.
22
+ */
23
+ push(element) {
24
+ this.nodes.push(element);
18
25
  this.bubbleUp(this.nodes.length - 1);
19
26
  return this;
20
27
  }
21
28
  /**
22
29
  * Remove and return the top element (smallest or largest element) from the heap.
23
- * @returns The top element or null if the heap is empty.
30
+ * @returns The top element or undefined if the heap is empty.
24
31
  */
25
32
  poll() {
26
33
  if (this.nodes.length === 0) {
27
- return null;
34
+ return undefined;
28
35
  }
29
36
  if (this.nodes.length === 1) {
30
37
  return this.nodes.pop();
@@ -34,6 +41,13 @@ export class Heap {
34
41
  this.sinkDown(0);
35
42
  return topValue;
36
43
  }
44
+ /**
45
+ * Remove and return the top element (smallest or largest element) from the heap.
46
+ * @returns The top element or undefined if the heap is empty.
47
+ */
48
+ pop() {
49
+ return this.poll();
50
+ }
37
51
  /**
38
52
  * Float operation to maintain heap properties after adding an element.
39
53
  * @param index - The index of the newly added element.
@@ -84,11 +98,11 @@ export class Heap {
84
98
  }
85
99
  /**
86
100
  * Peek at the top element of the heap without removing it.
87
- * @returns The top element or null if the heap is empty.
101
+ * @returns The top element or undefined if the heap is empty.
88
102
  */
89
103
  peek() {
90
104
  if (this.nodes.length === 0) {
91
- return null;
105
+ return undefined;
92
106
  }
93
107
  return this.nodes[0];
94
108
  }
@@ -100,11 +114,11 @@ export class Heap {
100
114
  }
101
115
  /**
102
116
  * Get the last element in the heap, which is not necessarily a leaf node.
103
- * @returns The last element or null if the heap is empty.
117
+ * @returns The last element or undefined if the heap is empty.
104
118
  */
105
119
  get leaf() {
106
120
  var _a;
107
- return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : null;
121
+ return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : undefined;
108
122
  }
109
123
  /**
110
124
  * Check if the heap is empty.
@@ -129,11 +143,11 @@ export class Heap {
129
143
  }
130
144
  /**
131
145
  * Use a comparison function to check whether a binary heap contains a specific element.
132
- * @param value - the element to check.
146
+ * @param element - the element to check.
133
147
  * @returns Returns true if the specified element is contained; otherwise, returns false.
134
148
  */
135
- has(value) {
136
- return this.nodes.includes(value);
149
+ has(element) {
150
+ return this.nodes.includes(element);
137
151
  }
138
152
  /**
139
153
  * Depth-first search (DFS) method, different traversal orders can be selected。
@@ -211,3 +225,267 @@ export class Heap {
211
225
  return binaryHeap;
212
226
  }
213
227
  }
228
+ export class FibonacciHeapNode {
229
+ constructor(element, degree = 0) {
230
+ this.element = element;
231
+ this.degree = degree;
232
+ this.marked = false;
233
+ }
234
+ }
235
+ export class FibonacciHeap {
236
+ constructor(comparator) {
237
+ this.size = 0;
238
+ this.clear();
239
+ this.comparator = comparator || this.defaultComparator;
240
+ if (typeof this.comparator !== 'function') {
241
+ throw new Error('FibonacciHeap constructor: given comparator should be a function.');
242
+ }
243
+ }
244
+ /**
245
+ * Default comparator function used by the heap.
246
+ * @param {E} a
247
+ * @param {E} b
248
+ * @protected
249
+ */
250
+ defaultComparator(a, b) {
251
+ if (a < b)
252
+ return -1;
253
+ if (a > b)
254
+ return 1;
255
+ return 0;
256
+ }
257
+ /**
258
+ * Get the size (number of elements) of the heap.
259
+ * @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
260
+ */
261
+ clear() {
262
+ this.root = undefined;
263
+ this.min = undefined;
264
+ this.size = 0;
265
+ }
266
+ /**
267
+ * Create a new node.
268
+ * @param element
269
+ * @protected
270
+ */
271
+ createNode(element) {
272
+ return new FibonacciHeapNode(element);
273
+ }
274
+ /**
275
+ * Merge the given node with the root list.
276
+ * @param node - The node to be merged.
277
+ */
278
+ mergeWithRoot(node) {
279
+ if (!this.root) {
280
+ this.root = node;
281
+ }
282
+ else {
283
+ node.right = this.root.right;
284
+ node.left = this.root;
285
+ this.root.right.left = node;
286
+ this.root.right = node;
287
+ }
288
+ }
289
+ /**
290
+ * O(1) time operation.
291
+ * Insert an element into the heap and maintain the heap properties.
292
+ * @param element
293
+ * @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
294
+ */
295
+ add(element) {
296
+ return this.push(element);
297
+ }
298
+ /**
299
+ * O(1) time operation.
300
+ * Insert an element into the heap and maintain the heap properties.
301
+ * @param element
302
+ * @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
303
+ */
304
+ push(element) {
305
+ const node = this.createNode(element);
306
+ node.left = node;
307
+ node.right = node;
308
+ this.mergeWithRoot(node);
309
+ if (!this.min || this.comparator(node.element, this.min.element) <= 0) {
310
+ this.min = node;
311
+ }
312
+ this.size++;
313
+ return this;
314
+ }
315
+ /**
316
+ * O(1) time operation.
317
+ * Peek at the top element of the heap without removing it.
318
+ * @returns The top element or undefined if the heap is empty.
319
+ * @protected
320
+ */
321
+ peek() {
322
+ return this.min ? this.min.element : undefined;
323
+ }
324
+ /**
325
+ * O(1) time operation.
326
+ * Get the size (number of elements) of the heap.
327
+ * @param {FibonacciHeapNode<E>} head - The head of the linked list.
328
+ * @protected
329
+ * @returns FibonacciHeapNode<E>[] - An array containing the nodes of the linked list.
330
+ */
331
+ consumeLinkedList(head) {
332
+ const nodes = [];
333
+ if (!head)
334
+ return nodes;
335
+ let node = head;
336
+ let flag = false;
337
+ while (true) {
338
+ if (node === head && flag)
339
+ break;
340
+ else if (node === head)
341
+ flag = true;
342
+ if (node) {
343
+ nodes.push(node);
344
+ node = node.right;
345
+ }
346
+ }
347
+ return nodes;
348
+ }
349
+ /**
350
+ * O(log n) time operation.
351
+ * Remove and return the top element (smallest or largest element) from the heap.
352
+ * @param node - The node to be removed.
353
+ * @protected
354
+ */
355
+ removeFromRoot(node) {
356
+ if (this.root === node)
357
+ this.root = node.right;
358
+ if (node.left)
359
+ node.left.right = node.right;
360
+ if (node.right)
361
+ node.right.left = node.left;
362
+ }
363
+ /**
364
+ * O(log n) time operation.
365
+ * Remove and return the top element (smallest or largest element) from the heap.
366
+ * @param parent
367
+ * @param node
368
+ */
369
+ mergeWithChild(parent, node) {
370
+ if (!parent.child) {
371
+ parent.child = node;
372
+ }
373
+ else {
374
+ node.right = parent.child.right;
375
+ node.left = parent.child;
376
+ parent.child.right.left = node;
377
+ parent.child.right = node;
378
+ }
379
+ }
380
+ /**
381
+ * O(log n) time operation.
382
+ * Remove and return the top element (smallest or largest element) from the heap.
383
+ * @param y
384
+ * @param x
385
+ * @protected
386
+ */
387
+ link(y, x) {
388
+ this.removeFromRoot(y);
389
+ y.left = y;
390
+ y.right = y;
391
+ this.mergeWithChild(x, y);
392
+ x.degree++;
393
+ y.parent = x;
394
+ }
395
+ /**
396
+ * O(log n) time operation.
397
+ * Remove and return the top element (smallest or largest element) from the heap.
398
+ * @protected
399
+ */
400
+ consolidate() {
401
+ const A = new Array(this.size);
402
+ const nodes = this.consumeLinkedList(this.root);
403
+ let x, y, d, t;
404
+ for (const node of nodes) {
405
+ x = node;
406
+ d = x.degree;
407
+ while (A[d]) {
408
+ y = A[d];
409
+ if (this.comparator(x.element, y.element) > 0) {
410
+ t = x;
411
+ x = y;
412
+ y = t;
413
+ }
414
+ this.link(y, x);
415
+ A[d] = undefined;
416
+ d++;
417
+ }
418
+ A[d] = x;
419
+ }
420
+ for (let i = 0; i < this.size; i++) {
421
+ if (A[i] && this.comparator(A[i].element, this.min.element) <= 0) {
422
+ this.min = A[i];
423
+ }
424
+ }
425
+ }
426
+ /**
427
+ * O(log n) time operation.
428
+ * Remove and return the top element (smallest or largest element) from the heap.
429
+ * @returns The top element or undefined if the heap is empty.
430
+ */
431
+ poll() {
432
+ return this.pop();
433
+ }
434
+ /**
435
+ * O(log n) time operation.
436
+ * Remove and return the top element (smallest or largest element) from the heap.
437
+ * @returns The top element or undefined if the heap is empty.
438
+ */
439
+ pop() {
440
+ if (this.size === 0)
441
+ return undefined;
442
+ const z = this.min;
443
+ if (z.child) {
444
+ const nodes = this.consumeLinkedList(z.child);
445
+ for (const node of nodes) {
446
+ this.mergeWithRoot(node);
447
+ node.parent = undefined;
448
+ }
449
+ }
450
+ this.removeFromRoot(z);
451
+ if (z === z.right) {
452
+ this.min = undefined;
453
+ this.root = undefined;
454
+ }
455
+ else {
456
+ this.min = z.right;
457
+ this.consolidate();
458
+ }
459
+ this.size--;
460
+ return z.element;
461
+ }
462
+ /**
463
+ * O(log n) time operation.
464
+ * merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
465
+ * @param heapToMerge
466
+ */
467
+ merge(heapToMerge) {
468
+ if (heapToMerge.size === 0) {
469
+ return; // Nothing to merge
470
+ }
471
+ // Merge the root lists of the two heaps
472
+ if (this.root && heapToMerge.root) {
473
+ const thisRoot = this.root;
474
+ const otherRoot = heapToMerge.root;
475
+ const thisRootRight = thisRoot.right;
476
+ const otherRootLeft = otherRoot.left;
477
+ thisRoot.right = otherRoot;
478
+ otherRoot.left = thisRoot;
479
+ thisRootRight.left = otherRootLeft;
480
+ otherRootLeft.right = thisRootRight;
481
+ }
482
+ // Update the minimum node
483
+ if (!this.min || (heapToMerge.min && this.comparator(heapToMerge.min.element, this.min.element) < 0)) {
484
+ this.min = heapToMerge.min;
485
+ }
486
+ // Update the size
487
+ this.size += heapToMerge.size;
488
+ // Clear the heap that was merged
489
+ heapToMerge.clear();
490
+ }
491
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.36.7",
3
+ "version": "1.36.8",
4
4
  "description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
5
5
  "main": "dist/index.js",
6
6
  "module": "lib/index.js",
@@ -33,7 +33,8 @@
33
33
  "check:deps": "dependency-cruiser src",
34
34
  "changelog": "auto-changelog",
35
35
  "coverage:badge": "istanbul-badges-readme",
36
- "ci": "env && npm run lint && npm run build && npm run update:individuals && npm run test && git fetch --tags && npm run changelog"
36
+ "ci": "env && npm run lint && npm run build && npm run update:individuals && npm run test && git fetch --tags && npm run changelog",
37
+ "publish:all": "npm run ci && npm publish && sh /scripts/publish_all_subs.sh && sh /scripts/publish_docs.sh"
37
38
  },
38
39
  "repository": {
39
40
  "type": "git",