avl-tree-typed 1.49.3 → 1.49.5

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 (61) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +1 -1
  2. package/dist/data-structures/binary-tree/binary-tree.d.ts +2 -14
  3. package/dist/data-structures/binary-tree/binary-tree.js +19 -49
  4. package/dist/data-structures/binary-tree/tree-multimap.d.ts +0 -16
  5. package/dist/data-structures/binary-tree/tree-multimap.js +1 -43
  6. package/dist/data-structures/graph/abstract-graph.d.ts +2 -11
  7. package/dist/data-structures/graph/abstract-graph.js +3 -19
  8. package/dist/data-structures/graph/directed-graph.js +4 -0
  9. package/dist/data-structures/hash/hash-map.d.ts +1 -1
  10. package/dist/data-structures/hash/hash-map.js +2 -2
  11. package/dist/data-structures/heap/heap.js +2 -3
  12. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  13. package/dist/data-structures/matrix/index.d.ts +0 -2
  14. package/dist/data-structures/matrix/index.js +0 -2
  15. package/dist/data-structures/matrix/matrix.d.ts +128 -10
  16. package/dist/data-structures/matrix/matrix.js +400 -15
  17. package/dist/data-structures/queue/deque.d.ts +2 -2
  18. package/dist/data-structures/queue/deque.js +5 -7
  19. package/dist/data-structures/queue/queue.d.ts +1 -1
  20. package/dist/types/data-structures/base/base.d.ts +1 -1
  21. package/dist/types/data-structures/heap/heap.d.ts +1 -1
  22. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  23. package/dist/utils/utils.d.ts +1 -0
  24. package/dist/utils/utils.js +6 -1
  25. package/package.json +2 -2
  26. package/src/data-structures/base/index.ts +1 -1
  27. package/src/data-structures/base/iterable-base.ts +7 -10
  28. package/src/data-structures/binary-tree/avl-tree.ts +15 -8
  29. package/src/data-structures/binary-tree/binary-tree.ts +57 -74
  30. package/src/data-structures/binary-tree/bst.ts +16 -13
  31. package/src/data-structures/binary-tree/rb-tree.ts +16 -10
  32. package/src/data-structures/binary-tree/tree-multimap.ts +11 -48
  33. package/src/data-structures/graph/abstract-graph.ts +14 -24
  34. package/src/data-structures/graph/directed-graph.ts +8 -6
  35. package/src/data-structures/graph/map-graph.ts +6 -1
  36. package/src/data-structures/graph/undirected-graph.ts +4 -7
  37. package/src/data-structures/hash/hash-map.ts +18 -16
  38. package/src/data-structures/heap/heap.ts +7 -10
  39. package/src/data-structures/heap/max-heap.ts +2 -1
  40. package/src/data-structures/heap/min-heap.ts +2 -1
  41. package/src/data-structures/linked-list/singly-linked-list.ts +3 -5
  42. package/src/data-structures/matrix/index.ts +0 -2
  43. package/src/data-structures/matrix/matrix.ts +442 -13
  44. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
  45. package/src/data-structures/queue/deque.ts +18 -39
  46. package/src/data-structures/queue/queue.ts +1 -1
  47. package/src/interfaces/binary-tree.ts +7 -2
  48. package/src/types/common.ts +4 -4
  49. package/src/types/data-structures/base/base.ts +14 -3
  50. package/src/types/data-structures/base/index.ts +1 -1
  51. package/src/types/data-structures/graph/abstract-graph.ts +4 -2
  52. package/src/types/data-structures/hash/hash-map.ts +3 -3
  53. package/src/types/data-structures/heap/heap.ts +2 -2
  54. package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
  55. package/src/utils/utils.ts +7 -1
  56. package/dist/data-structures/matrix/matrix2d.d.ts +0 -107
  57. package/dist/data-structures/matrix/matrix2d.js +0 -199
  58. package/dist/data-structures/matrix/vector2d.d.ts +0 -200
  59. package/dist/data-structures/matrix/vector2d.js +0 -290
  60. package/src/data-structures/matrix/matrix2d.ts +0 -211
  61. package/src/data-structures/matrix/vector2d.ts +0 -315
@@ -182,7 +182,6 @@ export class DirectedGraph<
182
182
  * Space Complexity: O(1)
183
183
  */
184
184
 
185
-
186
185
  /**
187
186
  * Time Complexity: O(E) where E is the number of edgeMap
188
187
  * Space Complexity: O(1)
@@ -240,7 +239,7 @@ export class DirectedGraph<
240
239
  * (`VertexKey`).
241
240
  * @returns The method is returning a boolean value.
242
241
  */
243
- override deleteVertex(vertexOrKey: VO | VertexKey): boolean {
242
+ deleteVertex(vertexOrKey: VO | VertexKey): boolean {
244
243
  let vertexKey: VertexKey;
245
244
  let vertex: VO | undefined;
246
245
  if (this.isVertexKey(vertexOrKey)) {
@@ -248,12 +247,16 @@ export class DirectedGraph<
248
247
  vertexKey = vertexOrKey;
249
248
  } else {
250
249
  vertex = vertexOrKey;
251
- vertexKey = this._getVertexKey(vertexOrKey)
250
+ vertexKey = this._getVertexKey(vertexOrKey);
252
251
  }
253
252
 
254
253
  if (vertex) {
255
- this._outEdgeMap.delete(vertex)
256
- this._inEdgeMap.delete(vertex)
254
+ const neighbors = this.getNeighbors(vertex);
255
+ for (const neighbor of neighbors) {
256
+ this._inEdgeMap.delete(neighbor);
257
+ }
258
+ this._outEdgeMap.delete(vertex);
259
+ this._inEdgeMap.delete(vertex);
257
260
  }
258
261
 
259
262
  return this._vertexMap.delete(vertexKey);
@@ -596,7 +599,6 @@ export class DirectedGraph<
596
599
  }
597
600
  }
598
601
 
599
-
600
602
  /**
601
603
  * Time Complexity: O(1)
602
604
  * Space Complexity: O(1)
@@ -84,7 +84,12 @@ export class MapGraph<
84
84
  * @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
85
85
  * @returns The method is returning a new instance of the `MapVertex` class, casted as type `VO`.
86
86
  */
87
- override createVertex(key: VertexKey, value?: V, lat: number = this.originCoord[0], long: number = this.originCoord[1]): VO {
87
+ override createVertex(
88
+ key: VertexKey,
89
+ value?: V,
90
+ lat: number = this.originCoord[0],
91
+ long: number = this.originCoord[1]
92
+ ): VO {
88
93
  return new MapVertex(key, value, lat, long) as VO;
89
94
  }
90
95
 
@@ -162,7 +162,6 @@ export class UndirectedGraph<
162
162
  * Space Complexity: O(1)
163
163
  */
164
164
 
165
-
166
165
  /**
167
166
  * Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
168
167
  * Space Complexity: O(1)
@@ -192,7 +191,6 @@ export class UndirectedGraph<
192
191
 
193
192
  if (oneSide && otherSide) {
194
193
  return this.deleteEdgeBetween(oneSide, otherSide);
195
-
196
194
  } else {
197
195
  return;
198
196
  }
@@ -212,7 +210,7 @@ export class UndirectedGraph<
212
210
  * (`VertexKey`).
213
211
  * @returns The method is returning a boolean value.
214
212
  */
215
- override deleteVertex(vertexOrKey: VO | VertexKey): boolean {
213
+ deleteVertex(vertexOrKey: VO | VertexKey): boolean {
216
214
  let vertexKey: VertexKey;
217
215
  let vertex: VO | undefined;
218
216
  if (this.isVertexKey(vertexOrKey)) {
@@ -220,10 +218,10 @@ export class UndirectedGraph<
220
218
  vertexKey = vertexOrKey;
221
219
  } else {
222
220
  vertex = vertexOrKey;
223
- vertexKey = this._getVertexKey(vertexOrKey)
221
+ vertexKey = this._getVertexKey(vertexOrKey);
224
222
  }
225
223
 
226
- const neighbors = this.getNeighbors(vertexOrKey)
224
+ const neighbors = this.getNeighbors(vertexOrKey);
227
225
 
228
226
  if (vertex) {
229
227
  neighbors.forEach(neighbor => {
@@ -234,9 +232,8 @@ export class UndirectedGraph<
234
232
  });
235
233
  this._edgeMap.set(neighbor, restEdges);
236
234
  }
237
- })
235
+ });
238
236
  this._edgeMap.delete(vertex);
239
-
240
237
  }
241
238
 
242
239
  return this._vertexMap.delete(vertexKey);
@@ -27,9 +27,12 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
27
27
  * @param [options] - The `options` parameter is an optional object that can contain additional
28
28
  * configuration options for the constructor. In this case, it has one property:
29
29
  */
30
- constructor(elements: Iterable<[K, V]> = [], options?: {
31
- hashFn: (key: K) => string
32
- }) {
30
+ constructor(
31
+ elements: Iterable<[K, V]> = [],
32
+ options?: {
33
+ hashFn: (key: K) => string;
34
+ }
35
+ ) {
33
36
  super();
34
37
  if (options) {
35
38
  const { hashFn } = options;
@@ -73,7 +76,6 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
73
76
  this._size++;
74
77
  }
75
78
  this._objMap.set(key, value);
76
-
77
79
  } else {
78
80
  const strKey = this._getNoObjKey(key);
79
81
  if (this._store[strKey] === undefined) {
@@ -137,7 +139,7 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
137
139
  delete(key: K): boolean {
138
140
  if (this._isObjKey(key)) {
139
141
  if (this._objMap.has(key)) {
140
- this._size--
142
+ this._size--;
141
143
  }
142
144
 
143
145
  return this._objMap.delete(key);
@@ -235,19 +237,19 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
235
237
 
236
238
  protected _hashFn: (key: K) => string = (key: K) => String(key);
237
239
 
238
- protected _isObjKey(key: any): key is (object | ((...args: any[]) => any)) {
240
+ protected _isObjKey(key: any): key is object | ((...args: any[]) => any) {
239
241
  const keyType = typeof key;
240
- return (keyType === 'object' || keyType === 'function') && key !== null
242
+ return (keyType === 'object' || keyType === 'function') && key !== null;
241
243
  }
242
244
 
243
245
  protected _getNoObjKey(key: K): string {
244
246
  const keyType = typeof key;
245
247
 
246
248
  let strKey: string;
247
- if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") {
249
+ if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') {
248
250
  strKey = this._hashFn(key);
249
251
  } else {
250
- if (keyType === "number") {
252
+ if (keyType === 'number') {
251
253
  // TODO numeric key should has its own hash
252
254
  strKey = <string>key;
253
255
  } else {
@@ -264,7 +266,6 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
264
266
  * 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
265
267
  */
266
268
  export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
267
-
268
269
  protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {};
269
270
  protected _objMap = new WeakMap<object, HashMapLinkedNode<K, V | undefined>>();
270
271
  protected _head: HashMapLinkedNode<K, V | undefined>;
@@ -273,12 +274,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
273
274
  protected _hashFn: (key: K) => string;
274
275
  protected _objHashFn: (key: K) => object;
275
276
 
276
-
277
- constructor(elements?: Iterable<[K, V]>, options: HashMapOptions<K> = {
278
-
279
- hashFn: (key: K) => String(key),
280
- objHashFn: (key: K) => (<object>key)
281
- }) {
277
+ constructor(
278
+ elements?: Iterable<[K, V]>,
279
+ options: HashMapOptions<K> = {
280
+ hashFn: (key: K) => String(key),
281
+ objHashFn: (key: K) => <object>key
282
+ }
283
+ ) {
282
284
  super();
283
285
  this._sentinel = <HashMapLinkedNode<K, V>>{};
284
286
  this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
@@ -31,13 +31,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
31
31
  } else {
32
32
  return a - b;
33
33
  }
34
- }
34
+ };
35
35
  if (options) {
36
- this.options = options
36
+ this.options = options;
37
37
  } else {
38
38
  this.options = {
39
39
  comparator: defaultComparator
40
- }
40
+ };
41
41
  }
42
42
 
43
43
  if (elements) {
@@ -225,7 +225,8 @@ export class Heap<E = any> extends IterableElementBase<E> {
225
225
 
226
226
  // Auxiliary recursive function, traverses the binary heap according to the traversal order
227
227
  const _dfs = (index: number) => {
228
- const left = 2 * index + 1, right = left + 1;
228
+ const left = 2 * index + 1,
229
+ right = left + 1;
229
230
  if (index < this.size) {
230
231
  if (order === 'in') {
231
232
  _dfs(left);
@@ -380,7 +381,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
380
381
  * original Heap.
381
382
  */
382
383
  map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T> {
383
-
384
384
  const mappedHeap: Heap<T> = new Heap<T>([], { comparator: comparator });
385
385
  let index = 0;
386
386
  for (const el of this) {
@@ -432,13 +432,10 @@ export class Heap<E = any> extends IterableElementBase<E> {
432
432
  protected _sinkDown(index: number, halfLength: number): boolean {
433
433
  const element = this.elements[index];
434
434
  while (index < halfLength) {
435
- let left = index << 1 | 1;
435
+ let left = (index << 1) | 1;
436
436
  const right = left + 1;
437
437
  let minItem = this.elements[left];
438
- if (
439
- right < this.elements.length &&
440
- this.options.comparator(minItem, this.elements[right]) > 0
441
- ) {
438
+ if (right < this.elements.length && this.options.comparator(minItem, this.elements[right]) > 0) {
442
439
  left = right;
443
440
  minItem = this.elements[right];
444
441
  }
@@ -29,7 +29,8 @@ export class MaxHeap<E = any> extends Heap<E> {
29
29
  return b - a;
30
30
  }
31
31
  }
32
- }) {
32
+ }
33
+ ) {
33
34
  super(elements, options);
34
35
  }
35
36
  }
@@ -29,7 +29,8 @@ export class MinHeap<E = any> extends Heap<E> {
29
29
  return a - b;
30
30
  }
31
31
  }
32
- }) {
32
+ }
33
+ ) {
33
34
  super(elements, options);
34
35
  }
35
36
  }
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { ElementCallback } from "../../types";
9
- import { IterableElementBase } from "../base";
8
+ import type { ElementCallback } from '../../types';
9
+ import { IterableElementBase } from '../base';
10
10
 
11
11
  export class SinglyLinkedListNode<E = any> {
12
12
  value: E;
@@ -33,8 +33,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
33
33
  this._tail = undefined;
34
34
  this._size = 0;
35
35
  if (elements) {
36
- for (const el of elements)
37
- this.push(el);
36
+ for (const el of elements) this.push(el);
38
37
  }
39
38
  }
40
39
 
@@ -716,7 +715,6 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
716
715
  return filteredList;
717
716
  }
718
717
 
719
-
720
718
  /**
721
719
  * Time Complexity: O(n), where n is the number of elements in the linked list.
722
720
  * Space Complexity: O(n)
@@ -1,4 +1,2 @@
1
1
  export * from './matrix';
2
- export * from './vector2d';
3
- export * from './matrix2d';
4
2
  export * from './navigator';