red-black-tree-typed 1.47.3
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/.eslintrc.js +61 -0
- package/.prettierignore +6 -0
- package/.prettierrc.js +16 -0
- package/LICENSE +21 -0
- package/README.md +713 -0
- package/coverage/clover.xml +13 -0
- package/coverage/coverage-final.json +96 -0
- package/coverage/coverage-summary.json +60 -0
- package/coverage/lcov-report/base.css +403 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +119 -0
- package/coverage/lcov-report/index.ts.html +109 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +206 -0
- package/coverage/lcov.info +14 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +173 -0
- package/dist/data-structures/binary-tree/avl-tree.js +429 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +515 -0
- package/dist/data-structures/binary-tree/binary-tree.js +1665 -0
- package/dist/data-structures/binary-tree/bst.d.ts +253 -0
- package/dist/data-structures/binary-tree/bst.js +651 -0
- package/dist/data-structures/binary-tree/index.d.ts +7 -0
- package/dist/data-structures/binary-tree/index.js +23 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +169 -0
- package/dist/data-structures/binary-tree/rb-tree.js +524 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
- package/dist/data-structures/binary-tree/segment-tree.js +180 -0
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +162 -0
- package/dist/data-structures/binary-tree/tree-multimap.js +407 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +450 -0
- package/dist/data-structures/graph/abstract-graph.js +1047 -0
- package/dist/data-structures/graph/directed-graph.d.ts +320 -0
- package/dist/data-structures/graph/directed-graph.js +530 -0
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/index.js +20 -0
- package/dist/data-structures/graph/map-graph.d.ts +73 -0
- package/dist/data-structures/graph/map-graph.js +93 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +183 -0
- package/dist/data-structures/graph/undirected-graph.js +302 -0
- package/dist/data-structures/hash/hash-map.d.ts +186 -0
- package/dist/data-structures/hash/hash-map.js +367 -0
- package/dist/data-structures/hash/hash-table.d.ts +103 -0
- package/dist/data-structures/hash/hash-table.js +236 -0
- package/dist/data-structures/hash/index.d.ts +2 -0
- package/dist/data-structures/hash/index.js +18 -0
- package/dist/data-structures/heap/heap.d.ts +410 -0
- package/dist/data-structures/heap/heap.js +697 -0
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/index.js +19 -0
- package/dist/data-structures/heap/max-heap.d.ts +15 -0
- package/dist/data-structures/heap/max-heap.js +26 -0
- package/dist/data-structures/heap/min-heap.d.ts +15 -0
- package/dist/data-structures/heap/min-heap.js +26 -0
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/index.js +27 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +456 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +772 -0
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/index.js +19 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +414 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +715 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +144 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +251 -0
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/index.js +20 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +28 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
- package/dist/data-structures/matrix/matrix2d.js +199 -0
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +106 -0
- package/dist/data-structures/matrix/vector2d.d.ts +200 -0
- package/dist/data-structures/matrix/vector2d.js +290 -0
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/index.js +19 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/priority-queue.js +17 -0
- package/dist/data-structures/queue/deque.d.ts +572 -0
- package/dist/data-structures/queue/deque.js +990 -0
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/index.js +18 -0
- package/dist/data-structures/queue/queue.d.ts +209 -0
- package/dist/data-structures/queue/queue.js +274 -0
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/index.js +17 -0
- package/dist/data-structures/stack/stack.d.ts +106 -0
- package/dist/data-structures/stack/stack.js +136 -0
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +8 -0
- package/dist/data-structures/tree/tree.js +40 -0
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/index.js +17 -0
- package/dist/data-structures/trie/trie.d.ts +155 -0
- package/dist/data-structures/trie/trie.js +326 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +27 -0
- package/dist/interfaces/binary-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.js +2 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/doubly-linked-list.js +2 -0
- package/dist/interfaces/graph.d.ts +5 -0
- package/dist/interfaces/graph.js +2 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/heap.js +2 -0
- package/dist/interfaces/index.d.ts +8 -0
- package/dist/interfaces/index.js +24 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/navigator.js +2 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/priority-queue.js +2 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/segment-tree.js +2 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/types/common.d.ts +20 -0
- package/dist/types/common.js +9 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +31 -0
- package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +8 -0
- package/dist/types/data-structures/binary-tree/bst.js +2 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/index.js +22 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +9 -0
- package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/tree-multimap.js +2 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
- package/dist/types/data-structures/graph/abstract-graph.js +2 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/directed-graph.js +2 -0
- package/dist/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/data-structures/graph/index.js +19 -0
- package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/map-graph.js +2 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/undirected-graph.js +2 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +11 -0
- package/dist/types/data-structures/hash/hash-map.js +2 -0
- package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
- package/dist/types/data-structures/hash/hash-table.js +2 -0
- package/dist/types/data-structures/hash/index.d.ts +3 -0
- package/dist/types/data-structures/hash/index.js +18 -0
- package/dist/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.js +2 -0
- package/dist/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/data-structures/heap/index.js +17 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/max-heap.js +2 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/min-heap.js +2 -0
- package/dist/types/data-structures/index.d.ts +11 -0
- package/dist/types/data-structures/index.js +27 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/index.d.ts +2 -0
- package/dist/types/data-structures/linked-list/index.js +18 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +17 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix.js +2 -0
- package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix2d.js +2 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/data-structures/matrix/navigator.js +2 -0
- package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/vector2d.js +2 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/index.js +19 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
- package/dist/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/types/data-structures/queue/deque.js +2 -0
- package/dist/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/data-structures/queue/index.js +18 -0
- package/dist/types/data-structures/queue/queue.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.js +2 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/index.js +17 -0
- package/dist/types/data-structures/stack/stack.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.js +2 -0
- package/dist/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/data-structures/tree/index.js +17 -0
- package/dist/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/data-structures/tree/tree.js +2 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/index.js +17 -0
- package/dist/types/data-structures/trie/trie.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.js +2 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/utils.d.ts +24 -0
- package/dist/utils/utils.js +89 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +92 -0
- package/docs/assets/main.js +59 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1383 -0
- package/docs/classes/AVLTree.html +2046 -0
- package/docs/classes/AVLTreeNode.html +263 -0
- package/docs/index.html +523 -0
- package/docs/modules.html +45 -0
- package/jest.config.js +8 -0
- package/package.json +147 -0
- package/src/data-structures/binary-tree/avl-tree.ts +443 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1974 -0
- package/src/data-structures/binary-tree/bst.ts +676 -0
- package/src/data-structures/binary-tree/index.ts +7 -0
- package/src/data-structures/binary-tree/rb-tree.ts +585 -0
- package/src/data-structures/binary-tree/segment-tree.ts +190 -0
- package/src/data-structures/binary-tree/tree-multimap.ts +435 -0
- package/src/data-structures/graph/abstract-graph.ts +1181 -0
- package/src/data-structures/graph/directed-graph.ts +593 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +106 -0
- package/src/data-structures/graph/undirected-graph.ts +331 -0
- package/src/data-structures/hash/hash-map.ts +401 -0
- package/src/data-structures/hash/hash-table.ts +268 -0
- package/src/data-structures/hash/index.ts +2 -0
- package/src/data-structures/heap/heap.ts +790 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +26 -0
- package/src/data-structures/heap/min-heap.ts +26 -0
- package/src/data-structures/index.ts +11 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +837 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +784 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +295 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +211 -0
- package/src/data-structures/matrix/navigator.ts +121 -0
- package/src/data-structures/matrix/vector2d.ts +315 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
- package/src/data-structures/priority-queue/priority-queue.ts +16 -0
- package/src/data-structures/queue/deque.ts +1073 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +308 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +150 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +41 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +345 -0
- package/src/index.ts +11 -0
- package/src/interfaces/binary-tree.ts +10 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/graph.ts +7 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +8 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/types/common.ts +23 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +9 -0
- package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/binary-tree.ts +35 -0
- package/src/types/data-structures/binary-tree/bst.ts +13 -0
- package/src/types/data-structures/binary-tree/index.ts +6 -0
- package/src/types/data-structures/binary-tree/rb-tree.ts +10 -0
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-multimap.ts +8 -0
- package/src/types/data-structures/graph/abstract-graph.ts +11 -0
- package/src/types/data-structures/graph/directed-graph.ts +2 -0
- package/src/types/data-structures/graph/index.ts +3 -0
- package/src/types/data-structures/graph/map-graph.ts +1 -0
- package/src/types/data-structures/graph/undirected-graph.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +12 -0
- package/src/types/data-structures/hash/hash-table.ts +1 -0
- package/src/types/data-structures/hash/index.ts +4 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/heap/index.ts +1 -0
- package/src/types/data-structures/heap/max-heap.ts +1 -0
- package/src/types/data-structures/heap/min-heap.ts +1 -0
- package/src/types/data-structures/index.ts +11 -0
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/index.ts +2 -0
- package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +1 -0
- package/src/types/data-structures/matrix/matrix2d.ts +1 -0
- package/src/types/data-structures/matrix/navigator.ts +14 -0
- package/src/types/data-structures/matrix/vector2d.ts +1 -0
- package/src/types/data-structures/priority-queue/index.ts +3 -0
- package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/data-structures/queue/deque.ts +1 -0
- package/src/types/data-structures/queue/index.ts +2 -0
- package/src/types/data-structures/queue/queue.ts +1 -0
- package/src/types/data-structures/stack/index.ts +1 -0
- package/src/types/data-structures/stack/stack.ts +1 -0
- package/src/types/data-structures/tree/index.ts +1 -0
- package/src/types/data-structures/tree/tree.ts +1 -0
- package/src/types/data-structures/trie/index.ts +1 -0
- package/src/types/data-structures/trie/trie.ts +1 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +6 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.ts +101 -0
- package/test/index.test.ts +111 -0
- package/tsconfig.json +38 -0
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
* @author Kirk Qi
|
|
5
|
+
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
|
|
10
|
+
class Heap {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this._elements = [];
|
|
13
|
+
this._comparator = options.comparator;
|
|
14
|
+
if (options.elements && options.elements.length > 0) {
|
|
15
|
+
this._elements = options.elements;
|
|
16
|
+
this.fix();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
get elements() {
|
|
20
|
+
return this._elements;
|
|
21
|
+
}
|
|
22
|
+
get comparator() {
|
|
23
|
+
return this._comparator;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Get the size (number of elements) of the heap.
|
|
27
|
+
*/
|
|
28
|
+
get size() {
|
|
29
|
+
return this.elements.length;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
33
|
+
* @returns The last element or undefined if the heap is empty.
|
|
34
|
+
*/
|
|
35
|
+
get leaf() {
|
|
36
|
+
var _a;
|
|
37
|
+
return (_a = this.elements[this.size - 1]) !== null && _a !== void 0 ? _a : undefined;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
41
|
+
* @returns A new Heap instance.
|
|
42
|
+
* @param options
|
|
43
|
+
*/
|
|
44
|
+
static heapify(options) {
|
|
45
|
+
return new Heap(options);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
49
|
+
* Space Complexity: O(1)
|
|
50
|
+
*/
|
|
51
|
+
/**
|
|
52
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
53
|
+
* Space Complexity: O(1)
|
|
54
|
+
*
|
|
55
|
+
* Insert an element into the heap and maintain the heap properties.
|
|
56
|
+
* @param element - The element to be inserted.
|
|
57
|
+
*/
|
|
58
|
+
add(element) {
|
|
59
|
+
return this.push(element);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
63
|
+
* Space Complexity: O(1)
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
67
|
+
* Space Complexity: O(1)
|
|
68
|
+
*
|
|
69
|
+
* Insert an element into the heap and maintain the heap properties.
|
|
70
|
+
* @param element - The element to be inserted.
|
|
71
|
+
*/
|
|
72
|
+
push(element) {
|
|
73
|
+
this._elements.push(element);
|
|
74
|
+
this._bubbleUp(this.elements.length - 1);
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
79
|
+
* Space Complexity: O(1)
|
|
80
|
+
*/
|
|
81
|
+
/**
|
|
82
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
83
|
+
* Space Complexity: O(1)
|
|
84
|
+
*
|
|
85
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
86
|
+
* @returns The top element or undefined if the heap is empty.
|
|
87
|
+
*/
|
|
88
|
+
poll() {
|
|
89
|
+
if (this.elements.length === 0)
|
|
90
|
+
return;
|
|
91
|
+
const value = this.elements[0];
|
|
92
|
+
const last = this.elements.pop();
|
|
93
|
+
if (this.elements.length) {
|
|
94
|
+
this.elements[0] = last;
|
|
95
|
+
this._sinkDown(0, this.elements.length >> 1);
|
|
96
|
+
}
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
101
|
+
* Space Complexity: O(1)
|
|
102
|
+
*/
|
|
103
|
+
/**
|
|
104
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
105
|
+
* Space Complexity: O(1)
|
|
106
|
+
*
|
|
107
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
108
|
+
* @returns The top element or undefined if the heap is empty.
|
|
109
|
+
*/
|
|
110
|
+
pop() {
|
|
111
|
+
return this.poll();
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Peek at the top element of the heap without removing it.
|
|
115
|
+
* @returns The top element or undefined if the heap is empty.
|
|
116
|
+
*/
|
|
117
|
+
peek() {
|
|
118
|
+
return this.elements[0];
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Check if the heap is empty.
|
|
122
|
+
* @returns True if the heap is empty, otherwise false.
|
|
123
|
+
*/
|
|
124
|
+
isEmpty() {
|
|
125
|
+
return this.size === 0;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Reset the elements of the heap. Make the elements empty.
|
|
129
|
+
*/
|
|
130
|
+
clear() {
|
|
131
|
+
this._elements = [];
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Time Complexity: O(n), where n is the number of elements in the elements array.
|
|
135
|
+
* Space Complexity: O(n)
|
|
136
|
+
*/
|
|
137
|
+
/**
|
|
138
|
+
* Time Complexity: O(n), where n is the number of elements in the elements array.
|
|
139
|
+
* Space Complexity: O(n)
|
|
140
|
+
*
|
|
141
|
+
* Clear and add elements of the heap
|
|
142
|
+
* @param elements
|
|
143
|
+
*/
|
|
144
|
+
refill(elements) {
|
|
145
|
+
this._elements = elements;
|
|
146
|
+
this.fix();
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
150
|
+
* Space Complexity: O(1)
|
|
151
|
+
*/
|
|
152
|
+
/**
|
|
153
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
154
|
+
* Space Complexity: O(1)
|
|
155
|
+
*
|
|
156
|
+
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
157
|
+
* @param element - the element to check.
|
|
158
|
+
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
159
|
+
*/
|
|
160
|
+
has(element) {
|
|
161
|
+
return this.elements.includes(element);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
|
|
165
|
+
* Space Complexity: O(1)
|
|
166
|
+
*/
|
|
167
|
+
/**
|
|
168
|
+
* Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
|
|
169
|
+
* Space Complexity: O(1)
|
|
170
|
+
*
|
|
171
|
+
* The `delete` function removes an element from an array-like data structure, maintaining the order
|
|
172
|
+
* and structure of the remaining elements.
|
|
173
|
+
* @param {E} element - The `element` parameter represents the element that you want to delete from
|
|
174
|
+
* the array `this.elements`.
|
|
175
|
+
* @returns The `delete` function is returning a boolean value. It returns `true` if the element was
|
|
176
|
+
* successfully deleted from the array, and `false` if the element was not found in the array.
|
|
177
|
+
*/
|
|
178
|
+
delete(element) {
|
|
179
|
+
const index = this.elements.indexOf(element);
|
|
180
|
+
if (index < 0)
|
|
181
|
+
return false;
|
|
182
|
+
if (index === 0) {
|
|
183
|
+
this.pop();
|
|
184
|
+
}
|
|
185
|
+
else if (index === this.elements.length - 1) {
|
|
186
|
+
this.elements.pop();
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
this.elements.splice(index, 1, this.elements.pop());
|
|
190
|
+
this._bubbleUp(index);
|
|
191
|
+
this._sinkDown(index, this.elements.length >> 1);
|
|
192
|
+
}
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
197
|
+
* Space Complexity: O(h), where h is the height of the heap.
|
|
198
|
+
*/
|
|
199
|
+
/**
|
|
200
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
201
|
+
* Space Complexity: O(h), where h is the height of the heap.
|
|
202
|
+
*
|
|
203
|
+
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
204
|
+
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
205
|
+
* @returns An array containing elements traversed in the specified order.
|
|
206
|
+
*/
|
|
207
|
+
dfs(order) {
|
|
208
|
+
const result = [];
|
|
209
|
+
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
210
|
+
const dfsHelper = (index) => {
|
|
211
|
+
if (index < this.size) {
|
|
212
|
+
if (order === 'in') {
|
|
213
|
+
dfsHelper(2 * index + 1);
|
|
214
|
+
result.push(this.elements[index]);
|
|
215
|
+
dfsHelper(2 * index + 2);
|
|
216
|
+
}
|
|
217
|
+
else if (order === 'pre') {
|
|
218
|
+
result.push(this.elements[index]);
|
|
219
|
+
dfsHelper(2 * index + 1);
|
|
220
|
+
dfsHelper(2 * index + 2);
|
|
221
|
+
}
|
|
222
|
+
else if (order === 'post') {
|
|
223
|
+
dfsHelper(2 * index + 1);
|
|
224
|
+
dfsHelper(2 * index + 2);
|
|
225
|
+
result.push(this.elements[index]);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
dfsHelper(0); // Traverse starting from the root node
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Time Complexity: O(n)
|
|
234
|
+
* Space Complexity: O(n)
|
|
235
|
+
*/
|
|
236
|
+
/**
|
|
237
|
+
* Time Complexity: O(n)
|
|
238
|
+
* Space Complexity: O(n)
|
|
239
|
+
*
|
|
240
|
+
* Convert the heap to an array.
|
|
241
|
+
* @returns An array containing the elements of the heap.
|
|
242
|
+
*/
|
|
243
|
+
toArray() {
|
|
244
|
+
return [...this.elements];
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Time Complexity: O(n)
|
|
248
|
+
* Space Complexity: O(n)
|
|
249
|
+
*/
|
|
250
|
+
/**
|
|
251
|
+
* Time Complexity: O(n)
|
|
252
|
+
* Space Complexity: O(n)
|
|
253
|
+
*
|
|
254
|
+
* Clone the heap, creating a new heap with the same elements.
|
|
255
|
+
* @returns A new Heap instance containing the same elements.
|
|
256
|
+
*/
|
|
257
|
+
clone() {
|
|
258
|
+
const clonedHeap = new Heap({ comparator: this.comparator });
|
|
259
|
+
clonedHeap._elements = [...this.elements];
|
|
260
|
+
return clonedHeap;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Time Complexity: O(n log n)
|
|
264
|
+
* Space Complexity: O(n)
|
|
265
|
+
*/
|
|
266
|
+
/**
|
|
267
|
+
* Time Complexity: O(n log n)
|
|
268
|
+
* Space Complexity: O(n)
|
|
269
|
+
*
|
|
270
|
+
* Sort the elements in the heap and return them as an array.
|
|
271
|
+
* @returns An array containing the elements sorted in ascending order.
|
|
272
|
+
*/
|
|
273
|
+
sort() {
|
|
274
|
+
const visitedNode = [];
|
|
275
|
+
const cloned = this.clone();
|
|
276
|
+
while (cloned.size !== 0) {
|
|
277
|
+
const top = cloned.poll();
|
|
278
|
+
if (top)
|
|
279
|
+
visitedNode.push(top);
|
|
280
|
+
}
|
|
281
|
+
return visitedNode;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Time Complexity: O(log n)
|
|
285
|
+
* Space Complexity: O(1)
|
|
286
|
+
*/
|
|
287
|
+
/**
|
|
288
|
+
* Time Complexity: O(n)
|
|
289
|
+
* Space Complexity: O(1)
|
|
290
|
+
*
|
|
291
|
+
* Fix the entire heap to maintain heap properties.
|
|
292
|
+
*/
|
|
293
|
+
fix() {
|
|
294
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
295
|
+
this._sinkDown(i, this.elements.length >> 1);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Time Complexity: O(log n)
|
|
299
|
+
* Space Complexity: O(1)
|
|
300
|
+
*/
|
|
301
|
+
/**
|
|
302
|
+
* Time Complexity: O(log n)
|
|
303
|
+
* Space Complexity: O(1)
|
|
304
|
+
*
|
|
305
|
+
* Float operation to maintain heap properties after adding an element.
|
|
306
|
+
* @param index - The index of the newly added element.
|
|
307
|
+
*/
|
|
308
|
+
_bubbleUp(index) {
|
|
309
|
+
const element = this.elements[index];
|
|
310
|
+
while (index > 0) {
|
|
311
|
+
const parent = (index - 1) >> 1;
|
|
312
|
+
const parentItem = this.elements[parent];
|
|
313
|
+
if (this._comparator(parentItem, element) <= 0)
|
|
314
|
+
break;
|
|
315
|
+
this.elements[index] = parentItem;
|
|
316
|
+
index = parent;
|
|
317
|
+
}
|
|
318
|
+
this.elements[index] = element;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Time Complexity: O(n)
|
|
322
|
+
* Space Complexity: O(1)
|
|
323
|
+
*/
|
|
324
|
+
/**
|
|
325
|
+
* Time Complexity: O(log n)
|
|
326
|
+
* Space Complexity: O(1)
|
|
327
|
+
*
|
|
328
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
329
|
+
* @param index - The index from which to start sinking.
|
|
330
|
+
* @param halfLength
|
|
331
|
+
*/
|
|
332
|
+
_sinkDown(index, halfLength) {
|
|
333
|
+
const element = this.elements[index];
|
|
334
|
+
while (index < halfLength) {
|
|
335
|
+
let left = index << 1 | 1;
|
|
336
|
+
const right = left + 1;
|
|
337
|
+
let minItem = this.elements[left];
|
|
338
|
+
if (right < this.elements.length &&
|
|
339
|
+
this._comparator(minItem, this.elements[right]) > 0) {
|
|
340
|
+
left = right;
|
|
341
|
+
minItem = this.elements[right];
|
|
342
|
+
}
|
|
343
|
+
if (this._comparator(minItem, element) >= 0)
|
|
344
|
+
break;
|
|
345
|
+
this.elements[index] = minItem;
|
|
346
|
+
index = left;
|
|
347
|
+
}
|
|
348
|
+
this.elements[index] = element;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
exports.Heap = Heap;
|
|
352
|
+
class FibonacciHeapNode {
|
|
353
|
+
constructor(element, degree = 0) {
|
|
354
|
+
this.element = element;
|
|
355
|
+
this.degree = degree;
|
|
356
|
+
this.marked = false;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
exports.FibonacciHeapNode = FibonacciHeapNode;
|
|
360
|
+
class FibonacciHeap {
|
|
361
|
+
constructor(comparator) {
|
|
362
|
+
this._size = 0;
|
|
363
|
+
this.clear();
|
|
364
|
+
this._comparator = comparator || this.defaultComparator;
|
|
365
|
+
if (typeof this.comparator !== 'function') {
|
|
366
|
+
throw new Error('FibonacciHeap constructor: given comparator should be a function.');
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
get root() {
|
|
370
|
+
return this._root;
|
|
371
|
+
}
|
|
372
|
+
get size() {
|
|
373
|
+
return this._size;
|
|
374
|
+
}
|
|
375
|
+
get min() {
|
|
376
|
+
return this._min;
|
|
377
|
+
}
|
|
378
|
+
get comparator() {
|
|
379
|
+
return this._comparator;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Get the size (number of elements) of the heap.
|
|
383
|
+
* @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
|
|
384
|
+
*/
|
|
385
|
+
clear() {
|
|
386
|
+
this._root = undefined;
|
|
387
|
+
this._min = undefined;
|
|
388
|
+
this._size = 0;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Time Complexity: O(1)
|
|
392
|
+
* Space Complexity: O(1)
|
|
393
|
+
*/
|
|
394
|
+
/**
|
|
395
|
+
* Time Complexity: O(1)
|
|
396
|
+
* Space Complexity: O(1)
|
|
397
|
+
*
|
|
398
|
+
* Insert an element into the heap and maintain the heap properties.
|
|
399
|
+
* @param element
|
|
400
|
+
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
401
|
+
*/
|
|
402
|
+
add(element) {
|
|
403
|
+
return this.push(element);
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Time Complexity: O(1)
|
|
407
|
+
* Space Complexity: O(1)
|
|
408
|
+
*/
|
|
409
|
+
/**
|
|
410
|
+
* Time Complexity: O(1)
|
|
411
|
+
* Space Complexity: O(1)
|
|
412
|
+
*
|
|
413
|
+
* Insert an element into the heap and maintain the heap properties.
|
|
414
|
+
* @param element
|
|
415
|
+
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
416
|
+
*/
|
|
417
|
+
push(element) {
|
|
418
|
+
const node = this.createNode(element);
|
|
419
|
+
node.left = node;
|
|
420
|
+
node.right = node;
|
|
421
|
+
this.mergeWithRoot(node);
|
|
422
|
+
if (!this.min || this.comparator(node.element, this.min.element) <= 0) {
|
|
423
|
+
this._min = node;
|
|
424
|
+
}
|
|
425
|
+
this._size++;
|
|
426
|
+
return this;
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Time Complexity: O(1)
|
|
430
|
+
* Space Complexity: O(1)
|
|
431
|
+
*/
|
|
432
|
+
/**
|
|
433
|
+
* Time Complexity: O(1)
|
|
434
|
+
* Space Complexity: O(1)
|
|
435
|
+
*
|
|
436
|
+
* Peek at the top element of the heap without removing it.
|
|
437
|
+
* @returns The top element or undefined if the heap is empty.
|
|
438
|
+
* @protected
|
|
439
|
+
*/
|
|
440
|
+
peek() {
|
|
441
|
+
return this.min ? this.min.element : undefined;
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
445
|
+
* Space Complexity: O(1)
|
|
446
|
+
*/
|
|
447
|
+
/**
|
|
448
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
449
|
+
* Space Complexity: O(1)
|
|
450
|
+
*
|
|
451
|
+
* Get the size (number of elements) of the heap.
|
|
452
|
+
* @param {FibonacciHeapNode<E>} head - The head of the linked list.
|
|
453
|
+
* @protected
|
|
454
|
+
* @returns FibonacciHeapNode<E>[] - An array containing the elements of the linked list.
|
|
455
|
+
*/
|
|
456
|
+
consumeLinkedList(head) {
|
|
457
|
+
const elements = [];
|
|
458
|
+
if (!head)
|
|
459
|
+
return elements;
|
|
460
|
+
let node = head;
|
|
461
|
+
let flag = false;
|
|
462
|
+
while (true) {
|
|
463
|
+
if (node === head && flag)
|
|
464
|
+
break;
|
|
465
|
+
else if (node === head)
|
|
466
|
+
flag = true;
|
|
467
|
+
if (node) {
|
|
468
|
+
elements.push(node);
|
|
469
|
+
node = node.right;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return elements;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Time Complexity: O(1)
|
|
476
|
+
* Space Complexity: O(1)
|
|
477
|
+
*
|
|
478
|
+
* @param parent
|
|
479
|
+
* @param node
|
|
480
|
+
*/
|
|
481
|
+
mergeWithChild(parent, node) {
|
|
482
|
+
if (!parent.child) {
|
|
483
|
+
parent.child = node;
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
node.right = parent.child.right;
|
|
487
|
+
node.left = parent.child;
|
|
488
|
+
parent.child.right.left = node;
|
|
489
|
+
parent.child.right = node;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
494
|
+
* Space Complexity: O(1)
|
|
495
|
+
*/
|
|
496
|
+
/**
|
|
497
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
498
|
+
* Space Complexity: O(1)
|
|
499
|
+
*
|
|
500
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
501
|
+
* @returns The top element or undefined if the heap is empty.
|
|
502
|
+
*/
|
|
503
|
+
poll() {
|
|
504
|
+
return this.pop();
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
508
|
+
* Space Complexity: O(1)
|
|
509
|
+
*/
|
|
510
|
+
/**
|
|
511
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
512
|
+
* Space Complexity: O(1)
|
|
513
|
+
*
|
|
514
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
515
|
+
* @returns The top element or undefined if the heap is empty.
|
|
516
|
+
*/
|
|
517
|
+
pop() {
|
|
518
|
+
if (this.size === 0)
|
|
519
|
+
return undefined;
|
|
520
|
+
const z = this.min;
|
|
521
|
+
if (z.child) {
|
|
522
|
+
const elements = this.consumeLinkedList(z.child);
|
|
523
|
+
for (const node of elements) {
|
|
524
|
+
this.mergeWithRoot(node);
|
|
525
|
+
node.parent = undefined;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
this.removeFromRoot(z);
|
|
529
|
+
if (z === z.right) {
|
|
530
|
+
this._min = undefined;
|
|
531
|
+
this._root = undefined;
|
|
532
|
+
}
|
|
533
|
+
else {
|
|
534
|
+
this._min = z.right;
|
|
535
|
+
this.consolidate();
|
|
536
|
+
}
|
|
537
|
+
this._size--;
|
|
538
|
+
return z.element;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Time Complexity: O(1)
|
|
542
|
+
* Space Complexity: O(1)
|
|
543
|
+
*/
|
|
544
|
+
/**
|
|
545
|
+
* Time Complexity: O(1)
|
|
546
|
+
* Space Complexity: O(1)
|
|
547
|
+
*
|
|
548
|
+
* merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
|
|
549
|
+
* @param heapToMerge
|
|
550
|
+
*/
|
|
551
|
+
merge(heapToMerge) {
|
|
552
|
+
if (heapToMerge.size === 0) {
|
|
553
|
+
return; // Nothing to merge
|
|
554
|
+
}
|
|
555
|
+
// Merge the root lists of the two heaps
|
|
556
|
+
if (this.root && heapToMerge.root) {
|
|
557
|
+
const thisRoot = this.root;
|
|
558
|
+
const otherRoot = heapToMerge.root;
|
|
559
|
+
const thisRootRight = thisRoot.right;
|
|
560
|
+
const otherRootLeft = otherRoot.left;
|
|
561
|
+
thisRoot.right = otherRoot;
|
|
562
|
+
otherRoot.left = thisRoot;
|
|
563
|
+
thisRootRight.left = otherRootLeft;
|
|
564
|
+
otherRootLeft.right = thisRootRight;
|
|
565
|
+
}
|
|
566
|
+
// Update the minimum node
|
|
567
|
+
if (!this.min || (heapToMerge.min && this.comparator(heapToMerge.min.element, this.min.element) < 0)) {
|
|
568
|
+
this._min = heapToMerge.min;
|
|
569
|
+
}
|
|
570
|
+
// Update the size
|
|
571
|
+
this._size += heapToMerge.size;
|
|
572
|
+
// Clear the heap that was merged
|
|
573
|
+
heapToMerge.clear();
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Default comparator function used by the heap.
|
|
577
|
+
* @param {E} a
|
|
578
|
+
* @param {E} b
|
|
579
|
+
* @protected
|
|
580
|
+
*/
|
|
581
|
+
defaultComparator(a, b) {
|
|
582
|
+
if (a < b)
|
|
583
|
+
return -1;
|
|
584
|
+
if (a > b)
|
|
585
|
+
return 1;
|
|
586
|
+
return 0;
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Create a new node.
|
|
590
|
+
* @param element
|
|
591
|
+
* @protected
|
|
592
|
+
*/
|
|
593
|
+
createNode(element) {
|
|
594
|
+
return new FibonacciHeapNode(element);
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Time Complexity: O(1)
|
|
598
|
+
* Space Complexity: O(1)
|
|
599
|
+
*/
|
|
600
|
+
/**
|
|
601
|
+
* Time Complexity: O(1)
|
|
602
|
+
* Space Complexity: O(1)
|
|
603
|
+
*
|
|
604
|
+
* Merge the given node with the root list.
|
|
605
|
+
* @param node - The node to be merged.
|
|
606
|
+
*/
|
|
607
|
+
mergeWithRoot(node) {
|
|
608
|
+
if (!this.root) {
|
|
609
|
+
this._root = node;
|
|
610
|
+
}
|
|
611
|
+
else {
|
|
612
|
+
node.right = this.root.right;
|
|
613
|
+
node.left = this.root;
|
|
614
|
+
this.root.right.left = node;
|
|
615
|
+
this.root.right = node;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Time Complexity: O(1)
|
|
620
|
+
* Space Complexity: O(1)
|
|
621
|
+
*/
|
|
622
|
+
/**
|
|
623
|
+
* Time Complexity: O(1)
|
|
624
|
+
* Space Complexity: O(1)
|
|
625
|
+
*.
|
|
626
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
627
|
+
* @param node - The node to be removed.
|
|
628
|
+
* @protected
|
|
629
|
+
*/
|
|
630
|
+
removeFromRoot(node) {
|
|
631
|
+
if (this.root === node)
|
|
632
|
+
this._root = node.right;
|
|
633
|
+
if (node.left)
|
|
634
|
+
node.left.right = node.right;
|
|
635
|
+
if (node.right)
|
|
636
|
+
node.right.left = node.left;
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Time Complexity: O(1)
|
|
640
|
+
* Space Complexity: O(1)
|
|
641
|
+
*/
|
|
642
|
+
/**
|
|
643
|
+
* Time Complexity: O(1)
|
|
644
|
+
* Space Complexity: O(1)
|
|
645
|
+
*
|
|
646
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
647
|
+
* @param y
|
|
648
|
+
* @param x
|
|
649
|
+
* @protected
|
|
650
|
+
*/
|
|
651
|
+
link(y, x) {
|
|
652
|
+
this.removeFromRoot(y);
|
|
653
|
+
y.left = y;
|
|
654
|
+
y.right = y;
|
|
655
|
+
this.mergeWithChild(x, y);
|
|
656
|
+
x.degree++;
|
|
657
|
+
y.parent = x;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Time Complexity: O(n log n), where n is the number of elements in the heap.
|
|
661
|
+
* Space Complexity: O(n)
|
|
662
|
+
*/
|
|
663
|
+
/**
|
|
664
|
+
* Time Complexity: O(n log n), where n is the number of elements in the heap.
|
|
665
|
+
* Space Complexity: O(n)
|
|
666
|
+
*
|
|
667
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
668
|
+
* @protected
|
|
669
|
+
*/
|
|
670
|
+
consolidate() {
|
|
671
|
+
const A = new Array(this.size);
|
|
672
|
+
const elements = this.consumeLinkedList(this.root);
|
|
673
|
+
let x, y, d, t;
|
|
674
|
+
for (const node of elements) {
|
|
675
|
+
x = node;
|
|
676
|
+
d = x.degree;
|
|
677
|
+
while (A[d]) {
|
|
678
|
+
y = A[d];
|
|
679
|
+
if (this.comparator(x.element, y.element) > 0) {
|
|
680
|
+
t = x;
|
|
681
|
+
x = y;
|
|
682
|
+
y = t;
|
|
683
|
+
}
|
|
684
|
+
this.link(y, x);
|
|
685
|
+
A[d] = undefined;
|
|
686
|
+
d++;
|
|
687
|
+
}
|
|
688
|
+
A[d] = x;
|
|
689
|
+
}
|
|
690
|
+
for (let i = 0; i < this.size; i++) {
|
|
691
|
+
if (A[i] && this.comparator(A[i].element, this.min.element) <= 0) {
|
|
692
|
+
this._min = A[i];
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
exports.FibonacciHeap = FibonacciHeap;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./max-heap"), exports);
|
|
18
|
+
__exportStar(require("./min-heap"), exports);
|
|
19
|
+
__exportStar(require("./heap"), exports);
|