linked-list-typed 1.45.3 → 1.46.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/hash/hash-map.d.ts +6 -5
- package/dist/data-structures/hash/hash-map.js +9 -6
- package/dist/data-structures/heap/heap.d.ts +59 -47
- package/dist/data-structures/heap/heap.js +133 -123
- package/dist/data-structures/queue/deque.d.ts +483 -114
- package/dist/data-structures/queue/deque.js +921 -183
- package/dist/types/data-structures/hash/hash-map.d.ts +0 -9
- package/dist/types/helpers.d.ts +11 -0
- package/dist/utils/utils.d.ts +1 -0
- package/dist/utils/utils.js +3 -1
- package/package.json +2 -2
- package/src/data-structures/hash/hash-map.ts +11 -7
- package/src/data-structures/heap/heap.ts +132 -128
- package/src/data-structures/queue/deque.ts +976 -170
- package/src/types/data-structures/hash/hash-map.ts +0 -11
- package/src/types/helpers.ts +15 -0
- package/src/utils/utils.ts +2 -0
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
export declare const enum IterateDirection {
|
|
2
|
-
DEFAULT = 0,
|
|
3
|
-
REVERSE = 1
|
|
4
|
-
}
|
|
5
|
-
export type HashMapOptions<T> = {
|
|
6
|
-
sizeFunction?: number | (() => number);
|
|
7
|
-
fixedLength?: number;
|
|
8
|
-
forEach: (callback: (el: T) => void) => void;
|
|
9
|
-
};
|
|
10
1
|
export type HashMapLinkedNode<K, V> = {
|
|
11
2
|
key: K;
|
|
12
3
|
value: V;
|
package/dist/types/helpers.d.ts
CHANGED
|
@@ -6,3 +6,14 @@ export declare enum CP {
|
|
|
6
6
|
eq = "eq",
|
|
7
7
|
gt = "gt"
|
|
8
8
|
}
|
|
9
|
+
export declare const enum IterateDirection {
|
|
10
|
+
DEFAULT = 0,
|
|
11
|
+
REVERSE = 1
|
|
12
|
+
}
|
|
13
|
+
export interface IterableWithSize<T> extends Iterable<T> {
|
|
14
|
+
size: number | ((...args: any[]) => number);
|
|
15
|
+
}
|
|
16
|
+
export interface IterableWithLength<T> extends Iterable<T> {
|
|
17
|
+
length: number | ((...args: any[]) => number);
|
|
18
|
+
}
|
|
19
|
+
export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>;
|
package/dist/utils/utils.d.ts
CHANGED
|
@@ -21,3 +21,4 @@ export declare const getMSB: (value: number) => number;
|
|
|
21
21
|
export declare const rangeCheck: (index: number, min: number, max: number, message?: string) => void;
|
|
22
22
|
export declare const throwRangeError: (message?: string) => void;
|
|
23
23
|
export declare const isObjOrFunc: (input: unknown) => input is Record<string, unknown> | ((...args: any[]) => any);
|
|
24
|
+
export declare const calcMinUnitsRequired: (totalQuantity: number, unitSize: number) => number;
|
package/dist/utils/utils.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.isObjOrFunc = exports.throwRangeError = exports.rangeCheck = exports.getMSB = exports.trampolineAsync = exports.trampoline = exports.toThunk = exports.isThunk = exports.THUNK_SYMBOL = exports.arrayRemove = exports.uuidV4 = void 0;
|
|
12
|
+
exports.calcMinUnitsRequired = exports.isObjOrFunc = exports.throwRangeError = exports.rangeCheck = exports.getMSB = exports.trampolineAsync = exports.trampoline = exports.toThunk = exports.isThunk = exports.THUNK_SYMBOL = exports.arrayRemove = exports.uuidV4 = void 0;
|
|
13
13
|
const uuidV4 = function () {
|
|
14
14
|
return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) {
|
|
15
15
|
const r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
@@ -85,3 +85,5 @@ const isObjOrFunc = (input) => {
|
|
|
85
85
|
return (inputType === 'object' && input !== null) || inputType === 'function';
|
|
86
86
|
};
|
|
87
87
|
exports.isObjOrFunc = isObjOrFunc;
|
|
88
|
+
const calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
|
|
89
|
+
exports.calcMinUnitsRequired = calcMinUnitsRequired;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "linked-list-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.46.2",
|
|
4
4
|
"description": "Linked List, Doubly Linked List, Singly Linked List. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
"typescript": "^4.9.5"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"data-structure-typed": "^1.
|
|
69
|
+
"data-structure-typed": "^1.46.2"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { isObjOrFunc, rangeCheck, throwRangeError } from '../../utils';
|
|
10
|
-
import { HashMapLinkedNode,
|
|
10
|
+
import { HashMapLinkedNode, IterableWithSizeOrLength, IterateDirection } from '../../types';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Because the implementation of HashMap relies on JavaScript's built-in objects and arrays,
|
|
@@ -122,6 +122,10 @@ export class HashMapIterator<K, V> {
|
|
|
122
122
|
next() {
|
|
123
123
|
return this;
|
|
124
124
|
}
|
|
125
|
+
|
|
126
|
+
clone() {
|
|
127
|
+
return new HashMapIterator<K, V>(this._node, this._sentinel, this.hashMap, this.iterateDirection)
|
|
128
|
+
}
|
|
125
129
|
}
|
|
126
130
|
|
|
127
131
|
export class HashMap<K = any, V = any> {
|
|
@@ -134,18 +138,18 @@ export class HashMap<K = any, V = any> {
|
|
|
134
138
|
|
|
135
139
|
/**
|
|
136
140
|
* The constructor initializes a HashMap object with an optional initial set of key-value pairs.
|
|
137
|
-
* @param
|
|
141
|
+
* @param {Iterable<[K, V]>} elements - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
|
|
138
142
|
* V]>`. It is an array of key-value pairs, where each pair is represented as an array `[K, V]`. The
|
|
139
143
|
* `K` represents the type of the key and `V` represents the
|
|
140
144
|
*/
|
|
141
|
-
constructor(
|
|
145
|
+
constructor(elements: IterableWithSizeOrLength<[K, V]> = []) {
|
|
142
146
|
Object.setPrototypeOf(this._orgMap, null);
|
|
143
147
|
this._sentinel = <HashMapLinkedNode<K, V>>{};
|
|
144
148
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
145
149
|
|
|
146
|
-
|
|
150
|
+
for (const el of elements) {
|
|
147
151
|
this.set(el[0], el[1]);
|
|
148
|
-
}
|
|
152
|
+
}
|
|
149
153
|
}
|
|
150
154
|
|
|
151
155
|
protected _size = 0;
|
|
@@ -209,7 +213,7 @@ export class HashMap<K = any, V = any> {
|
|
|
209
213
|
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
210
214
|
* value (V).
|
|
211
215
|
*/
|
|
212
|
-
get
|
|
216
|
+
get first() {
|
|
213
217
|
if (this._size === 0) return;
|
|
214
218
|
return <[K, V]>[this._head.key, this._head.value];
|
|
215
219
|
}
|
|
@@ -222,7 +226,7 @@ export class HashMap<K = any, V = any> {
|
|
|
222
226
|
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
223
227
|
* data structure.
|
|
224
228
|
*/
|
|
225
|
-
get
|
|
229
|
+
get last() {
|
|
226
230
|
if (this._size === 0) return;
|
|
227
231
|
return <[K, V]>[this._tail.key, this._tail.value];
|
|
228
232
|
}
|
|
@@ -8,18 +8,18 @@
|
|
|
8
8
|
import type { Comparator, DFSOrderPattern } from '../../types';
|
|
9
9
|
|
|
10
10
|
export class Heap<E = any> {
|
|
11
|
-
constructor(options: { comparator: Comparator<E>;
|
|
11
|
+
constructor(options: { comparator: Comparator<E>; elements?: E[] }) {
|
|
12
12
|
this._comparator = options.comparator;
|
|
13
|
-
if (options.
|
|
14
|
-
this.
|
|
13
|
+
if (options.elements && options.elements.length > 0) {
|
|
14
|
+
this._elements = options.elements;
|
|
15
15
|
this.fix();
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
protected
|
|
19
|
+
protected _elements: E[] = [];
|
|
20
20
|
|
|
21
|
-
get
|
|
22
|
-
return this.
|
|
21
|
+
get elements(): E[] {
|
|
22
|
+
return this._elements;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
protected _comparator: Comparator<E>;
|
|
@@ -32,7 +32,7 @@ export class Heap<E = any> {
|
|
|
32
32
|
* Get the size (number of elements) of the heap.
|
|
33
33
|
*/
|
|
34
34
|
get size(): number {
|
|
35
|
-
return this.
|
|
35
|
+
return this.elements.length;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/**
|
|
@@ -40,25 +40,25 @@ export class Heap<E = any> {
|
|
|
40
40
|
* @returns The last element or undefined if the heap is empty.
|
|
41
41
|
*/
|
|
42
42
|
get leaf(): E | undefined {
|
|
43
|
-
return this.
|
|
43
|
+
return this.elements[this.size - 1] ?? undefined;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
|
-
* Static method that creates a binary heap from an array of
|
|
47
|
+
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
48
48
|
* @returns A new Heap instance.
|
|
49
49
|
* @param options
|
|
50
50
|
*/
|
|
51
|
-
static heapify<E>(options: {
|
|
51
|
+
static heapify<E>(options: { elements: E[]; comparator: Comparator<E> }): Heap<E> {
|
|
52
52
|
return new Heap<E>(options);
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
|
-
* Time Complexity: O(log n), where n is the number of
|
|
56
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
57
57
|
* Space Complexity: O(1)
|
|
58
58
|
*/
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
|
-
* Time Complexity: O(log n), where n is the number of
|
|
61
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
62
62
|
* Space Complexity: O(1)
|
|
63
63
|
*
|
|
64
64
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -69,56 +69,53 @@ export class Heap<E = any> {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
|
-
* Time Complexity: O(log n), where n is the number of
|
|
72
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
73
73
|
* Space Complexity: O(1)
|
|
74
74
|
*/
|
|
75
75
|
|
|
76
76
|
/**
|
|
77
|
-
* Time Complexity: O(log n), where n is the number of
|
|
77
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
78
78
|
* Space Complexity: O(1)
|
|
79
79
|
*
|
|
80
80
|
* Insert an element into the heap and maintain the heap properties.
|
|
81
81
|
* @param element - The element to be inserted.
|
|
82
82
|
*/
|
|
83
83
|
push(element: E): Heap<E> {
|
|
84
|
-
this.
|
|
85
|
-
this.
|
|
84
|
+
this._elements.push(element);
|
|
85
|
+
this._bubbleUp(this.elements.length - 1);
|
|
86
86
|
return this;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
/**
|
|
90
|
-
* Time Complexity: O(log n), where n is the number of
|
|
90
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
91
91
|
* Space Complexity: O(1)
|
|
92
92
|
*/
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
|
-
* Time Complexity: O(log n), where n is the number of
|
|
95
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
96
96
|
* Space Complexity: O(1)
|
|
97
97
|
*
|
|
98
98
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
99
99
|
* @returns The top element or undefined if the heap is empty.
|
|
100
100
|
*/
|
|
101
101
|
poll(): E | undefined {
|
|
102
|
-
if (this.
|
|
103
|
-
|
|
102
|
+
if (this.elements.length === 0) return;
|
|
103
|
+
const value = this.elements[0];
|
|
104
|
+
const last = this.elements.pop()!;
|
|
105
|
+
if (this.elements.length) {
|
|
106
|
+
this.elements[0] = last;
|
|
107
|
+
this._sinkDown(0, this.elements.length >> 1);
|
|
104
108
|
}
|
|
105
|
-
|
|
106
|
-
return this.nodes.pop() as E;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const topValue = this.nodes[0];
|
|
110
|
-
this.nodes[0] = this.nodes.pop() as E;
|
|
111
|
-
this.sinkDown(0);
|
|
112
|
-
return topValue;
|
|
109
|
+
return value;
|
|
113
110
|
}
|
|
114
111
|
|
|
115
112
|
/**
|
|
116
|
-
* Time Complexity: O(log n), where n is the number of
|
|
113
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
117
114
|
* Space Complexity: O(1)
|
|
118
115
|
*/
|
|
119
116
|
|
|
120
117
|
/**
|
|
121
|
-
* Time Complexity: O(log n), where n is the number of
|
|
118
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
122
119
|
* Space Complexity: O(1)
|
|
123
120
|
*
|
|
124
121
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -133,10 +130,7 @@ export class Heap<E = any> {
|
|
|
133
130
|
* @returns The top element or undefined if the heap is empty.
|
|
134
131
|
*/
|
|
135
132
|
peek(): E | undefined {
|
|
136
|
-
|
|
137
|
-
return undefined;
|
|
138
|
-
}
|
|
139
|
-
return this.nodes[0];
|
|
133
|
+
return this.elements[0];
|
|
140
134
|
}
|
|
141
135
|
|
|
142
136
|
/**
|
|
@@ -148,36 +142,36 @@ export class Heap<E = any> {
|
|
|
148
142
|
}
|
|
149
143
|
|
|
150
144
|
/**
|
|
151
|
-
* Reset the
|
|
145
|
+
* Reset the elements of the heap. Make the elements empty.
|
|
152
146
|
*/
|
|
153
147
|
clear() {
|
|
154
|
-
this.
|
|
148
|
+
this._elements = [];
|
|
155
149
|
}
|
|
156
150
|
|
|
157
151
|
/**
|
|
158
|
-
* Time Complexity: O(n), where n is the number of elements in the
|
|
152
|
+
* Time Complexity: O(n), where n is the number of elements in the elements array.
|
|
159
153
|
* Space Complexity: O(n)
|
|
160
154
|
*/
|
|
161
155
|
|
|
162
156
|
/**
|
|
163
|
-
* Time Complexity: O(n), where n is the number of elements in the
|
|
157
|
+
* Time Complexity: O(n), where n is the number of elements in the elements array.
|
|
164
158
|
* Space Complexity: O(n)
|
|
165
159
|
*
|
|
166
|
-
* Clear and add
|
|
167
|
-
* @param
|
|
160
|
+
* Clear and add elements of the heap
|
|
161
|
+
* @param elements
|
|
168
162
|
*/
|
|
169
|
-
refill(
|
|
170
|
-
this.
|
|
163
|
+
refill(elements: E[]) {
|
|
164
|
+
this._elements = elements;
|
|
171
165
|
this.fix();
|
|
172
166
|
}
|
|
173
167
|
|
|
174
168
|
/**
|
|
175
|
-
* Time Complexity: O(n), where n is the number of
|
|
169
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
176
170
|
* Space Complexity: O(1)
|
|
177
171
|
*/
|
|
178
172
|
|
|
179
173
|
/**
|
|
180
|
-
* Time Complexity: O(n), where n is the number of
|
|
174
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
181
175
|
* Space Complexity: O(1)
|
|
182
176
|
*
|
|
183
177
|
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
@@ -185,16 +179,47 @@ export class Heap<E = any> {
|
|
|
185
179
|
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
186
180
|
*/
|
|
187
181
|
has(element: E): boolean {
|
|
188
|
-
return this.
|
|
182
|
+
return this.elements.includes(element);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 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.
|
|
187
|
+
* Space Complexity: O(1)
|
|
188
|
+
*/
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 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.
|
|
192
|
+
* Space Complexity: O(1)
|
|
193
|
+
*
|
|
194
|
+
* The `delete` function removes an element from an array-like data structure, maintaining the order
|
|
195
|
+
* and structure of the remaining elements.
|
|
196
|
+
* @param {E} element - The `element` parameter represents the element that you want to delete from
|
|
197
|
+
* the array `this.elements`.
|
|
198
|
+
* @returns The `delete` function is returning a boolean value. It returns `true` if the element was
|
|
199
|
+
* successfully deleted from the array, and `false` if the element was not found in the array.
|
|
200
|
+
*/
|
|
201
|
+
delete(element: E) {
|
|
202
|
+
const index = this.elements.indexOf(element);
|
|
203
|
+
if (index < 0) return false;
|
|
204
|
+
if (index === 0) {
|
|
205
|
+
this.pop();
|
|
206
|
+
} else if (index === this.elements.length - 1) {
|
|
207
|
+
this.elements.pop();
|
|
208
|
+
} else {
|
|
209
|
+
this.elements.splice(index, 1, this.elements.pop()!);
|
|
210
|
+
this._bubbleUp(index);
|
|
211
|
+
this._sinkDown(index, this.elements.length >> 1);
|
|
212
|
+
}
|
|
213
|
+
return true;
|
|
189
214
|
}
|
|
190
215
|
|
|
191
216
|
/**
|
|
192
|
-
* Time Complexity: O(n), where n is the number of
|
|
217
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
193
218
|
* Space Complexity: O(h), where h is the height of the heap.
|
|
194
219
|
*/
|
|
195
220
|
|
|
196
221
|
/**
|
|
197
|
-
* Time Complexity: O(n), where n is the number of
|
|
222
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
198
223
|
* Space Complexity: O(h), where h is the height of the heap.
|
|
199
224
|
*
|
|
200
225
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
@@ -209,16 +234,16 @@ export class Heap<E = any> {
|
|
|
209
234
|
if (index < this.size) {
|
|
210
235
|
if (order === 'in') {
|
|
211
236
|
dfsHelper(2 * index + 1);
|
|
212
|
-
result.push(this.
|
|
237
|
+
result.push(this.elements[index]);
|
|
213
238
|
dfsHelper(2 * index + 2);
|
|
214
239
|
} else if (order === 'pre') {
|
|
215
|
-
result.push(this.
|
|
240
|
+
result.push(this.elements[index]);
|
|
216
241
|
dfsHelper(2 * index + 1);
|
|
217
242
|
dfsHelper(2 * index + 2);
|
|
218
243
|
} else if (order === 'post') {
|
|
219
244
|
dfsHelper(2 * index + 1);
|
|
220
245
|
dfsHelper(2 * index + 2);
|
|
221
|
-
result.push(this.
|
|
246
|
+
result.push(this.elements[index]);
|
|
222
247
|
}
|
|
223
248
|
}
|
|
224
249
|
};
|
|
@@ -241,15 +266,7 @@ export class Heap<E = any> {
|
|
|
241
266
|
* @returns An array containing the elements of the heap.
|
|
242
267
|
*/
|
|
243
268
|
toArray(): E[] {
|
|
244
|
-
return [...this.
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Time Complexity: O(1)
|
|
249
|
-
* Space Complexity: O(1)
|
|
250
|
-
*/
|
|
251
|
-
getNodes(): E[] {
|
|
252
|
-
return this.nodes;
|
|
269
|
+
return [...this.elements];
|
|
253
270
|
}
|
|
254
271
|
|
|
255
272
|
/**
|
|
@@ -266,7 +283,7 @@ export class Heap<E = any> {
|
|
|
266
283
|
*/
|
|
267
284
|
clone(): Heap<E> {
|
|
268
285
|
const clonedHeap = new Heap<E>({ comparator: this.comparator });
|
|
269
|
-
clonedHeap.
|
|
286
|
+
clonedHeap._elements = [...this.elements];
|
|
270
287
|
return clonedHeap;
|
|
271
288
|
}
|
|
272
289
|
|
|
@@ -298,35 +315,13 @@ export class Heap<E = any> {
|
|
|
298
315
|
*/
|
|
299
316
|
|
|
300
317
|
/**
|
|
301
|
-
* Time Complexity: O(
|
|
318
|
+
* Time Complexity: O(n)
|
|
302
319
|
* Space Complexity: O(1)
|
|
303
320
|
*
|
|
304
|
-
*
|
|
305
|
-
* @param index - The index of the newly added element.
|
|
321
|
+
* Fix the entire heap to maintain heap properties.
|
|
306
322
|
*/
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
// while (index > 0) {
|
|
310
|
-
// const parentIndex = (index - 1) >> 1;
|
|
311
|
-
// const parent = this.nodes[parentIndex];
|
|
312
|
-
// if (this.comparator(element, parent) < 0) {
|
|
313
|
-
// this.nodes[index] = parent;
|
|
314
|
-
// this.nodes[parentIndex] = element;
|
|
315
|
-
// index = parentIndex;
|
|
316
|
-
// } else {
|
|
317
|
-
// break;
|
|
318
|
-
// }
|
|
319
|
-
// }
|
|
320
|
-
|
|
321
|
-
const item = this.nodes[index];
|
|
322
|
-
while (index > 0) {
|
|
323
|
-
const parent = (index - 1) >> 1;
|
|
324
|
-
const parentItem = this.nodes[parent];
|
|
325
|
-
if (this.comparator(parentItem, item) <= 0) break;
|
|
326
|
-
this.nodes[index] = parentItem;
|
|
327
|
-
index = parent;
|
|
328
|
-
}
|
|
329
|
-
this.nodes[index] = item;
|
|
323
|
+
fix() {
|
|
324
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--) this._sinkDown(i, this.elements.length >> 1);
|
|
330
325
|
}
|
|
331
326
|
|
|
332
327
|
/**
|
|
@@ -338,28 +333,19 @@ export class Heap<E = any> {
|
|
|
338
333
|
* Time Complexity: O(log n)
|
|
339
334
|
* Space Complexity: O(1)
|
|
340
335
|
*
|
|
341
|
-
*
|
|
342
|
-
* @param index - The index
|
|
336
|
+
* Float operation to maintain heap properties after adding an element.
|
|
337
|
+
* @param index - The index of the newly added element.
|
|
343
338
|
*/
|
|
344
|
-
protected
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
}
|
|
353
|
-
if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
|
|
354
|
-
targetIndex = rightChildIndex;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
if (targetIndex !== index) {
|
|
358
|
-
const temp = this.nodes[index];
|
|
359
|
-
this.nodes[index] = this.nodes[targetIndex];
|
|
360
|
-
this.nodes[targetIndex] = temp;
|
|
361
|
-
this.sinkDown(targetIndex);
|
|
339
|
+
protected _bubbleUp(index: number) {
|
|
340
|
+
const element = this.elements[index];
|
|
341
|
+
while (index > 0) {
|
|
342
|
+
const parent = (index - 1) >> 1;
|
|
343
|
+
const parentItem = this.elements[parent];
|
|
344
|
+
if (this._comparator(parentItem, element) <= 0) break;
|
|
345
|
+
this.elements[index] = parentItem;
|
|
346
|
+
index = parent;
|
|
362
347
|
}
|
|
348
|
+
this.elements[index] = element;
|
|
363
349
|
}
|
|
364
350
|
|
|
365
351
|
/**
|
|
@@ -368,13 +354,31 @@ export class Heap<E = any> {
|
|
|
368
354
|
*/
|
|
369
355
|
|
|
370
356
|
/**
|
|
371
|
-
* Time Complexity: O(n)
|
|
357
|
+
* Time Complexity: O(log n)
|
|
372
358
|
* Space Complexity: O(1)
|
|
373
359
|
*
|
|
374
|
-
*
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
360
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
361
|
+
* @param index - The index from which to start sinking.
|
|
362
|
+
* @param halfLength
|
|
363
|
+
*/
|
|
364
|
+
protected _sinkDown(index: number, halfLength: number) {
|
|
365
|
+
const element = this.elements[index];
|
|
366
|
+
while (index < halfLength) {
|
|
367
|
+
let left = index << 1 | 1;
|
|
368
|
+
const right = left + 1;
|
|
369
|
+
let minItem = this.elements[left];
|
|
370
|
+
if (
|
|
371
|
+
right < this.elements.length &&
|
|
372
|
+
this._comparator(minItem, this.elements[right]) > 0
|
|
373
|
+
) {
|
|
374
|
+
left = right;
|
|
375
|
+
minItem = this.elements[right];
|
|
376
|
+
}
|
|
377
|
+
if (this._comparator(minItem, element) >= 0) break;
|
|
378
|
+
this.elements[index] = minItem;
|
|
379
|
+
index = left;
|
|
380
|
+
}
|
|
381
|
+
this.elements[index] = element;
|
|
378
382
|
}
|
|
379
383
|
}
|
|
380
384
|
|
|
@@ -500,22 +504,22 @@ export class FibonacciHeap<E> {
|
|
|
500
504
|
}
|
|
501
505
|
|
|
502
506
|
/**
|
|
503
|
-
* Time Complexity: O(n), where n is the number of
|
|
507
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
504
508
|
* Space Complexity: O(1)
|
|
505
509
|
*/
|
|
506
510
|
|
|
507
511
|
/**
|
|
508
|
-
* Time Complexity: O(n), where n is the number of
|
|
512
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
509
513
|
* Space Complexity: O(1)
|
|
510
514
|
*
|
|
511
515
|
* Get the size (number of elements) of the heap.
|
|
512
516
|
* @param {FibonacciHeapNode<E>} head - The head of the linked list.
|
|
513
517
|
* @protected
|
|
514
|
-
* @returns FibonacciHeapNode<E>[] - An array containing the
|
|
518
|
+
* @returns FibonacciHeapNode<E>[] - An array containing the elements of the linked list.
|
|
515
519
|
*/
|
|
516
520
|
consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[] {
|
|
517
|
-
const
|
|
518
|
-
if (!head) return
|
|
521
|
+
const elements: FibonacciHeapNode<E>[] = [];
|
|
522
|
+
if (!head) return elements;
|
|
519
523
|
|
|
520
524
|
let node: FibonacciHeapNode<E> | undefined = head;
|
|
521
525
|
let flag = false;
|
|
@@ -525,12 +529,12 @@ export class FibonacciHeap<E> {
|
|
|
525
529
|
else if (node === head) flag = true;
|
|
526
530
|
|
|
527
531
|
if (node) {
|
|
528
|
-
|
|
532
|
+
elements.push(node);
|
|
529
533
|
node = node.right;
|
|
530
534
|
}
|
|
531
535
|
}
|
|
532
536
|
|
|
533
|
-
return
|
|
537
|
+
return elements;
|
|
534
538
|
}
|
|
535
539
|
|
|
536
540
|
/**
|
|
@@ -552,12 +556,12 @@ export class FibonacciHeap<E> {
|
|
|
552
556
|
}
|
|
553
557
|
|
|
554
558
|
/**
|
|
555
|
-
* Time Complexity: O(log n), where n is the number of
|
|
559
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
556
560
|
* Space Complexity: O(1)
|
|
557
561
|
*/
|
|
558
562
|
|
|
559
563
|
/**
|
|
560
|
-
* Time Complexity: O(log n), where n is the number of
|
|
564
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
561
565
|
* Space Complexity: O(1)
|
|
562
566
|
*
|
|
563
567
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -568,12 +572,12 @@ export class FibonacciHeap<E> {
|
|
|
568
572
|
}
|
|
569
573
|
|
|
570
574
|
/**
|
|
571
|
-
* Time Complexity: O(log n), where n is the number of
|
|
575
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
572
576
|
* Space Complexity: O(1)
|
|
573
577
|
*/
|
|
574
578
|
|
|
575
579
|
/**
|
|
576
|
-
* Time Complexity: O(log n), where n is the number of
|
|
580
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
577
581
|
* Space Complexity: O(1)
|
|
578
582
|
*
|
|
579
583
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -584,8 +588,8 @@ export class FibonacciHeap<E> {
|
|
|
584
588
|
|
|
585
589
|
const z = this.min!;
|
|
586
590
|
if (z.child) {
|
|
587
|
-
const
|
|
588
|
-
for (const node of
|
|
591
|
+
const elements = this.consumeLinkedList(z.child);
|
|
592
|
+
for (const node of elements) {
|
|
589
593
|
this.mergeWithRoot(node);
|
|
590
594
|
node.parent = undefined;
|
|
591
595
|
}
|
|
@@ -737,12 +741,12 @@ export class FibonacciHeap<E> {
|
|
|
737
741
|
}
|
|
738
742
|
|
|
739
743
|
/**
|
|
740
|
-
* Time Complexity: O(n log n), where n is the number of
|
|
744
|
+
* Time Complexity: O(n log n), where n is the number of elements in the heap.
|
|
741
745
|
* Space Complexity: O(n)
|
|
742
746
|
*/
|
|
743
747
|
|
|
744
748
|
/**
|
|
745
|
-
* Time Complexity: O(n log n), where n is the number of
|
|
749
|
+
* Time Complexity: O(n log n), where n is the number of elements in the heap.
|
|
746
750
|
* Space Complexity: O(n)
|
|
747
751
|
*
|
|
748
752
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -750,13 +754,13 @@ export class FibonacciHeap<E> {
|
|
|
750
754
|
*/
|
|
751
755
|
protected consolidate(): void {
|
|
752
756
|
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this.size);
|
|
753
|
-
const
|
|
757
|
+
const elements = this.consumeLinkedList(this.root);
|
|
754
758
|
let x: FibonacciHeapNode<E> | undefined,
|
|
755
759
|
y: FibonacciHeapNode<E> | undefined,
|
|
756
760
|
d: number,
|
|
757
761
|
t: FibonacciHeapNode<E> | undefined;
|
|
758
762
|
|
|
759
|
-
for (const node of
|
|
763
|
+
for (const node of elements) {
|
|
760
764
|
x = node;
|
|
761
765
|
d = x.degree;
|
|
762
766
|
|