data-structure-typed 1.33.7 → 1.33.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.
Files changed (149) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +25 -14
  3. package/package.json +22 -22
  4. package/.prettierrc.js +0 -16
  5. package/coverage/coverage-final.json +0 -68
  6. package/coverage/coverage-summary.json +0 -69
  7. package/docs/.nojekyll +0 -1
  8. package/docs/index.html +0 -875
  9. package/docs/modules.html +0 -336
  10. package/jest.config.js +0 -8
  11. package/src/data-structures/binary-tree/aa-tree.ts +0 -1
  12. package/src/data-structures/binary-tree/abstract-binary-tree.ts +0 -1608
  13. package/src/data-structures/binary-tree/avl-tree.ts +0 -307
  14. package/src/data-structures/binary-tree/b-tree.ts +0 -1
  15. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -76
  16. package/src/data-structures/binary-tree/binary-tree.ts +0 -47
  17. package/src/data-structures/binary-tree/bst.ts +0 -537
  18. package/src/data-structures/binary-tree/index.ts +0 -12
  19. package/src/data-structures/binary-tree/rb-tree.ts +0 -366
  20. package/src/data-structures/binary-tree/segment-tree.ts +0 -260
  21. package/src/data-structures/binary-tree/splay-tree.ts +0 -1
  22. package/src/data-structures/binary-tree/tree-multiset.ts +0 -700
  23. package/src/data-structures/binary-tree/two-three-tree.ts +0 -1
  24. package/src/data-structures/graph/abstract-graph.ts +0 -1040
  25. package/src/data-structures/graph/directed-graph.ts +0 -470
  26. package/src/data-structures/graph/index.ts +0 -4
  27. package/src/data-structures/graph/map-graph.ts +0 -129
  28. package/src/data-structures/graph/undirected-graph.ts +0 -274
  29. package/src/data-structures/hash/coordinate-map.ts +0 -67
  30. package/src/data-structures/hash/coordinate-set.ts +0 -56
  31. package/src/data-structures/hash/hash-map.ts +0 -203
  32. package/src/data-structures/hash/hash-table.ts +0 -277
  33. package/src/data-structures/hash/index.ts +0 -7
  34. package/src/data-structures/hash/pair.ts +0 -1
  35. package/src/data-structures/hash/tree-map.ts +0 -1
  36. package/src/data-structures/hash/tree-set.ts +0 -1
  37. package/src/data-structures/heap/heap.ts +0 -212
  38. package/src/data-structures/heap/index.ts +0 -3
  39. package/src/data-structures/heap/max-heap.ts +0 -31
  40. package/src/data-structures/heap/min-heap.ts +0 -32
  41. package/src/data-structures/index.ts +0 -11
  42. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -636
  43. package/src/data-structures/linked-list/index.ts +0 -3
  44. package/src/data-structures/linked-list/singly-linked-list.ts +0 -501
  45. package/src/data-structures/linked-list/skip-linked-list.ts +0 -166
  46. package/src/data-structures/matrix/index.ts +0 -4
  47. package/src/data-structures/matrix/matrix.ts +0 -27
  48. package/src/data-structures/matrix/matrix2d.ts +0 -213
  49. package/src/data-structures/matrix/navigator.ts +0 -121
  50. package/src/data-structures/matrix/vector2d.ts +0 -316
  51. package/src/data-structures/priority-queue/index.ts +0 -3
  52. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -56
  53. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -57
  54. package/src/data-structures/priority-queue/priority-queue.ts +0 -359
  55. package/src/data-structures/queue/deque.ts +0 -297
  56. package/src/data-structures/queue/index.ts +0 -2
  57. package/src/data-structures/queue/queue.ts +0 -191
  58. package/src/data-structures/stack/index.ts +0 -1
  59. package/src/data-structures/stack/stack.ts +0 -98
  60. package/src/data-structures/tree/index.ts +0 -1
  61. package/src/data-structures/tree/tree.ts +0 -69
  62. package/src/data-structures/trie/index.ts +0 -1
  63. package/src/data-structures/trie/trie.ts +0 -225
  64. package/src/index.ts +0 -4
  65. package/src/interfaces/abstract-binary-tree.ts +0 -189
  66. package/src/interfaces/abstract-graph.ts +0 -31
  67. package/src/interfaces/avl-tree.ts +0 -25
  68. package/src/interfaces/binary-tree.ts +0 -6
  69. package/src/interfaces/bst.ts +0 -31
  70. package/src/interfaces/directed-graph.ts +0 -20
  71. package/src/interfaces/doubly-linked-list.ts +0 -1
  72. package/src/interfaces/heap.ts +0 -1
  73. package/src/interfaces/index.ts +0 -15
  74. package/src/interfaces/navigator.ts +0 -1
  75. package/src/interfaces/priority-queue.ts +0 -1
  76. package/src/interfaces/rb-tree.ts +0 -9
  77. package/src/interfaces/segment-tree.ts +0 -1
  78. package/src/interfaces/singly-linked-list.ts +0 -1
  79. package/src/interfaces/tree-multiset.ts +0 -7
  80. package/src/interfaces/undirected-graph.ts +0 -6
  81. package/src/types/data-structures/abstract-binary-tree.ts +0 -50
  82. package/src/types/data-structures/abstract-graph.ts +0 -11
  83. package/src/types/data-structures/avl-tree.ts +0 -5
  84. package/src/types/data-structures/binary-tree.ts +0 -5
  85. package/src/types/data-structures/bst.ts +0 -13
  86. package/src/types/data-structures/directed-graph.ts +0 -8
  87. package/src/types/data-structures/doubly-linked-list.ts +0 -1
  88. package/src/types/data-structures/hash.ts +0 -1
  89. package/src/types/data-structures/heap.ts +0 -5
  90. package/src/types/data-structures/index.ts +0 -16
  91. package/src/types/data-structures/map-graph.ts +0 -1
  92. package/src/types/data-structures/navigator.ts +0 -13
  93. package/src/types/data-structures/priority-queue.ts +0 -9
  94. package/src/types/data-structures/rb-tree.ts +0 -8
  95. package/src/types/data-structures/segment-tree.ts +0 -1
  96. package/src/types/data-structures/singly-linked-list.ts +0 -1
  97. package/src/types/data-structures/tree-multiset.ts +0 -6
  98. package/src/types/helpers.ts +0 -1
  99. package/src/types/index.ts +0 -3
  100. package/src/types/utils/index.ts +0 -2
  101. package/src/types/utils/utils.ts +0 -6
  102. package/src/types/utils/validate-type.ts +0 -35
  103. package/src/utils/index.ts +0 -1
  104. package/src/utils/utils.ts +0 -79
  105. package/test/integration/avl-tree.test.ts +0 -108
  106. package/test/integration/bst.test.ts +0 -380
  107. package/test/integration/heap.test.js +0 -16
  108. package/test/integration/index.html +0 -44
  109. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +0 -108
  110. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -142
  111. package/test/unit/data-structures/binary-tree/bst.test.ts +0 -380
  112. package/test/unit/data-structures/binary-tree/overall.test.ts +0 -65
  113. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +0 -43
  114. package/test/unit/data-structures/binary-tree/segment-tree.test.ts +0 -50
  115. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +0 -461
  116. package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -5
  117. package/test/unit/data-structures/graph/directed-graph.test.ts +0 -519
  118. package/test/unit/data-structures/graph/index.ts +0 -2
  119. package/test/unit/data-structures/graph/map-graph.test.ts +0 -45
  120. package/test/unit/data-structures/graph/overall.test.ts +0 -49
  121. package/test/unit/data-structures/graph/undirected-graph.test.ts +0 -59
  122. package/test/unit/data-structures/hash/coordinate-map.test.ts +0 -54
  123. package/test/unit/data-structures/hash/coordinate-set.test.ts +0 -41
  124. package/test/unit/data-structures/hash/hash-map.test.ts +0 -104
  125. package/test/unit/data-structures/hash/hash-table.test.ts +0 -184
  126. package/test/unit/data-structures/heap/heap.test.ts +0 -55
  127. package/test/unit/data-structures/heap/max-heap.test.ts +0 -44
  128. package/test/unit/data-structures/heap/min-heap.test.ts +0 -82
  129. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -364
  130. package/test/unit/data-structures/linked-list/index.ts +0 -4
  131. package/test/unit/data-structures/linked-list/linked-list.test.ts +0 -35
  132. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -451
  133. package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +0 -13
  134. package/test/unit/data-structures/linked-list/skip-list.test.ts +0 -55
  135. package/test/unit/data-structures/matrix/matrix.test.ts +0 -54
  136. package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -138
  137. package/test/unit/data-structures/matrix/navigator.test.ts +0 -79
  138. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +0 -106
  139. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +0 -105
  140. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -27
  141. package/test/unit/data-structures/queue/deque.test.ts +0 -130
  142. package/test/unit/data-structures/queue/queue.test.ts +0 -199
  143. package/test/unit/data-structures/stack/stack.test.ts +0 -67
  144. package/test/unit/data-structures/tree/tree.test.ts +0 -39
  145. package/test/unit/data-structures/trie/trie.test.ts +0 -95
  146. package/test/utils/index.ts +0 -2
  147. package/test/utils/magnitude.ts +0 -21
  148. package/test/utils/number.ts +0 -3
  149. package/tsconfig.json +0 -29
@@ -1,277 +0,0 @@
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
- }
@@ -1,7 +0,0 @@
1
- export * from './hash-table';
2
- export * from './coordinate-map';
3
- export * from './coordinate-set';
4
- export * from './pair';
5
- export * from './tree-map';
6
- export * from './tree-set';
7
- export * from './hash-map';
@@ -1 +0,0 @@
1
- export class Pair {}
@@ -1 +0,0 @@
1
- export class TreeMap {}
@@ -1 +0,0 @@
1
- export class TreeSet {}
@@ -1,212 +0,0 @@
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
- }
@@ -1,3 +0,0 @@
1
- export * from './max-heap';
2
- export * from './min-heap';
3
- export * from './heap';
@@ -1,31 +0,0 @@
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
- }
@@ -1,32 +0,0 @@
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
- }
@@ -1,11 +0,0 @@
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';