heap-typed 1.38.0 → 1.38.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/binary-tree/avl-tree.d.ts +9 -9
- package/dist/data-structures/binary-tree/avl-tree.js +22 -22
- package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -31
- package/dist/data-structures/binary-tree/binary-tree.js +32 -32
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -9
- package/dist/data-structures/binary-tree/tree-multiset.js +23 -23
- package/dist/data-structures/hash/hash-map.d.ts +25 -25
- package/dist/data-structures/hash/hash-map.js +59 -59
- package/dist/data-structures/hash/hash-table.d.ts +34 -34
- package/dist/data-structures/hash/hash-table.js +99 -99
- package/dist/data-structures/heap/heap.d.ts +66 -66
- package/dist/data-structures/heap/heap.js +167 -167
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +3 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +17 -17
- package/dist/data-structures/linked-list/skip-linked-list.js +34 -34
- package/dist/data-structures/matrix/matrix2d.d.ts +7 -7
- package/dist/data-structures/matrix/matrix2d.js +9 -9
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +6 -6
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/package.json +1 -4
- package/src/data-structures/binary-tree/avl-tree.ts +28 -28
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +57 -57
- package/src/data-structures/binary-tree/bst.ts +4 -0
- package/src/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multiset.ts +30 -31
- package/src/data-structures/graph/abstract-graph.ts +10 -11
- package/src/data-structures/graph/directed-graph.ts +1 -2
- package/src/data-structures/graph/undirected-graph.ts +4 -5
- package/src/data-structures/hash/hash-map.ts +82 -76
- package/src/data-structures/hash/hash-table.ts +112 -109
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +182 -181
- package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +45 -38
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/matrix2d.ts +10 -10
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/queue/deque.ts +5 -4
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +9 -9
- package/src/index.ts +3 -3
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/helpers.ts +5 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -13,19 +13,46 @@ export declare class HashTableNode<K, V> {
|
|
|
13
13
|
}
|
|
14
14
|
import { HashFunction } from '../../types';
|
|
15
15
|
export declare class HashTable<K, V> {
|
|
16
|
-
get hashFn(): HashFunction<K>;
|
|
17
|
-
set hashFn(value: HashFunction<K>);
|
|
18
|
-
get buckets(): Array<HashTableNode<K, V> | null>;
|
|
19
|
-
set buckets(value: Array<HashTableNode<K, V> | null>);
|
|
20
|
-
get capacity(): number;
|
|
21
|
-
set capacity(value: number);
|
|
22
16
|
private static readonly DEFAULT_CAPACITY;
|
|
23
17
|
private static readonly LOAD_FACTOR;
|
|
18
|
+
constructor(capacity?: number, hashFn?: HashFunction<K>);
|
|
24
19
|
private _capacity;
|
|
20
|
+
get capacity(): number;
|
|
21
|
+
set capacity(value: number);
|
|
25
22
|
private _size;
|
|
23
|
+
get size(): number;
|
|
26
24
|
private _buckets;
|
|
25
|
+
get buckets(): Array<HashTableNode<K, V> | null>;
|
|
26
|
+
set buckets(value: Array<HashTableNode<K, V> | null>);
|
|
27
27
|
private _hashFn;
|
|
28
|
-
|
|
28
|
+
get hashFn(): HashFunction<K>;
|
|
29
|
+
set hashFn(value: HashFunction<K>);
|
|
30
|
+
/**
|
|
31
|
+
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
32
|
+
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
33
|
+
* table. It is of type K, which is a generic type representing the key's data type.
|
|
34
|
+
* @param {V} val - The parameter `val` represents the value that you want to associate with the given key in the hash
|
|
35
|
+
* table.
|
|
36
|
+
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
37
|
+
* value.
|
|
38
|
+
*/
|
|
39
|
+
set(key: K, val: V): void;
|
|
40
|
+
/**
|
|
41
|
+
* The `get` function retrieves the value associated with a given key from a hash table.
|
|
42
|
+
* @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
|
|
43
|
+
* structure.
|
|
44
|
+
* @returns The method is returning the value associated with the given key if it exists in the hash table. If the key is
|
|
45
|
+
* not found, it returns `undefined`.
|
|
46
|
+
*/
|
|
47
|
+
get(key: K): V | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* The delete function removes a key-value pair from a hash table.
|
|
50
|
+
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
51
|
+
* table.
|
|
52
|
+
* @returns Nothing is being returned. The `delete` method has a return type of `void`, which means it does not return
|
|
53
|
+
* any value.
|
|
54
|
+
*/
|
|
55
|
+
delete(key: K): void;
|
|
29
56
|
/**
|
|
30
57
|
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
31
58
|
* capacity of the data structure.
|
|
@@ -71,36 +98,9 @@ export declare class HashTable<K, V> {
|
|
|
71
98
|
* @returns a number, which is the hash value of the key.
|
|
72
99
|
*/
|
|
73
100
|
protected _objectHash(key: K): number;
|
|
74
|
-
/**
|
|
75
|
-
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
76
|
-
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
77
|
-
* table. It is of type K, which is a generic type representing the key's data type.
|
|
78
|
-
* @param {V} val - The parameter `val` represents the value that you want to associate with the given key in the hash
|
|
79
|
-
* table.
|
|
80
|
-
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
81
|
-
* value.
|
|
82
|
-
*/
|
|
83
|
-
set(key: K, val: V): void;
|
|
84
|
-
/**
|
|
85
|
-
* The `get` function retrieves the value associated with a given key from a hash table.
|
|
86
|
-
* @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
|
|
87
|
-
* structure.
|
|
88
|
-
* @returns The method is returning the value associated with the given key if it exists in the hash table. If the key is
|
|
89
|
-
* not found, it returns `undefined`.
|
|
90
|
-
*/
|
|
91
|
-
get(key: K): V | undefined;
|
|
92
|
-
/**
|
|
93
|
-
* The delete function removes a key-value pair from a hash table.
|
|
94
|
-
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
95
|
-
* table.
|
|
96
|
-
* @returns Nothing is being returned. The `delete` method has a return type of `void`, which means it does not return
|
|
97
|
-
* any value.
|
|
98
|
-
*/
|
|
99
|
-
delete(key: K): void;
|
|
100
101
|
/**
|
|
101
102
|
* The `expand` function increases the capacity of a hash table by creating a new array of buckets with double the
|
|
102
103
|
* capacity and rehashing all the existing key-value pairs into the new buckets.
|
|
103
104
|
*/
|
|
104
105
|
protected _expand(): void;
|
|
105
|
-
get size(): number;
|
|
106
106
|
}
|
|
@@ -17,113 +17,32 @@ class HashTableNode {
|
|
|
17
17
|
}
|
|
18
18
|
exports.HashTableNode = HashTableNode;
|
|
19
19
|
class HashTable {
|
|
20
|
-
get hashFn() {
|
|
21
|
-
return this._hashFn;
|
|
22
|
-
}
|
|
23
|
-
set hashFn(value) {
|
|
24
|
-
this._hashFn = value;
|
|
25
|
-
}
|
|
26
|
-
get buckets() {
|
|
27
|
-
return this._buckets;
|
|
28
|
-
}
|
|
29
|
-
set buckets(value) {
|
|
30
|
-
this._buckets = value;
|
|
31
|
-
}
|
|
32
|
-
get capacity() {
|
|
33
|
-
return this._capacity;
|
|
34
|
-
}
|
|
35
|
-
set capacity(value) {
|
|
36
|
-
this._capacity = value;
|
|
37
|
-
}
|
|
38
20
|
constructor(capacity = HashTable.DEFAULT_CAPACITY, hashFn) {
|
|
39
21
|
this._hashFn = hashFn || this._defaultHashFn;
|
|
40
22
|
this._capacity = Math.max(capacity, HashTable.DEFAULT_CAPACITY);
|
|
41
23
|
this._size = 0;
|
|
42
24
|
this._buckets = new Array(this._capacity).fill(null);
|
|
43
25
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
* capacity of the data structure.
|
|
47
|
-
* @param {K} key - The `key` parameter is the input value that needs to be hashed. It can be of any type, but in this
|
|
48
|
-
* code snippet, it is checked whether the key is a string or an object. If it is a string, the `_murmurStringHashFn`
|
|
49
|
-
* function is used to
|
|
50
|
-
* @returns the hash value of the key modulo the capacity of the data structure.
|
|
51
|
-
*/
|
|
52
|
-
_defaultHashFn(key) {
|
|
53
|
-
// Can be replaced with other hash functions as needed
|
|
54
|
-
const hashValue = typeof key === 'string' ? this._murmurStringHashFn(key) : this._objectHash(key);
|
|
55
|
-
return hashValue % this._capacity;
|
|
26
|
+
get capacity() {
|
|
27
|
+
return this._capacity;
|
|
56
28
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
* string hash function.
|
|
60
|
-
* @param {K} key - The `key` parameter is the input value for which we want to calculate the hash. It can be of any
|
|
61
|
-
* type, as it is generic (`K`). The function converts the `key` to a string using the `String()` function.
|
|
62
|
-
* @returns a number, which is the result of the multiplicative string hash function applied to the input key.
|
|
63
|
-
*/
|
|
64
|
-
_multiplicativeStringHashFn(key) {
|
|
65
|
-
const keyString = String(key);
|
|
66
|
-
let hash = 0;
|
|
67
|
-
for (let i = 0; i < keyString.length; i++) {
|
|
68
|
-
const charCode = keyString.charCodeAt(i);
|
|
69
|
-
// Some constants for adjusting the hash function
|
|
70
|
-
const A = 0.618033988749895;
|
|
71
|
-
const M = 1 << 30; // 2^30
|
|
72
|
-
hash = (hash * A + charCode) % M;
|
|
73
|
-
}
|
|
74
|
-
return Math.abs(hash); // Take absolute value to ensure non-negative numbers
|
|
29
|
+
set capacity(value) {
|
|
30
|
+
this._capacity = value;
|
|
75
31
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
* @param {K} key - The `key` parameter is the input value for which you want to calculate the hash. It can be of any
|
|
79
|
-
* type, but it will be converted to a string using the `String()` function before calculating the hash.
|
|
80
|
-
* @returns a number, which is the hash value calculated for the given key.
|
|
81
|
-
*/
|
|
82
|
-
_murmurStringHashFn(key) {
|
|
83
|
-
const keyString = String(key);
|
|
84
|
-
const seed = 0;
|
|
85
|
-
let hash = seed;
|
|
86
|
-
for (let i = 0; i < keyString.length; i++) {
|
|
87
|
-
const char = keyString.charCodeAt(i);
|
|
88
|
-
hash = (hash ^ char) * 0x5bd1e995;
|
|
89
|
-
hash = (hash ^ (hash >>> 15)) * 0x27d4eb2d;
|
|
90
|
-
hash = hash ^ (hash >>> 15);
|
|
91
|
-
}
|
|
92
|
-
return Math.abs(hash);
|
|
32
|
+
get size() {
|
|
33
|
+
return this._size;
|
|
93
34
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
* @param {K} key - The parameter "key" is of type K, which represents the type of the key that will be hashed.
|
|
97
|
-
* @returns The hash function is returning a number.
|
|
98
|
-
*/
|
|
99
|
-
_hash(key) {
|
|
100
|
-
return this.hashFn(key);
|
|
35
|
+
get buckets() {
|
|
36
|
+
return this._buckets;
|
|
101
37
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
* @param {string} key - The `key` parameter in the `stringHash` function is a string value that represents the input for
|
|
105
|
-
* which we want to calculate the hash value.
|
|
106
|
-
* @returns a number, which is the hash value of the input string.
|
|
107
|
-
*/
|
|
108
|
-
_stringHash(key) {
|
|
109
|
-
let hash = 0;
|
|
110
|
-
for (let i = 0; i < key.length; i++) {
|
|
111
|
-
hash = (hash * 31 + key.charCodeAt(i)) & 0xffffffff;
|
|
112
|
-
}
|
|
113
|
-
return hash;
|
|
38
|
+
set buckets(value) {
|
|
39
|
+
this._buckets = value;
|
|
114
40
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
* @returns a number, which is the hash value of the key.
|
|
121
|
-
*/
|
|
122
|
-
_objectHash(key) {
|
|
123
|
-
// If the key is an object, you can write a custom hash function
|
|
124
|
-
// For example, convert the object's properties to a string and use string hashing
|
|
125
|
-
// This is just an example; you should write a specific object hash function as needed
|
|
126
|
-
return this._stringHash(JSON.stringify(key));
|
|
41
|
+
get hashFn() {
|
|
42
|
+
return this._hashFn;
|
|
43
|
+
}
|
|
44
|
+
set hashFn(value) {
|
|
45
|
+
this._hashFn = value;
|
|
127
46
|
}
|
|
128
47
|
/**
|
|
129
48
|
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
@@ -208,6 +127,90 @@ class HashTable {
|
|
|
208
127
|
currentNode = currentNode.next;
|
|
209
128
|
}
|
|
210
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
132
|
+
* capacity of the data structure.
|
|
133
|
+
* @param {K} key - The `key` parameter is the input value that needs to be hashed. It can be of any type, but in this
|
|
134
|
+
* code snippet, it is checked whether the key is a string or an object. If it is a string, the `_murmurStringHashFn`
|
|
135
|
+
* function is used to
|
|
136
|
+
* @returns the hash value of the key modulo the capacity of the data structure.
|
|
137
|
+
*/
|
|
138
|
+
_defaultHashFn(key) {
|
|
139
|
+
// Can be replaced with other hash functions as needed
|
|
140
|
+
const hashValue = typeof key === 'string' ? this._murmurStringHashFn(key) : this._objectHash(key);
|
|
141
|
+
return hashValue % this._capacity;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* The `_multiplicativeStringHashFn` function calculates a hash value for a given string key using the multiplicative
|
|
145
|
+
* string hash function.
|
|
146
|
+
* @param {K} key - The `key` parameter is the input value for which we want to calculate the hash. It can be of any
|
|
147
|
+
* type, as it is generic (`K`). The function converts the `key` to a string using the `String()` function.
|
|
148
|
+
* @returns a number, which is the result of the multiplicative string hash function applied to the input key.
|
|
149
|
+
*/
|
|
150
|
+
_multiplicativeStringHashFn(key) {
|
|
151
|
+
const keyString = String(key);
|
|
152
|
+
let hash = 0;
|
|
153
|
+
for (let i = 0; i < keyString.length; i++) {
|
|
154
|
+
const charCode = keyString.charCodeAt(i);
|
|
155
|
+
// Some constants for adjusting the hash function
|
|
156
|
+
const A = 0.618033988749895;
|
|
157
|
+
const M = 1 << 30; // 2^30
|
|
158
|
+
hash = (hash * A + charCode) % M;
|
|
159
|
+
}
|
|
160
|
+
return Math.abs(hash); // Take absolute value to ensure non-negative numbers
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The function `_murmurStringHashFn` calculates a hash value for a given string key using the MurmurHash algorithm.
|
|
164
|
+
* @param {K} key - The `key` parameter is the input value for which you want to calculate the hash. It can be of any
|
|
165
|
+
* type, but it will be converted to a string using the `String()` function before calculating the hash.
|
|
166
|
+
* @returns a number, which is the hash value calculated for the given key.
|
|
167
|
+
*/
|
|
168
|
+
_murmurStringHashFn(key) {
|
|
169
|
+
const keyString = String(key);
|
|
170
|
+
const seed = 0;
|
|
171
|
+
let hash = seed;
|
|
172
|
+
for (let i = 0; i < keyString.length; i++) {
|
|
173
|
+
const char = keyString.charCodeAt(i);
|
|
174
|
+
hash = (hash ^ char) * 0x5bd1e995;
|
|
175
|
+
hash = (hash ^ (hash >>> 15)) * 0x27d4eb2d;
|
|
176
|
+
hash = hash ^ (hash >>> 15);
|
|
177
|
+
}
|
|
178
|
+
return Math.abs(hash);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The _hash function takes a key and returns a number.
|
|
182
|
+
* @param {K} key - The parameter "key" is of type K, which represents the type of the key that will be hashed.
|
|
183
|
+
* @returns The hash function is returning a number.
|
|
184
|
+
*/
|
|
185
|
+
_hash(key) {
|
|
186
|
+
return this.hashFn(key);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* The function calculates a hash value for a given string using the djb2 algorithm.
|
|
190
|
+
* @param {string} key - The `key` parameter in the `stringHash` function is a string value that represents the input for
|
|
191
|
+
* which we want to calculate the hash value.
|
|
192
|
+
* @returns a number, which is the hash value of the input string.
|
|
193
|
+
*/
|
|
194
|
+
_stringHash(key) {
|
|
195
|
+
let hash = 0;
|
|
196
|
+
for (let i = 0; i < key.length; i++) {
|
|
197
|
+
hash = (hash * 31 + key.charCodeAt(i)) & 0xffffffff;
|
|
198
|
+
}
|
|
199
|
+
return hash;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* The function `_objectHash` takes a key and returns a hash value, using a custom hash function for objects.
|
|
203
|
+
* @param {K} key - The parameter "key" is of type "K", which means it can be any type. It could be a string, number,
|
|
204
|
+
* boolean, object, or any other type of value. The purpose of the objectHash function is to generate a hash value for
|
|
205
|
+
* the key, which can be used for
|
|
206
|
+
* @returns a number, which is the hash value of the key.
|
|
207
|
+
*/
|
|
208
|
+
_objectHash(key) {
|
|
209
|
+
// If the key is an object, you can write a custom hash function
|
|
210
|
+
// For example, convert the object's properties to a string and use string hashing
|
|
211
|
+
// This is just an example; you should write a specific object hash function as needed
|
|
212
|
+
return this._stringHash(JSON.stringify(key));
|
|
213
|
+
}
|
|
211
214
|
/**
|
|
212
215
|
* The `expand` function increases the capacity of a hash table by creating a new array of buckets with double the
|
|
213
216
|
* capacity and rehashing all the existing key-value pairs into the new buckets.
|
|
@@ -236,9 +239,6 @@ class HashTable {
|
|
|
236
239
|
this._buckets = newBuckets;
|
|
237
240
|
this._capacity = newCapacity;
|
|
238
241
|
}
|
|
239
|
-
get size() {
|
|
240
|
-
return this._size;
|
|
241
|
-
}
|
|
242
242
|
}
|
|
243
243
|
exports.HashTable = HashTable;
|
|
244
244
|
HashTable.DEFAULT_CAPACITY = 16;
|
|
@@ -9,6 +9,22 @@ export declare class Heap<E> {
|
|
|
9
9
|
protected nodes: E[];
|
|
10
10
|
protected readonly comparator: Comparator<E>;
|
|
11
11
|
constructor(comparator: Comparator<E>);
|
|
12
|
+
/**
|
|
13
|
+
* Get the size (number of elements) of the heap.
|
|
14
|
+
*/
|
|
15
|
+
get size(): number;
|
|
16
|
+
/**
|
|
17
|
+
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
18
|
+
* @returns The last element or undefined if the heap is empty.
|
|
19
|
+
*/
|
|
20
|
+
get leaf(): E | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
23
|
+
* @param nodes
|
|
24
|
+
* @param comparator - Comparison function.
|
|
25
|
+
* @returns A new Heap instance.
|
|
26
|
+
*/
|
|
27
|
+
static heapify<E>(nodes: E[], comparator: Comparator<E>): Heap<E>;
|
|
12
28
|
/**
|
|
13
29
|
* Insert an element into the heap and maintain the heap properties.
|
|
14
30
|
* @param element - The element to be inserted.
|
|
@@ -29,34 +45,11 @@ export declare class Heap<E> {
|
|
|
29
45
|
* @returns The top element or undefined if the heap is empty.
|
|
30
46
|
*/
|
|
31
47
|
pop(): E | undefined;
|
|
32
|
-
/**
|
|
33
|
-
* Float operation to maintain heap properties after adding an element.
|
|
34
|
-
* @param index - The index of the newly added element.
|
|
35
|
-
*/
|
|
36
|
-
protected bubbleUp(index: number): void;
|
|
37
|
-
/**
|
|
38
|
-
* Sinking operation to maintain heap properties after removing the top element.
|
|
39
|
-
* @param index - The index from which to start sinking.
|
|
40
|
-
*/
|
|
41
|
-
protected sinkDown(index: number): void;
|
|
42
|
-
/**
|
|
43
|
-
* Fix the entire heap to maintain heap properties.
|
|
44
|
-
*/
|
|
45
|
-
protected fix(): void;
|
|
46
48
|
/**
|
|
47
49
|
* Peek at the top element of the heap without removing it.
|
|
48
50
|
* @returns The top element or undefined if the heap is empty.
|
|
49
51
|
*/
|
|
50
52
|
peek(): E | undefined;
|
|
51
|
-
/**
|
|
52
|
-
* Get the size (number of elements) of the heap.
|
|
53
|
-
*/
|
|
54
|
-
get size(): number;
|
|
55
|
-
/**
|
|
56
|
-
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
57
|
-
* @returns The last element or undefined if the heap is empty.
|
|
58
|
-
*/
|
|
59
|
-
get leaf(): E | undefined;
|
|
60
53
|
/**
|
|
61
54
|
* Check if the heap is empty.
|
|
62
55
|
* @returns True if the heap is empty, otherwise false.
|
|
@@ -100,12 +93,19 @@ export declare class Heap<E> {
|
|
|
100
93
|
*/
|
|
101
94
|
sort(): E[];
|
|
102
95
|
/**
|
|
103
|
-
*
|
|
104
|
-
* @param
|
|
105
|
-
* @param comparator - Comparison function.
|
|
106
|
-
* @returns A new Heap instance.
|
|
96
|
+
* Float operation to maintain heap properties after adding an element.
|
|
97
|
+
* @param index - The index of the newly added element.
|
|
107
98
|
*/
|
|
108
|
-
|
|
99
|
+
protected bubbleUp(index: number): void;
|
|
100
|
+
/**
|
|
101
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
102
|
+
* @param index - The index from which to start sinking.
|
|
103
|
+
*/
|
|
104
|
+
protected sinkDown(index: number): void;
|
|
105
|
+
/**
|
|
106
|
+
* Fix the entire heap to maintain heap properties.
|
|
107
|
+
*/
|
|
108
|
+
protected fix(): void;
|
|
109
109
|
}
|
|
110
110
|
export declare class FibonacciHeapNode<E> {
|
|
111
111
|
element: E;
|
|
@@ -119,33 +119,15 @@ export declare class FibonacciHeapNode<E> {
|
|
|
119
119
|
}
|
|
120
120
|
export declare class FibonacciHeap<E> {
|
|
121
121
|
root?: FibonacciHeapNode<E>;
|
|
122
|
-
protected min?: FibonacciHeapNode<E>;
|
|
123
122
|
size: number;
|
|
123
|
+
protected min?: FibonacciHeapNode<E>;
|
|
124
124
|
protected readonly comparator: Comparator<E>;
|
|
125
125
|
constructor(comparator?: Comparator<E>);
|
|
126
|
-
/**
|
|
127
|
-
* Default comparator function used by the heap.
|
|
128
|
-
* @param {E} a
|
|
129
|
-
* @param {E} b
|
|
130
|
-
* @protected
|
|
131
|
-
*/
|
|
132
|
-
protected defaultComparator(a: E, b: E): number;
|
|
133
126
|
/**
|
|
134
127
|
* Get the size (number of elements) of the heap.
|
|
135
128
|
* @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
|
|
136
129
|
*/
|
|
137
130
|
clear(): void;
|
|
138
|
-
/**
|
|
139
|
-
* Create a new node.
|
|
140
|
-
* @param element
|
|
141
|
-
* @protected
|
|
142
|
-
*/
|
|
143
|
-
protected createNode(element: E): FibonacciHeapNode<E>;
|
|
144
|
-
/**
|
|
145
|
-
* Merge the given node with the root list.
|
|
146
|
-
* @param node - The node to be merged.
|
|
147
|
-
*/
|
|
148
|
-
protected mergeWithRoot(node: FibonacciHeapNode<E>): void;
|
|
149
131
|
/**
|
|
150
132
|
* O(1) time operation.
|
|
151
133
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -175,13 +157,6 @@ export declare class FibonacciHeap<E> {
|
|
|
175
157
|
* @returns FibonacciHeapNode<E>[] - An array containing the nodes of the linked list.
|
|
176
158
|
*/
|
|
177
159
|
consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[];
|
|
178
|
-
/**
|
|
179
|
-
* O(log n) time operation.
|
|
180
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
181
|
-
* @param node - The node to be removed.
|
|
182
|
-
* @protected
|
|
183
|
-
*/
|
|
184
|
-
protected removeFromRoot(node: FibonacciHeapNode<E>): void;
|
|
185
160
|
/**
|
|
186
161
|
* O(log n) time operation.
|
|
187
162
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -192,33 +167,58 @@ export declare class FibonacciHeap<E> {
|
|
|
192
167
|
/**
|
|
193
168
|
* O(log n) time operation.
|
|
194
169
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
195
|
-
* @
|
|
196
|
-
* @param x
|
|
197
|
-
* @protected
|
|
170
|
+
* @returns The top element or undefined if the heap is empty.
|
|
198
171
|
*/
|
|
199
|
-
|
|
172
|
+
poll(): E | undefined;
|
|
200
173
|
/**
|
|
201
174
|
* O(log n) time operation.
|
|
202
175
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
176
|
+
* @returns The top element or undefined if the heap is empty.
|
|
177
|
+
*/
|
|
178
|
+
pop(): E | undefined;
|
|
179
|
+
/**
|
|
180
|
+
* O(log n) time operation.
|
|
181
|
+
* merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
|
|
182
|
+
* @param heapToMerge
|
|
183
|
+
*/
|
|
184
|
+
merge(heapToMerge: FibonacciHeap<E>): void;
|
|
185
|
+
/**
|
|
186
|
+
* Default comparator function used by the heap.
|
|
187
|
+
* @param {E} a
|
|
188
|
+
* @param {E} b
|
|
203
189
|
* @protected
|
|
204
190
|
*/
|
|
205
|
-
protected
|
|
191
|
+
protected defaultComparator(a: E, b: E): number;
|
|
192
|
+
/**
|
|
193
|
+
* Create a new node.
|
|
194
|
+
* @param element
|
|
195
|
+
* @protected
|
|
196
|
+
*/
|
|
197
|
+
protected createNode(element: E): FibonacciHeapNode<E>;
|
|
198
|
+
/**
|
|
199
|
+
* Merge the given node with the root list.
|
|
200
|
+
* @param node - The node to be merged.
|
|
201
|
+
*/
|
|
202
|
+
protected mergeWithRoot(node: FibonacciHeapNode<E>): void;
|
|
206
203
|
/**
|
|
207
204
|
* O(log n) time operation.
|
|
208
205
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
209
|
-
* @
|
|
206
|
+
* @param node - The node to be removed.
|
|
207
|
+
* @protected
|
|
210
208
|
*/
|
|
211
|
-
|
|
209
|
+
protected removeFromRoot(node: FibonacciHeapNode<E>): void;
|
|
212
210
|
/**
|
|
213
211
|
* O(log n) time operation.
|
|
214
212
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
215
|
-
* @
|
|
213
|
+
* @param y
|
|
214
|
+
* @param x
|
|
215
|
+
* @protected
|
|
216
216
|
*/
|
|
217
|
-
|
|
217
|
+
protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
|
|
218
218
|
/**
|
|
219
219
|
* O(log n) time operation.
|
|
220
|
-
*
|
|
221
|
-
* @
|
|
220
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
221
|
+
* @protected
|
|
222
222
|
*/
|
|
223
|
-
|
|
223
|
+
protected consolidate(): void;
|
|
224
224
|
}
|