data-structure-typed 1.50.1 → 1.50.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/CHANGELOG.md +2 -1
- package/README.md +30 -28
- package/README_zh-CN.md +1 -1
- package/SPECIFICATION.md +56 -0
- package/SPONSOR-zh-CN.md +90 -0
- package/SPONSOR.md +54 -0
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +149 -161
- package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/cjs/data-structures/base/iterable-base.js +143 -7
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +131 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/cjs/data-structures/graph/map-graph.js +14 -0
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/cjs/data-structures/hash/hash-map.js +347 -78
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/cjs/data-structures/heap/heap.js +95 -26
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/cjs/data-structures/matrix/matrix.js +50 -11
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/cjs/data-structures/queue/deque.js +200 -62
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/cjs/data-structures/queue/queue.js +111 -49
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/cjs/data-structures/stack/stack.js +58 -22
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/cjs/data-structures/tree/tree.js +77 -11
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/cjs/data-structures/trie/trie.js +153 -33
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +131 -0
- package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/mjs/data-structures/graph/map-graph.js +14 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/mjs/data-structures/hash/hash-map.js +353 -84
- package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/mjs/data-structures/heap/heap.js +95 -26
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/mjs/data-structures/matrix/matrix.js +50 -11
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/mjs/data-structures/queue/deque.js +205 -67
- package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/mjs/data-structures/queue/queue.js +111 -49
- package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/mjs/data-structures/stack/stack.js +58 -22
- package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/mjs/data-structures/tree/tree.js +80 -14
- package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/mjs/data-structures/trie/trie.js +156 -36
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +3121 -1583
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +61 -55
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/performance/data-structures/queue/deque.test.ts +26 -25
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
- package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
- package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
- package/test/unit/data-structures/heap/heap.test.ts +18 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +65 -5
- package/test/unit/data-structures/queue/queue.test.ts +24 -5
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- package/test/unit/data-structures/trie/trie.test.ts +17 -0
|
@@ -19,10 +19,28 @@ import { IterableElementBase } from '../base';
|
|
|
19
19
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
20
20
|
*/
|
|
21
21
|
export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
22
|
+
/**
|
|
23
|
+
* The constructor initializes a heap data structure with optional elements and options.
|
|
24
|
+
* @param elements - The `elements` parameter is an iterable object that contains the initial
|
|
25
|
+
* elements to be added to the heap. It is an optional parameter and if not provided, the heap will
|
|
26
|
+
* be initialized as empty.
|
|
27
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
28
|
+
* configuration options for the heap. In this case, it is used to specify a custom comparator
|
|
29
|
+
* function for comparing elements in the heap. The comparator function is used to determine the
|
|
30
|
+
* order of elements in the heap.
|
|
31
|
+
*/
|
|
22
32
|
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
23
33
|
protected _comparator: (a: E, b: E) => number;
|
|
34
|
+
/**
|
|
35
|
+
* The function returns the value of the _comparator property.
|
|
36
|
+
* @returns The `_comparator` property is being returned.
|
|
37
|
+
*/
|
|
24
38
|
get comparator(): (a: E, b: E) => number;
|
|
25
39
|
protected _elements: E[];
|
|
40
|
+
/**
|
|
41
|
+
* The function returns an array of elements.
|
|
42
|
+
* @returns The elements array is being returned.
|
|
43
|
+
*/
|
|
26
44
|
get elements(): E[];
|
|
27
45
|
/**
|
|
28
46
|
* Get the size (number of elements) of the heap.
|
|
@@ -43,11 +61,12 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
43
61
|
comparator: Comparator<E>;
|
|
44
62
|
}): Heap<E>;
|
|
45
63
|
/**
|
|
46
|
-
* Time Complexity: O(log n)
|
|
64
|
+
* Time Complexity: O(log n)
|
|
47
65
|
* Space Complexity: O(1)
|
|
66
|
+
* where n is the number of elements in the heap.
|
|
48
67
|
*/
|
|
49
68
|
/**
|
|
50
|
-
* Time Complexity: O(log n)
|
|
69
|
+
* Time Complexity: O(log n)
|
|
51
70
|
* Space Complexity: O(1)
|
|
52
71
|
*
|
|
53
72
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -55,11 +74,12 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
55
74
|
*/
|
|
56
75
|
add(element: E): boolean;
|
|
57
76
|
/**
|
|
58
|
-
* Time Complexity: O(log n)
|
|
77
|
+
* Time Complexity: O(log n)
|
|
59
78
|
* Space Complexity: O(1)
|
|
79
|
+
* where n is the number of elements in the heap.
|
|
60
80
|
*/
|
|
61
81
|
/**
|
|
62
|
-
* Time Complexity: O(log n)
|
|
82
|
+
* Time Complexity: O(log n)
|
|
63
83
|
* Space Complexity: O(1)
|
|
64
84
|
*
|
|
65
85
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -67,6 +87,13 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
67
87
|
*/
|
|
68
88
|
poll(): E | undefined;
|
|
69
89
|
/**
|
|
90
|
+
* Time Complexity: O(1)
|
|
91
|
+
* Space Complexity: O(1)
|
|
92
|
+
*/
|
|
93
|
+
/**
|
|
94
|
+
* Time Complexity: O(1)
|
|
95
|
+
* Space Complexity: O(1)
|
|
96
|
+
*
|
|
70
97
|
* Peek at the top element of the heap without removing it.
|
|
71
98
|
* @returns The top element or undefined if the heap is empty.
|
|
72
99
|
*/
|
|
@@ -81,11 +108,11 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
81
108
|
*/
|
|
82
109
|
clear(): void;
|
|
83
110
|
/**
|
|
84
|
-
* Time Complexity: O(n)
|
|
111
|
+
* Time Complexity: O(n)
|
|
85
112
|
* Space Complexity: O(n)
|
|
86
113
|
*/
|
|
87
114
|
/**
|
|
88
|
-
* Time Complexity: O(n)
|
|
115
|
+
* Time Complexity: O(n)
|
|
89
116
|
* Space Complexity: O(n)
|
|
90
117
|
*
|
|
91
118
|
* Clear and add elements of the heap
|
|
@@ -93,11 +120,11 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
93
120
|
*/
|
|
94
121
|
refill(elements: E[]): boolean[];
|
|
95
122
|
/**
|
|
96
|
-
* Time Complexity: O(n)
|
|
123
|
+
* Time Complexity: O(n)
|
|
97
124
|
* Space Complexity: O(1)
|
|
98
125
|
*/
|
|
99
126
|
/**
|
|
100
|
-
* Time Complexity: O(n)
|
|
127
|
+
* Time Complexity: O(n)
|
|
101
128
|
* Space Complexity: O(1)
|
|
102
129
|
*
|
|
103
130
|
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
@@ -106,11 +133,12 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
106
133
|
*/
|
|
107
134
|
has(element: E): boolean;
|
|
108
135
|
/**
|
|
109
|
-
* Time Complexity: O(n)
|
|
136
|
+
* Time Complexity: O(n)
|
|
110
137
|
* Space Complexity: O(1)
|
|
138
|
+
* The worst-case O(n) 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.
|
|
111
139
|
*/
|
|
112
140
|
/**
|
|
113
|
-
* Time Complexity: O(n)
|
|
141
|
+
* Time Complexity: O(n)
|
|
114
142
|
* Space Complexity: O(1)
|
|
115
143
|
*
|
|
116
144
|
* The `delete` function removes an element from an array-like data structure, maintaining the order
|
|
@@ -122,12 +150,13 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
122
150
|
*/
|
|
123
151
|
delete(element: E): boolean;
|
|
124
152
|
/**
|
|
125
|
-
* Time Complexity: O(n)
|
|
126
|
-
* Space Complexity: O(
|
|
153
|
+
* Time Complexity: O(n)
|
|
154
|
+
* Space Complexity: O(log n)
|
|
155
|
+
* where log n is the height of the heap.
|
|
127
156
|
*/
|
|
128
157
|
/**
|
|
129
|
-
* Time Complexity: O(n)
|
|
130
|
-
* Space Complexity: O(
|
|
158
|
+
* Time Complexity: O(n)
|
|
159
|
+
* Space Complexity: O(log n)
|
|
131
160
|
*
|
|
132
161
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
133
162
|
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
@@ -171,12 +200,12 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
171
200
|
*/
|
|
172
201
|
sort(): E[];
|
|
173
202
|
/**
|
|
174
|
-
* Time Complexity: O(log n)
|
|
175
|
-
* Space Complexity: O(
|
|
203
|
+
* Time Complexity: O(n log n)
|
|
204
|
+
* Space Complexity: O(n)
|
|
176
205
|
*/
|
|
177
206
|
/**
|
|
178
|
-
* Time Complexity: O(n)
|
|
179
|
-
* Space Complexity: O(
|
|
207
|
+
* Time Complexity: O(n log n)
|
|
208
|
+
* Space Complexity: O(n)
|
|
180
209
|
*
|
|
181
210
|
* Fix the entire heap to maintain heap properties.
|
|
182
211
|
*/
|
|
@@ -227,9 +256,12 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
227
256
|
* original Heap.
|
|
228
257
|
*/
|
|
229
258
|
map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T>;
|
|
259
|
+
/**
|
|
260
|
+
* The function `_getIterator` returns an iterable iterator for the elements in the class.
|
|
261
|
+
*/
|
|
230
262
|
protected _getIterator(): IterableIterator<E>;
|
|
231
263
|
/**
|
|
232
|
-
* Time Complexity: O(n)
|
|
264
|
+
* Time Complexity: O(log n)
|
|
233
265
|
* Space Complexity: O(1)
|
|
234
266
|
*/
|
|
235
267
|
/**
|
|
@@ -240,6 +272,10 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
240
272
|
* @param index - The index of the newly added element.
|
|
241
273
|
*/
|
|
242
274
|
protected _bubbleUp(index: number): boolean;
|
|
275
|
+
/**
|
|
276
|
+
* Time Complexity: O(log n)
|
|
277
|
+
* Space Complexity: O(1)
|
|
278
|
+
*/
|
|
243
279
|
/**
|
|
244
280
|
* Time Complexity: O(log n)
|
|
245
281
|
* Space Complexity: O(1)
|
|
@@ -258,17 +294,51 @@ export declare class FibonacciHeapNode<E> {
|
|
|
258
294
|
child?: FibonacciHeapNode<E>;
|
|
259
295
|
parent?: FibonacciHeapNode<E>;
|
|
260
296
|
marked: boolean;
|
|
297
|
+
/**
|
|
298
|
+
* The constructor function initializes an object with an element and a degree, and sets the marked
|
|
299
|
+
* property to false.
|
|
300
|
+
* @param {E} element - The "element" parameter represents the value or data that will be stored in
|
|
301
|
+
* the node of a data structure. It can be any type of data, such as a number, string, object, or
|
|
302
|
+
* even another data structure.
|
|
303
|
+
* @param [degree=0] - The degree parameter represents the degree of the element in a data structure
|
|
304
|
+
* called a Fibonacci heap. The degree of a node is the number of children it has. By default, the
|
|
305
|
+
* degree is set to 0 when a new node is created.
|
|
306
|
+
*/
|
|
261
307
|
constructor(element: E, degree?: number);
|
|
262
308
|
}
|
|
263
309
|
export declare class FibonacciHeap<E> {
|
|
310
|
+
/**
|
|
311
|
+
* The constructor function initializes a FibonacciHeap object with an optional comparator function.
|
|
312
|
+
* @param [comparator] - The `comparator` parameter is an optional argument that represents a
|
|
313
|
+
* function used to compare elements in the FibonacciHeap. If a comparator function is provided, it
|
|
314
|
+
* will be used to determine the order of elements in the heap. If no comparator function is
|
|
315
|
+
* provided, a default comparator function will be used.
|
|
316
|
+
*/
|
|
264
317
|
constructor(comparator?: Comparator<E>);
|
|
265
318
|
protected _root?: FibonacciHeapNode<E>;
|
|
319
|
+
/**
|
|
320
|
+
* The function returns the root node of a Fibonacci heap.
|
|
321
|
+
* @returns The method is returning either a FibonacciHeapNode object or undefined.
|
|
322
|
+
*/
|
|
266
323
|
get root(): FibonacciHeapNode<E> | undefined;
|
|
267
324
|
protected _size: number;
|
|
325
|
+
/**
|
|
326
|
+
* The function returns the size of an object.
|
|
327
|
+
* @returns The size of the object, which is a number.
|
|
328
|
+
*/
|
|
268
329
|
get size(): number;
|
|
269
330
|
protected _min?: FibonacciHeapNode<E>;
|
|
331
|
+
/**
|
|
332
|
+
* The function returns the minimum node in a Fibonacci heap.
|
|
333
|
+
* @returns The method is returning the minimum node of the Fibonacci heap, which is of type
|
|
334
|
+
* `FibonacciHeapNode<E>`. If there is no minimum node, it will return `undefined`.
|
|
335
|
+
*/
|
|
270
336
|
get min(): FibonacciHeapNode<E> | undefined;
|
|
271
337
|
protected _comparator: Comparator<E>;
|
|
338
|
+
/**
|
|
339
|
+
* The function returns the comparator used for comparing elements.
|
|
340
|
+
* @returns The `_comparator` property of the object.
|
|
341
|
+
*/
|
|
272
342
|
get comparator(): Comparator<E>;
|
|
273
343
|
/**
|
|
274
344
|
* Get the size (number of elements) of the heap.
|
|
@@ -337,11 +407,11 @@ export declare class FibonacciHeap<E> {
|
|
|
337
407
|
*/
|
|
338
408
|
mergeWithChild(parent: FibonacciHeapNode<E>, node: FibonacciHeapNode<E>): void;
|
|
339
409
|
/**
|
|
340
|
-
* Time Complexity: O(log n)
|
|
410
|
+
* Time Complexity: O(log n)
|
|
341
411
|
* Space Complexity: O(1)
|
|
342
412
|
*/
|
|
343
413
|
/**
|
|
344
|
-
* Time Complexity: O(log n)
|
|
414
|
+
* Time Complexity: O(log n)
|
|
345
415
|
* Space Complexity: O(1)
|
|
346
416
|
*
|
|
347
417
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -349,11 +419,11 @@ export declare class FibonacciHeap<E> {
|
|
|
349
419
|
*/
|
|
350
420
|
poll(): E | undefined;
|
|
351
421
|
/**
|
|
352
|
-
* Time Complexity: O(log n)
|
|
422
|
+
* Time Complexity: O(log n)
|
|
353
423
|
* Space Complexity: O(1)
|
|
354
424
|
*/
|
|
355
425
|
/**
|
|
356
|
-
* Time Complexity: O(log n)
|
|
426
|
+
* Time Complexity: O(log n)
|
|
357
427
|
* Space Complexity: O(1)
|
|
358
428
|
*
|
|
359
429
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -425,11 +495,11 @@ export declare class FibonacciHeap<E> {
|
|
|
425
495
|
*/
|
|
426
496
|
protected _link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
|
|
427
497
|
/**
|
|
428
|
-
* Time Complexity: O(n log n)
|
|
498
|
+
* Time Complexity: O(n log n)
|
|
429
499
|
* Space Complexity: O(n)
|
|
430
500
|
*/
|
|
431
501
|
/**
|
|
432
|
-
* Time Complexity: O(n log n)
|
|
502
|
+
* Time Complexity: O(n log n)
|
|
433
503
|
* Space Complexity: O(n)
|
|
434
504
|
*
|
|
435
505
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -18,6 +18,16 @@ import { IterableElementBase } from '../base';
|
|
|
18
18
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
19
19
|
*/
|
|
20
20
|
export class Heap extends IterableElementBase {
|
|
21
|
+
/**
|
|
22
|
+
* The constructor initializes a heap data structure with optional elements and options.
|
|
23
|
+
* @param elements - The `elements` parameter is an iterable object that contains the initial
|
|
24
|
+
* elements to be added to the heap. It is an optional parameter and if not provided, the heap will
|
|
25
|
+
* be initialized as empty.
|
|
26
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
27
|
+
* configuration options for the heap. In this case, it is used to specify a custom comparator
|
|
28
|
+
* function for comparing elements in the heap. The comparator function is used to determine the
|
|
29
|
+
* order of elements in the heap.
|
|
30
|
+
*/
|
|
21
31
|
constructor(elements = [], options) {
|
|
22
32
|
super();
|
|
23
33
|
if (options) {
|
|
@@ -29,7 +39,6 @@ export class Heap extends IterableElementBase {
|
|
|
29
39
|
for (const el of elements) {
|
|
30
40
|
this.add(el);
|
|
31
41
|
}
|
|
32
|
-
// this.fix();
|
|
33
42
|
}
|
|
34
43
|
}
|
|
35
44
|
_comparator = (a, b) => {
|
|
@@ -40,10 +49,18 @@ export class Heap extends IterableElementBase {
|
|
|
40
49
|
return a - b;
|
|
41
50
|
}
|
|
42
51
|
};
|
|
52
|
+
/**
|
|
53
|
+
* The function returns the value of the _comparator property.
|
|
54
|
+
* @returns The `_comparator` property is being returned.
|
|
55
|
+
*/
|
|
43
56
|
get comparator() {
|
|
44
57
|
return this._comparator;
|
|
45
58
|
}
|
|
46
59
|
_elements = [];
|
|
60
|
+
/**
|
|
61
|
+
* The function returns an array of elements.
|
|
62
|
+
* @returns The elements array is being returned.
|
|
63
|
+
*/
|
|
47
64
|
get elements() {
|
|
48
65
|
return this._elements;
|
|
49
66
|
}
|
|
@@ -70,11 +87,12 @@ export class Heap extends IterableElementBase {
|
|
|
70
87
|
return new Heap(elements, options);
|
|
71
88
|
}
|
|
72
89
|
/**
|
|
73
|
-
* Time Complexity: O(log n)
|
|
90
|
+
* Time Complexity: O(log n)
|
|
74
91
|
* Space Complexity: O(1)
|
|
92
|
+
* where n is the number of elements in the heap.
|
|
75
93
|
*/
|
|
76
94
|
/**
|
|
77
|
-
* Time Complexity: O(log n)
|
|
95
|
+
* Time Complexity: O(log n)
|
|
78
96
|
* Space Complexity: O(1)
|
|
79
97
|
*
|
|
80
98
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -85,11 +103,12 @@ export class Heap extends IterableElementBase {
|
|
|
85
103
|
return this._bubbleUp(this.elements.length - 1);
|
|
86
104
|
}
|
|
87
105
|
/**
|
|
88
|
-
* Time Complexity: O(log n)
|
|
106
|
+
* Time Complexity: O(log n)
|
|
89
107
|
* Space Complexity: O(1)
|
|
108
|
+
* where n is the number of elements in the heap.
|
|
90
109
|
*/
|
|
91
110
|
/**
|
|
92
|
-
* Time Complexity: O(log n)
|
|
111
|
+
* Time Complexity: O(log n)
|
|
93
112
|
* Space Complexity: O(1)
|
|
94
113
|
*
|
|
95
114
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -107,6 +126,13 @@ export class Heap extends IterableElementBase {
|
|
|
107
126
|
return value;
|
|
108
127
|
}
|
|
109
128
|
/**
|
|
129
|
+
* Time Complexity: O(1)
|
|
130
|
+
* Space Complexity: O(1)
|
|
131
|
+
*/
|
|
132
|
+
/**
|
|
133
|
+
* Time Complexity: O(1)
|
|
134
|
+
* Space Complexity: O(1)
|
|
135
|
+
*
|
|
110
136
|
* Peek at the top element of the heap without removing it.
|
|
111
137
|
* @returns The top element or undefined if the heap is empty.
|
|
112
138
|
*/
|
|
@@ -127,11 +153,11 @@ export class Heap extends IterableElementBase {
|
|
|
127
153
|
this._elements = [];
|
|
128
154
|
}
|
|
129
155
|
/**
|
|
130
|
-
* Time Complexity: O(n)
|
|
156
|
+
* Time Complexity: O(n)
|
|
131
157
|
* Space Complexity: O(n)
|
|
132
158
|
*/
|
|
133
159
|
/**
|
|
134
|
-
* Time Complexity: O(n)
|
|
160
|
+
* Time Complexity: O(n)
|
|
135
161
|
* Space Complexity: O(n)
|
|
136
162
|
*
|
|
137
163
|
* Clear and add elements of the heap
|
|
@@ -142,11 +168,11 @@ export class Heap extends IterableElementBase {
|
|
|
142
168
|
return this.fix();
|
|
143
169
|
}
|
|
144
170
|
/**
|
|
145
|
-
* Time Complexity: O(n)
|
|
171
|
+
* Time Complexity: O(n)
|
|
146
172
|
* Space Complexity: O(1)
|
|
147
173
|
*/
|
|
148
174
|
/**
|
|
149
|
-
* Time Complexity: O(n)
|
|
175
|
+
* Time Complexity: O(n)
|
|
150
176
|
* Space Complexity: O(1)
|
|
151
177
|
*
|
|
152
178
|
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
@@ -157,11 +183,12 @@ export class Heap extends IterableElementBase {
|
|
|
157
183
|
return this.elements.includes(element);
|
|
158
184
|
}
|
|
159
185
|
/**
|
|
160
|
-
* Time Complexity: O(n)
|
|
186
|
+
* Time Complexity: O(n)
|
|
161
187
|
* Space Complexity: O(1)
|
|
188
|
+
* The worst-case O(n) 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.
|
|
162
189
|
*/
|
|
163
190
|
/**
|
|
164
|
-
* Time Complexity: O(n)
|
|
191
|
+
* Time Complexity: O(n)
|
|
165
192
|
* Space Complexity: O(1)
|
|
166
193
|
*
|
|
167
194
|
* The `delete` function removes an element from an array-like data structure, maintaining the order
|
|
@@ -189,12 +216,13 @@ export class Heap extends IterableElementBase {
|
|
|
189
216
|
return true;
|
|
190
217
|
}
|
|
191
218
|
/**
|
|
192
|
-
* Time Complexity: O(n)
|
|
193
|
-
* Space Complexity: O(
|
|
219
|
+
* Time Complexity: O(n)
|
|
220
|
+
* Space Complexity: O(log n)
|
|
221
|
+
* where log n is the height of the heap.
|
|
194
222
|
*/
|
|
195
223
|
/**
|
|
196
|
-
* Time Complexity: O(n)
|
|
197
|
-
* Space Complexity: O(
|
|
224
|
+
* Time Complexity: O(n)
|
|
225
|
+
* Space Complexity: O(log n)
|
|
198
226
|
*
|
|
199
227
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
200
228
|
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
@@ -278,12 +306,12 @@ export class Heap extends IterableElementBase {
|
|
|
278
306
|
return visitedNode;
|
|
279
307
|
}
|
|
280
308
|
/**
|
|
281
|
-
* Time Complexity: O(log n)
|
|
282
|
-
* Space Complexity: O(
|
|
309
|
+
* Time Complexity: O(n log n)
|
|
310
|
+
* Space Complexity: O(n)
|
|
283
311
|
*/
|
|
284
312
|
/**
|
|
285
|
-
* Time Complexity: O(n)
|
|
286
|
-
* Space Complexity: O(
|
|
313
|
+
* Time Complexity: O(n log n)
|
|
314
|
+
* Space Complexity: O(n)
|
|
287
315
|
*
|
|
288
316
|
* Fix the entire heap to maintain heap properties.
|
|
289
317
|
*/
|
|
@@ -357,13 +385,16 @@ export class Heap extends IterableElementBase {
|
|
|
357
385
|
}
|
|
358
386
|
return mappedHeap;
|
|
359
387
|
}
|
|
388
|
+
/**
|
|
389
|
+
* The function `_getIterator` returns an iterable iterator for the elements in the class.
|
|
390
|
+
*/
|
|
360
391
|
*_getIterator() {
|
|
361
392
|
for (const element of this.elements) {
|
|
362
393
|
yield element;
|
|
363
394
|
}
|
|
364
395
|
}
|
|
365
396
|
/**
|
|
366
|
-
* Time Complexity: O(n)
|
|
397
|
+
* Time Complexity: O(log n)
|
|
367
398
|
* Space Complexity: O(1)
|
|
368
399
|
*/
|
|
369
400
|
/**
|
|
@@ -386,6 +417,10 @@ export class Heap extends IterableElementBase {
|
|
|
386
417
|
this.elements[index] = element;
|
|
387
418
|
return true;
|
|
388
419
|
}
|
|
420
|
+
/**
|
|
421
|
+
* Time Complexity: O(log n)
|
|
422
|
+
* Space Complexity: O(1)
|
|
423
|
+
*/
|
|
389
424
|
/**
|
|
390
425
|
* Time Complexity: O(log n)
|
|
391
426
|
* Space Complexity: O(1)
|
|
@@ -421,6 +456,16 @@ export class FibonacciHeapNode {
|
|
|
421
456
|
child;
|
|
422
457
|
parent;
|
|
423
458
|
marked;
|
|
459
|
+
/**
|
|
460
|
+
* The constructor function initializes an object with an element and a degree, and sets the marked
|
|
461
|
+
* property to false.
|
|
462
|
+
* @param {E} element - The "element" parameter represents the value or data that will be stored in
|
|
463
|
+
* the node of a data structure. It can be any type of data, such as a number, string, object, or
|
|
464
|
+
* even another data structure.
|
|
465
|
+
* @param [degree=0] - The degree parameter represents the degree of the element in a data structure
|
|
466
|
+
* called a Fibonacci heap. The degree of a node is the number of children it has. By default, the
|
|
467
|
+
* degree is set to 0 when a new node is created.
|
|
468
|
+
*/
|
|
424
469
|
constructor(element, degree = 0) {
|
|
425
470
|
this.element = element;
|
|
426
471
|
this.degree = degree;
|
|
@@ -428,6 +473,13 @@ export class FibonacciHeapNode {
|
|
|
428
473
|
}
|
|
429
474
|
}
|
|
430
475
|
export class FibonacciHeap {
|
|
476
|
+
/**
|
|
477
|
+
* The constructor function initializes a FibonacciHeap object with an optional comparator function.
|
|
478
|
+
* @param [comparator] - The `comparator` parameter is an optional argument that represents a
|
|
479
|
+
* function used to compare elements in the FibonacciHeap. If a comparator function is provided, it
|
|
480
|
+
* will be used to determine the order of elements in the heap. If no comparator function is
|
|
481
|
+
* provided, a default comparator function will be used.
|
|
482
|
+
*/
|
|
431
483
|
constructor(comparator) {
|
|
432
484
|
this.clear();
|
|
433
485
|
this._comparator = comparator || this._defaultComparator;
|
|
@@ -436,18 +488,35 @@ export class FibonacciHeap {
|
|
|
436
488
|
}
|
|
437
489
|
}
|
|
438
490
|
_root;
|
|
491
|
+
/**
|
|
492
|
+
* The function returns the root node of a Fibonacci heap.
|
|
493
|
+
* @returns The method is returning either a FibonacciHeapNode object or undefined.
|
|
494
|
+
*/
|
|
439
495
|
get root() {
|
|
440
496
|
return this._root;
|
|
441
497
|
}
|
|
442
498
|
_size = 0;
|
|
499
|
+
/**
|
|
500
|
+
* The function returns the size of an object.
|
|
501
|
+
* @returns The size of the object, which is a number.
|
|
502
|
+
*/
|
|
443
503
|
get size() {
|
|
444
504
|
return this._size;
|
|
445
505
|
}
|
|
446
506
|
_min;
|
|
507
|
+
/**
|
|
508
|
+
* The function returns the minimum node in a Fibonacci heap.
|
|
509
|
+
* @returns The method is returning the minimum node of the Fibonacci heap, which is of type
|
|
510
|
+
* `FibonacciHeapNode<E>`. If there is no minimum node, it will return `undefined`.
|
|
511
|
+
*/
|
|
447
512
|
get min() {
|
|
448
513
|
return this._min;
|
|
449
514
|
}
|
|
450
515
|
_comparator;
|
|
516
|
+
/**
|
|
517
|
+
* The function returns the comparator used for comparing elements.
|
|
518
|
+
* @returns The `_comparator` property of the object.
|
|
519
|
+
*/
|
|
451
520
|
get comparator() {
|
|
452
521
|
return this._comparator;
|
|
453
522
|
}
|
|
@@ -563,11 +632,11 @@ export class FibonacciHeap {
|
|
|
563
632
|
}
|
|
564
633
|
}
|
|
565
634
|
/**
|
|
566
|
-
* Time Complexity: O(log n)
|
|
635
|
+
* Time Complexity: O(log n)
|
|
567
636
|
* Space Complexity: O(1)
|
|
568
637
|
*/
|
|
569
638
|
/**
|
|
570
|
-
* Time Complexity: O(log n)
|
|
639
|
+
* Time Complexity: O(log n)
|
|
571
640
|
* Space Complexity: O(1)
|
|
572
641
|
*
|
|
573
642
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -577,11 +646,11 @@ export class FibonacciHeap {
|
|
|
577
646
|
return this.pop();
|
|
578
647
|
}
|
|
579
648
|
/**
|
|
580
|
-
* Time Complexity: O(log n)
|
|
649
|
+
* Time Complexity: O(log n)
|
|
581
650
|
* Space Complexity: O(1)
|
|
582
651
|
*/
|
|
583
652
|
/**
|
|
584
|
-
* Time Complexity: O(log n)
|
|
653
|
+
* Time Complexity: O(log n)
|
|
585
654
|
* Space Complexity: O(1)
|
|
586
655
|
*
|
|
587
656
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -730,11 +799,11 @@ export class FibonacciHeap {
|
|
|
730
799
|
y.parent = x;
|
|
731
800
|
}
|
|
732
801
|
/**
|
|
733
|
-
* Time Complexity: O(n log n)
|
|
802
|
+
* Time Complexity: O(n log n)
|
|
734
803
|
* Space Complexity: O(n)
|
|
735
804
|
*/
|
|
736
805
|
/**
|
|
737
|
-
* Time Complexity: O(n log n)
|
|
806
|
+
* Time Complexity: O(n log n)
|
|
738
807
|
* Space Complexity: O(n)
|
|
739
808
|
*
|
|
740
809
|
* Remove and return the top element (smallest or largest element) from the heap.
|