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