data-structure-typed 1.33.9 → 1.33.10
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/package.json +1 -1
- package/src/data-structures/binary-tree/aa-tree.ts +1 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +1608 -0
- package/src/data-structures/binary-tree/avl-tree.ts +307 -0
- package/src/data-structures/binary-tree/b-tree.ts +1 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +76 -0
- package/src/data-structures/binary-tree/binary-tree.ts +47 -0
- package/src/data-structures/binary-tree/bst.ts +537 -0
- package/src/data-structures/binary-tree/index.ts +12 -0
- package/src/data-structures/binary-tree/rb-tree.ts +366 -0
- package/src/data-structures/binary-tree/segment-tree.ts +260 -0
- package/src/data-structures/binary-tree/splay-tree.ts +1 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +700 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +1 -0
- package/src/data-structures/graph/abstract-graph.ts +1040 -0
- package/src/data-structures/graph/directed-graph.ts +470 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +129 -0
- package/src/data-structures/graph/undirected-graph.ts +274 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-map.ts +203 -0
- package/src/data-structures/hash/hash-table.ts +277 -0
- package/src/data-structures/hash/index.ts +7 -0
- package/src/data-structures/hash/pair.ts +1 -0
- package/src/data-structures/hash/tree-map.ts +1 -0
- package/src/data-structures/hash/tree-set.ts +1 -0
- package/src/data-structures/heap/heap.ts +212 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +31 -0
- package/src/data-structures/heap/min-heap.ts +32 -0
- package/src/data-structures/index.ts +11 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +636 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +501 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +166 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +213 -0
- package/src/data-structures/matrix/navigator.ts +121 -0
- package/src/data-structures/matrix/vector2d.ts +316 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +56 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/priority-queue.ts +359 -0
- package/src/data-structures/queue/deque.ts +297 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +191 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +69 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +225 -0
- package/src/index.ts +4 -0
- package/src/interfaces/abstract-binary-tree.ts +189 -0
- package/src/interfaces/abstract-graph.ts +31 -0
- package/src/interfaces/avl-tree.ts +25 -0
- package/src/interfaces/binary-tree.ts +6 -0
- package/src/interfaces/bst.ts +31 -0
- package/src/interfaces/directed-graph.ts +20 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +15 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/rb-tree.ts +9 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/interfaces/tree-multiset.ts +7 -0
- package/src/interfaces/undirected-graph.ts +6 -0
- package/src/types/data-structures/abstract-binary-tree.ts +50 -0
- package/src/types/data-structures/abstract-graph.ts +11 -0
- package/src/types/data-structures/avl-tree.ts +5 -0
- package/src/types/data-structures/binary-tree.ts +5 -0
- package/src/types/data-structures/bst.ts +13 -0
- package/src/types/data-structures/directed-graph.ts +8 -0
- package/src/types/data-structures/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/hash.ts +1 -0
- package/src/types/data-structures/heap.ts +5 -0
- package/src/types/data-structures/index.ts +16 -0
- package/src/types/data-structures/map-graph.ts +1 -0
- package/src/types/data-structures/navigator.ts +13 -0
- package/src/types/data-structures/priority-queue.ts +9 -0
- package/src/types/data-structures/rb-tree.ts +8 -0
- package/src/types/data-structures/segment-tree.ts +1 -0
- package/src/types/data-structures/singly-linked-list.ts +1 -0
- package/src/types/data-structures/tree-multiset.ts +6 -0
- package/src/types/helpers.ts +1 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +6 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.ts +79 -0
- package/test/integration/avl-tree.test.ts +108 -0
- package/test/integration/bst.test.ts +380 -0
- package/test/integration/heap.test.js +16 -0
- package/test/integration/index.html +52 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +108 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +142 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +380 -0
- package/test/unit/data-structures/binary-tree/overall.test.ts +65 -0
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +43 -0
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +50 -0
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +461 -0
- package/test/unit/data-structures/graph/abstract-graph.test.ts +5 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +519 -0
- package/test/unit/data-structures/graph/index.ts +2 -0
- package/test/unit/data-structures/graph/map-graph.test.ts +45 -0
- package/test/unit/data-structures/graph/overall.test.ts +49 -0
- package/test/unit/data-structures/graph/undirected-graph.test.ts +59 -0
- package/test/unit/data-structures/hash/coordinate-map.test.ts +54 -0
- package/test/unit/data-structures/hash/coordinate-set.test.ts +41 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +104 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +184 -0
- package/test/unit/data-structures/heap/heap.test.ts +55 -0
- package/test/unit/data-structures/heap/max-heap.test.ts +44 -0
- package/test/unit/data-structures/heap/min-heap.test.ts +82 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +364 -0
- package/test/unit/data-structures/linked-list/index.ts +4 -0
- package/test/unit/data-structures/linked-list/linked-list.test.ts +35 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +451 -0
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +13 -0
- package/test/unit/data-structures/linked-list/skip-list.test.ts +55 -0
- package/test/unit/data-structures/matrix/matrix.test.ts +54 -0
- package/test/unit/data-structures/matrix/matrix2d.test.ts +138 -0
- package/test/unit/data-structures/matrix/navigator.test.ts +79 -0
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +106 -0
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +105 -0
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +27 -0
- package/test/unit/data-structures/queue/deque.test.ts +130 -0
- package/test/unit/data-structures/queue/queue.test.ts +199 -0
- package/test/unit/data-structures/stack/stack.test.ts +67 -0
- package/test/unit/data-structures/tree/tree.test.ts +39 -0
- package/test/unit/data-structures/trie/trie.test.ts +95 -0
- package/test/utils/index.ts +2 -0
- package/test/utils/magnitude.ts +21 -0
- package/test/utils/number.ts +3 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export class HashTableNode<K, V> {
|
|
10
|
+
key: K;
|
|
11
|
+
val: V;
|
|
12
|
+
next: HashTableNode<K, V> | null;
|
|
13
|
+
|
|
14
|
+
constructor(key: K, val: V) {
|
|
15
|
+
this.key = key;
|
|
16
|
+
this.val = val;
|
|
17
|
+
this.next = null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
import {HashFunction} from '../../types';
|
|
22
|
+
|
|
23
|
+
export class HashTable<K, V> {
|
|
24
|
+
get hashFn(): HashFunction<K> {
|
|
25
|
+
return this._hashFn;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
set hashFn(value: HashFunction<K>) {
|
|
29
|
+
this._hashFn = value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get buckets(): Array<HashTableNode<K, V> | null> {
|
|
33
|
+
return this._buckets;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
set buckets(value: Array<HashTableNode<K, V> | null>) {
|
|
37
|
+
this._buckets = value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get capacity(): number {
|
|
41
|
+
return this._capacity;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
set capacity(value: number) {
|
|
45
|
+
this._capacity = value;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private static readonly DEFAULT_CAPACITY = 16;
|
|
49
|
+
private static readonly LOAD_FACTOR = 0.75;
|
|
50
|
+
|
|
51
|
+
private _capacity: number;
|
|
52
|
+
private _size: number;
|
|
53
|
+
private _buckets: Array<HashTableNode<K, V> | null>;
|
|
54
|
+
private _hashFn: HashFunction<K>;
|
|
55
|
+
|
|
56
|
+
constructor(capacity: number = HashTable.DEFAULT_CAPACITY, hashFn?: HashFunction<K>) {
|
|
57
|
+
this._hashFn = hashFn || this._defaultHashFn;
|
|
58
|
+
this._capacity = Math.max(capacity, HashTable.DEFAULT_CAPACITY);
|
|
59
|
+
this._size = 0;
|
|
60
|
+
this._buckets = new Array<HashTableNode<K, V> | null>(this._capacity).fill(null);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
65
|
+
* capacity of the data structure.
|
|
66
|
+
* @param {K} key - The `key` parameter is the input value that needs to be hashed. It can be of any type, but in this
|
|
67
|
+
* code snippet, it is checked whether the key is a string or an object. If it is a string, the `_murmurStringHashFn`
|
|
68
|
+
* function is used to
|
|
69
|
+
* @returns the hash value of the key modulo the capacity of the data structure.
|
|
70
|
+
*/
|
|
71
|
+
protected _defaultHashFn(key: K): number {
|
|
72
|
+
// Can be replaced with other hash functions as needed
|
|
73
|
+
const hashValue = typeof key === 'string' ? this._murmurStringHashFn(key) : this._objectHash(key);
|
|
74
|
+
return hashValue % this._capacity;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The `_multiplicativeStringHashFn` function calculates a hash value for a given string key using the multiplicative
|
|
79
|
+
* string hash function.
|
|
80
|
+
* @param {K} key - The `key` parameter is the input value for which we want to calculate the hash. It can be of any
|
|
81
|
+
* type, as it is generic (`K`). The function converts the `key` to a string using the `String()` function.
|
|
82
|
+
* @returns a number, which is the result of the multiplicative string hash function applied to the input key.
|
|
83
|
+
*/
|
|
84
|
+
protected _multiplicativeStringHashFn<K>(key: K): number {
|
|
85
|
+
const keyString = String(key);
|
|
86
|
+
let hash = 0;
|
|
87
|
+
for (let i = 0; i < keyString.length; i++) {
|
|
88
|
+
const charCode = keyString.charCodeAt(i);
|
|
89
|
+
// Some constants for adjusting the hash function
|
|
90
|
+
const A = 0.618033988749895;
|
|
91
|
+
const M = 1 << 30; // 2^30
|
|
92
|
+
hash = (hash * A + charCode) % M;
|
|
93
|
+
}
|
|
94
|
+
return Math.abs(hash); // Take absolute value to ensure non-negative numbers
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The function `_murmurStringHashFn` calculates a hash value for a given string key using the MurmurHash algorithm.
|
|
99
|
+
* @param {K} key - The `key` parameter is the input value for which you want to calculate the hash. It can be of any
|
|
100
|
+
* type, but it will be converted to a string using the `String()` function before calculating the hash.
|
|
101
|
+
* @returns a number, which is the hash value calculated for the given key.
|
|
102
|
+
*/
|
|
103
|
+
protected _murmurStringHashFn<K>(key: K): number {
|
|
104
|
+
const keyString = String(key);
|
|
105
|
+
const seed = 0;
|
|
106
|
+
let hash = seed;
|
|
107
|
+
|
|
108
|
+
for (let i = 0; i < keyString.length; i++) {
|
|
109
|
+
const char = keyString.charCodeAt(i);
|
|
110
|
+
hash = (hash ^ char) * 0x5bd1e995;
|
|
111
|
+
hash = (hash ^ (hash >>> 15)) * 0x27d4eb2d;
|
|
112
|
+
hash = hash ^ (hash >>> 15);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return Math.abs(hash);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The _hash function takes a key and returns a number.
|
|
120
|
+
* @param {K} key - The parameter "key" is of type K, which represents the type of the key that will be hashed.
|
|
121
|
+
* @returns The hash function is returning a number.
|
|
122
|
+
*/
|
|
123
|
+
protected _hash(key: K): number {
|
|
124
|
+
return this.hashFn(key);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The function calculates a hash value for a given string using the djb2 algorithm.
|
|
129
|
+
* @param {string} key - The `key` parameter in the `stringHash` function is a string value that represents the input for
|
|
130
|
+
* which we want to calculate the hash value.
|
|
131
|
+
* @returns a number, which is the hash value of the input string.
|
|
132
|
+
*/
|
|
133
|
+
protected _stringHash(key: string): number {
|
|
134
|
+
let hash = 0;
|
|
135
|
+
for (let i = 0; i < key.length; i++) {
|
|
136
|
+
hash = (hash * 31 + key.charCodeAt(i)) & 0xffffffff;
|
|
137
|
+
}
|
|
138
|
+
return hash;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The function `_objectHash` takes a key and returns a hash value, using a custom hash function for objects.
|
|
143
|
+
* @param {K} key - The parameter "key" is of type "K", which means it can be any type. It could be a string, number,
|
|
144
|
+
* boolean, object, or any other type of value. The purpose of the objectHash function is to generate a hash value for
|
|
145
|
+
* the key, which can be used for
|
|
146
|
+
* @returns a number, which is the hash value of the key.
|
|
147
|
+
*/
|
|
148
|
+
protected _objectHash(key: K): number {
|
|
149
|
+
// If the key is an object, you can write a custom hash function
|
|
150
|
+
// For example, convert the object's properties to a string and use string hashing
|
|
151
|
+
// This is just an example; you should write a specific object hash function as needed
|
|
152
|
+
return this._stringHash(JSON.stringify(key));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
157
|
+
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
158
|
+
* table. It is of type K, which is a generic type representing the key's data type.
|
|
159
|
+
* @param {V} val - The parameter `val` represents the value that you want to associate with the given key in the hash
|
|
160
|
+
* table.
|
|
161
|
+
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
162
|
+
* value.
|
|
163
|
+
*/
|
|
164
|
+
set(key: K, val: V): void {
|
|
165
|
+
const index = this._hash(key);
|
|
166
|
+
const newNode = new HashTableNode<K, V>(key, val);
|
|
167
|
+
|
|
168
|
+
if (!this._buckets[index]) {
|
|
169
|
+
this._buckets[index] = newNode;
|
|
170
|
+
} else {
|
|
171
|
+
// Handle collisions, consider using open addressing, etc.
|
|
172
|
+
let currentNode = this._buckets[index]!;
|
|
173
|
+
while (currentNode) {
|
|
174
|
+
if (currentNode.key === key) {
|
|
175
|
+
// If the key already exists, update the value
|
|
176
|
+
currentNode.val = val;
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (!currentNode.next) {
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
currentNode = currentNode.next;
|
|
183
|
+
}
|
|
184
|
+
// Add to the end of the linked list
|
|
185
|
+
currentNode.next = newNode;
|
|
186
|
+
}
|
|
187
|
+
this._size++;
|
|
188
|
+
|
|
189
|
+
// If the load factor is too high, resize the hash table
|
|
190
|
+
if (this._size / this._capacity >= HashTable.LOAD_FACTOR) {
|
|
191
|
+
this._expand();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The `get` function retrieves the value associated with a given key from a hash table.
|
|
197
|
+
* @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
|
|
198
|
+
* structure.
|
|
199
|
+
* @returns The method is returning the value associated with the given key if it exists in the hash table. If the key is
|
|
200
|
+
* not found, it returns `undefined`.
|
|
201
|
+
*/
|
|
202
|
+
get(key: K): V | undefined {
|
|
203
|
+
const index = this._hash(key);
|
|
204
|
+
let currentNode = this._buckets[index];
|
|
205
|
+
|
|
206
|
+
while (currentNode) {
|
|
207
|
+
if (currentNode.key === key) {
|
|
208
|
+
return currentNode.val;
|
|
209
|
+
}
|
|
210
|
+
currentNode = currentNode.next;
|
|
211
|
+
}
|
|
212
|
+
return undefined; // Key not found
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The remove function removes a key-value pair from a hash table.
|
|
217
|
+
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
218
|
+
* table.
|
|
219
|
+
* @returns Nothing is being returned. The `remove` method has a return type of `void`, which means it does not return
|
|
220
|
+
* any value.
|
|
221
|
+
*/
|
|
222
|
+
remove(key: K): void {
|
|
223
|
+
const index = this._hash(key);
|
|
224
|
+
let currentNode = this._buckets[index];
|
|
225
|
+
let prevNode: HashTableNode<K, V> | null = null;
|
|
226
|
+
|
|
227
|
+
while (currentNode) {
|
|
228
|
+
if (currentNode.key === key) {
|
|
229
|
+
if (prevNode) {
|
|
230
|
+
prevNode.next = currentNode.next;
|
|
231
|
+
} else {
|
|
232
|
+
this._buckets[index] = currentNode.next;
|
|
233
|
+
}
|
|
234
|
+
this._size--;
|
|
235
|
+
currentNode.next = null; // Release memory
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
prevNode = currentNode;
|
|
239
|
+
currentNode = currentNode.next;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The `expand` function increases the capacity of a hash table by creating a new array of buckets with double the
|
|
245
|
+
* capacity and rehashing all the existing key-value pairs into the new buckets.
|
|
246
|
+
*/
|
|
247
|
+
protected _expand(): void {
|
|
248
|
+
const newCapacity = this._capacity * 2;
|
|
249
|
+
const newBuckets = new Array<HashTableNode<K, V> | null>(newCapacity).fill(null);
|
|
250
|
+
|
|
251
|
+
for (const bucket of this._buckets) {
|
|
252
|
+
let currentNode = bucket;
|
|
253
|
+
while (currentNode) {
|
|
254
|
+
const newIndex = this._hash(currentNode.key);
|
|
255
|
+
const newNode = new HashTableNode<K, V>(currentNode.key, currentNode.val);
|
|
256
|
+
|
|
257
|
+
if (!newBuckets[newIndex]) {
|
|
258
|
+
newBuckets[newIndex] = newNode;
|
|
259
|
+
} else {
|
|
260
|
+
let currentNewNode = newBuckets[newIndex]!;
|
|
261
|
+
while (currentNewNode.next) {
|
|
262
|
+
currentNewNode = currentNewNode.next;
|
|
263
|
+
}
|
|
264
|
+
currentNewNode.next = newNode;
|
|
265
|
+
}
|
|
266
|
+
currentNode = currentNode.next;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
this._buckets = newBuckets;
|
|
271
|
+
this._capacity = newCapacity;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
get size(): number {
|
|
275
|
+
return this._size;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class Pair {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class TreeMap {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class TreeSet {}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import {PriorityQueue} from '../priority-queue';
|
|
9
|
+
import type {HeapOptions} from '../../types';
|
|
10
|
+
|
|
11
|
+
export class HeapItem<V = any> {
|
|
12
|
+
/**
|
|
13
|
+
* The constructor function initializes an instance of a class with a priority and a value.
|
|
14
|
+
* @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
|
|
15
|
+
* optional and has a default value of `NaN`.
|
|
16
|
+
* @param {V | null} [val=null] - The `val` parameter is of type `V | null`, which means it can accept a value of type
|
|
17
|
+
* `V` or `null`.
|
|
18
|
+
*/
|
|
19
|
+
constructor(priority: number = Number.MAX_SAFE_INTEGER, val: V | null = null) {
|
|
20
|
+
this._val = val;
|
|
21
|
+
this._priority = priority;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private _priority: number;
|
|
25
|
+
|
|
26
|
+
get priority(): number {
|
|
27
|
+
return this._priority;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
set priority(value: number) {
|
|
31
|
+
this._priority = value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private _val: V | null;
|
|
35
|
+
|
|
36
|
+
get val(): V | null {
|
|
37
|
+
return this._val;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
set val(value: V | null) {
|
|
41
|
+
this._val = value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export abstract class Heap<V = number> {
|
|
46
|
+
/**
|
|
47
|
+
* The function is a constructor for a class that initializes a priority callback function based on the
|
|
48
|
+
* options provided.
|
|
49
|
+
* @param [options] - An optional object that contains configuration options for the Heap.
|
|
50
|
+
*/
|
|
51
|
+
protected constructor(options?: HeapOptions<V>) {
|
|
52
|
+
if (options) {
|
|
53
|
+
const {priorityExtractor} = options;
|
|
54
|
+
if (priorityExtractor !== undefined && typeof priorityExtractor !== 'function') {
|
|
55
|
+
throw new Error('.constructor expects a valid priority function');
|
|
56
|
+
}
|
|
57
|
+
this._priorityExtractor = priorityExtractor || (el => +el);
|
|
58
|
+
} else {
|
|
59
|
+
this._priorityExtractor = el => +el;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected abstract _pq: PriorityQueue<HeapItem<V>>;
|
|
64
|
+
|
|
65
|
+
get pq() {
|
|
66
|
+
return this._pq;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
protected _priorityExtractor: (val: V) => number;
|
|
70
|
+
get priorityExtractor() {
|
|
71
|
+
return this._priorityExtractor;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The function returns the size of a priority queue.
|
|
76
|
+
* @returns The size of the priority queue.
|
|
77
|
+
*/
|
|
78
|
+
get size(): number {
|
|
79
|
+
return this._pq.size;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The function checks if a priority queue is empty.
|
|
84
|
+
* @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
|
|
85
|
+
*/
|
|
86
|
+
isEmpty(): boolean {
|
|
87
|
+
return this._pq.size < 1;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
peek(isItem?: undefined): V | undefined;
|
|
91
|
+
peek(isItem: false): V | undefined;
|
|
92
|
+
peek(isItem: true): HeapItem<V> | null;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The `peek` function returns the top item in the priority queue without removing it.
|
|
96
|
+
* @returns The `peek()` method is returning either a `HeapItem<V>` object or `null`.Returns an val with the highest priority in the queue
|
|
97
|
+
*/
|
|
98
|
+
peek(isItem?: boolean): HeapItem<V> | null | V | undefined {
|
|
99
|
+
isItem = isItem ?? false;
|
|
100
|
+
const peeked = this._pq.peek();
|
|
101
|
+
|
|
102
|
+
return isItem ? peeked : peeked?.val;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
peekLast(isItem?: undefined): V | undefined;
|
|
106
|
+
peekLast(isItem: false): V | undefined;
|
|
107
|
+
peekLast(isItem: true): HeapItem<V> | null;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The `peekLast` function returns the last item in the heap.
|
|
111
|
+
* @returns The method `peekLast()` returns either a `HeapItem<V>` object or `null`.Returns an val with the lowest priority in the queue
|
|
112
|
+
*/
|
|
113
|
+
peekLast(isItem?: boolean): HeapItem<V> | null | V | undefined {
|
|
114
|
+
isItem = isItem ?? false;
|
|
115
|
+
const leafItem = this._pq.leaf();
|
|
116
|
+
|
|
117
|
+
return isItem ? leafItem : leafItem?.val;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The `add` function adds an val to a priority queue with an optional priority value.
|
|
122
|
+
* @param {V} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
|
|
123
|
+
* type.
|
|
124
|
+
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
|
|
125
|
+
* val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
|
|
126
|
+
* the value of `val`. If the `val` parameter is not a number, then the
|
|
127
|
+
* @returns The `add` method returns the instance of the `Heap` class.
|
|
128
|
+
* @throws {Error} if priority is not a valid number
|
|
129
|
+
*/
|
|
130
|
+
add(priority: number, val?: V): Heap<V> {
|
|
131
|
+
val = val === undefined ? (priority as unknown as V) : val;
|
|
132
|
+
this._pq.add(new HeapItem<V>(priority, val));
|
|
133
|
+
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
poll(isItem?: undefined): V | undefined;
|
|
138
|
+
poll(isItem: false): V | undefined;
|
|
139
|
+
poll(isItem: true): HeapItem<V> | null;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an val with the highest priority in the queue
|
|
143
|
+
* @returns either a HeapItem<V> object or null.
|
|
144
|
+
*/
|
|
145
|
+
poll(isItem?: boolean): HeapItem<V> | null | V | undefined {
|
|
146
|
+
isItem = isItem ?? false;
|
|
147
|
+
const top = this._pq.poll();
|
|
148
|
+
if (!top) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return isItem ? top : top.val;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The function checks if a given node or value exists in the priority queue.
|
|
157
|
+
* @param {V | HeapItem<V>} node - The parameter `node` can be of type `V` or `HeapItem<V>`.
|
|
158
|
+
* @returns a boolean value.
|
|
159
|
+
*/
|
|
160
|
+
has(node: V | HeapItem<V>): boolean {
|
|
161
|
+
if (node instanceof HeapItem) {
|
|
162
|
+
return this.pq.getNodes().includes(node);
|
|
163
|
+
} else {
|
|
164
|
+
return (
|
|
165
|
+
this.pq.getNodes().findIndex(item => {
|
|
166
|
+
return item.val === node;
|
|
167
|
+
}) !== -1
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
toArray(isItem?: undefined): (V | undefined)[];
|
|
173
|
+
toArray(isItem: false): (V | undefined)[];
|
|
174
|
+
toArray(isItem: true): (HeapItem<V> | null)[];
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The `toArray` function returns an array of `HeapItem<V>` objects.
|
|
178
|
+
* @returns An array of HeapItem<V> objects.Returns a sorted list of vals
|
|
179
|
+
*/
|
|
180
|
+
toArray(isItem?: boolean): (HeapItem<V> | null | V | undefined)[] {
|
|
181
|
+
isItem = isItem ?? false;
|
|
182
|
+
const itemArray = this._pq.toArray();
|
|
183
|
+
|
|
184
|
+
return isItem ? itemArray : itemArray.map(item => item.val);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
sort(isItem?: undefined): (V | undefined)[];
|
|
188
|
+
sort(isItem: false): (V | undefined)[];
|
|
189
|
+
sort(isItem: true): (HeapItem<V> | null)[];
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The function sorts the elements in the priority queue and returns either the sorted items or their values depending
|
|
193
|
+
* on the value of the isItem parameter.
|
|
194
|
+
* @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the sorted result should
|
|
195
|
+
* be an array of `HeapItem<V>` objects or an array of the values (`V`) of those objects. If `isItem` is `true`, the
|
|
196
|
+
* sorted result will be an array of `HeapItem
|
|
197
|
+
* @returns an array of either `HeapItem<V>`, `null`, `V`, or `undefined` values.
|
|
198
|
+
*/
|
|
199
|
+
sort(isItem?: boolean): (HeapItem<V> | null | V | undefined)[] {
|
|
200
|
+
isItem = isItem ?? false;
|
|
201
|
+
const sorted = this._pq.sort();
|
|
202
|
+
|
|
203
|
+
return isItem ? sorted : sorted.map(item => item.val);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The clear function clears the priority queue.
|
|
208
|
+
*/
|
|
209
|
+
clear(): void {
|
|
210
|
+
this._pq.clear();
|
|
211
|
+
}
|
|
212
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {Heap, HeapItem} from './heap';
|
|
10
|
+
import {PriorityQueue} from '../priority-queue';
|
|
11
|
+
import type {HeapOptions} from '../../types';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @class MaxHeap
|
|
15
|
+
* @extends Heap
|
|
16
|
+
*/
|
|
17
|
+
export class MaxHeap<V = any> extends Heap<V> {
|
|
18
|
+
protected _pq: PriorityQueue<HeapItem<V>>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The constructor initializes a PriorityQueue with a custom comparator function.
|
|
22
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
|
|
23
|
+
* type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
|
|
24
|
+
*/
|
|
25
|
+
constructor(options?: HeapOptions<V>) {
|
|
26
|
+
super(options);
|
|
27
|
+
this._pq = new PriorityQueue<HeapItem<V>>({
|
|
28
|
+
comparator: (a, b) => b.priority - a.priority
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {Heap, HeapItem} from './heap';
|
|
10
|
+
import {PriorityQueue} from '../priority-queue';
|
|
11
|
+
import type {HeapOptions} from '../../types';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @class MinHeap
|
|
15
|
+
* @extends Heap
|
|
16
|
+
*/
|
|
17
|
+
export class MinHeap<V = any> extends Heap<V> {
|
|
18
|
+
protected _pq: PriorityQueue<HeapItem<V>>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
|
|
22
|
+
* objects.
|
|
23
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
|
|
24
|
+
* type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
|
|
25
|
+
*/
|
|
26
|
+
constructor(options?: HeapOptions<V>) {
|
|
27
|
+
super(options);
|
|
28
|
+
this._pq = new PriorityQueue<HeapItem<V>>({
|
|
29
|
+
comparator: (a, b) => a.priority - b.priority
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './hash';
|
|
2
|
+
export * from './linked-list';
|
|
3
|
+
export * from './stack';
|
|
4
|
+
export * from './queue';
|
|
5
|
+
export * from './graph';
|
|
6
|
+
export * from './binary-tree';
|
|
7
|
+
export * from './tree';
|
|
8
|
+
export * from './heap';
|
|
9
|
+
export * from './priority-queue';
|
|
10
|
+
export * from './matrix';
|
|
11
|
+
export * from './trie';
|