directed-graph-typed 1.39.5 → 1.40.0

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 (96) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +6 -6
  2. package/dist/data-structures/binary-tree/avl-tree.js +13 -14
  3. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
  4. package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -27
  6. package/dist/data-structures/binary-tree/binary-tree.js +25 -46
  7. package/dist/data-structures/binary-tree/bst.d.ts +7 -7
  8. package/dist/data-structures/binary-tree/bst.js +16 -16
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -5
  10. package/dist/data-structures/binary-tree/rb-tree.js +5 -11
  11. package/dist/data-structures/binary-tree/segment-tree.d.ts +14 -30
  12. package/dist/data-structures/binary-tree/segment-tree.js +20 -68
  13. package/dist/data-structures/binary-tree/tree-multiset.d.ts +7 -7
  14. package/dist/data-structures/binary-tree/tree-multiset.js +24 -24
  15. package/dist/data-structures/graph/abstract-graph.d.ts +16 -35
  16. package/dist/data-structures/graph/abstract-graph.js +18 -57
  17. package/dist/data-structures/graph/directed-graph.d.ts +16 -22
  18. package/dist/data-structures/graph/directed-graph.js +17 -35
  19. package/dist/data-structures/graph/map-graph.d.ts +13 -19
  20. package/dist/data-structures/graph/map-graph.js +15 -33
  21. package/dist/data-structures/graph/undirected-graph.d.ts +12 -19
  22. package/dist/data-structures/graph/undirected-graph.js +15 -28
  23. package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
  24. package/dist/data-structures/hash/coordinate-map.js +0 -3
  25. package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
  26. package/dist/data-structures/hash/coordinate-set.js +0 -3
  27. package/dist/data-structures/hash/hash-map.d.ts +8 -14
  28. package/dist/data-structures/hash/hash-map.js +4 -22
  29. package/dist/data-structures/hash/hash-table.d.ts +10 -13
  30. package/dist/data-structures/hash/hash-table.js +8 -17
  31. package/dist/data-structures/heap/heap.d.ts +12 -6
  32. package/dist/data-structures/heap/heap.js +40 -22
  33. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +34 -42
  34. package/dist/data-structures/linked-list/doubly-linked-list.js +67 -91
  35. package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -32
  36. package/dist/data-structures/linked-list/singly-linked-list.js +64 -82
  37. package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
  38. package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
  39. package/dist/data-structures/matrix/matrix.d.ts +1 -1
  40. package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
  41. package/dist/data-structures/matrix/navigator.d.ts +4 -4
  42. package/dist/data-structures/queue/deque.d.ts +8 -12
  43. package/dist/data-structures/queue/deque.js +31 -43
  44. package/dist/data-structures/queue/queue.d.ts +20 -5
  45. package/dist/data-structures/queue/queue.js +35 -18
  46. package/dist/data-structures/stack/stack.d.ts +2 -1
  47. package/dist/data-structures/stack/stack.js +10 -7
  48. package/dist/data-structures/tree/tree.d.ts +3 -9
  49. package/dist/data-structures/tree/tree.js +3 -21
  50. package/dist/data-structures/trie/trie.d.ts +6 -12
  51. package/dist/data-structures/trie/trie.js +6 -24
  52. package/dist/interfaces/binary-tree.d.ts +3 -3
  53. package/dist/interfaces/graph.d.ts +2 -2
  54. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  55. package/package.json +2 -2
  56. package/src/data-structures/binary-tree/avl-tree.ts +15 -17
  57. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
  58. package/src/data-structures/binary-tree/binary-tree.ts +35 -60
  59. package/src/data-structures/binary-tree/bst.ts +21 -22
  60. package/src/data-structures/binary-tree/rb-tree.ts +19 -27
  61. package/src/data-structures/binary-tree/segment-tree.ts +25 -92
  62. package/src/data-structures/binary-tree/tree-multiset.ts +26 -27
  63. package/src/data-structures/graph/abstract-graph.ts +42 -88
  64. package/src/data-structures/graph/directed-graph.ts +29 -55
  65. package/src/data-structures/graph/map-graph.ts +20 -45
  66. package/src/data-structures/graph/undirected-graph.ts +24 -41
  67. package/src/data-structures/hash/coordinate-map.ts +0 -4
  68. package/src/data-structures/hash/coordinate-set.ts +0 -4
  69. package/src/data-structures/hash/hash-map.ts +13 -37
  70. package/src/data-structures/hash/hash-table.ts +15 -27
  71. package/src/data-structures/hash/tree-map.ts +2 -1
  72. package/src/data-structures/hash/tree-set.ts +2 -1
  73. package/src/data-structures/heap/heap.ts +58 -30
  74. package/src/data-structures/heap/max-heap.ts +1 -1
  75. package/src/data-structures/heap/min-heap.ts +1 -1
  76. package/src/data-structures/linked-list/doubly-linked-list.ts +81 -115
  77. package/src/data-structures/linked-list/singly-linked-list.ts +76 -101
  78. package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
  79. package/src/data-structures/matrix/matrix.ts +2 -2
  80. package/src/data-structures/matrix/matrix2d.ts +1 -1
  81. package/src/data-structures/matrix/navigator.ts +4 -4
  82. package/src/data-structures/matrix/vector2d.ts +2 -1
  83. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  84. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  85. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  86. package/src/data-structures/queue/deque.ts +38 -53
  87. package/src/data-structures/queue/queue.ts +38 -20
  88. package/src/data-structures/stack/stack.ts +13 -9
  89. package/src/data-structures/tree/tree.ts +7 -33
  90. package/src/data-structures/trie/trie.ts +14 -40
  91. package/src/interfaces/binary-tree.ts +3 -3
  92. package/src/interfaces/graph.ts +2 -2
  93. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  94. package/src/types/data-structures/matrix/navigator.ts +1 -1
  95. package/src/types/utils/utils.ts +1 -1
  96. package/src/types/utils/validate-type.ts +2 -2
@@ -49,8 +49,4 @@ export class CoordinateSet extends Set<any> {
49
49
  override delete(value: number[]) {
50
50
  return super.delete(value.join(this._joint));
51
51
  }
52
-
53
- protected _setJoint(v: string) {
54
- this._joint = v;
55
- }
56
52
  }
@@ -38,66 +38,42 @@ export class HashMap<K, V> {
38
38
  });
39
39
  }
40
40
 
41
- private _initialCapacity: number;
41
+ protected _initialCapacity: number;
42
42
 
43
43
  get initialCapacity(): number {
44
44
  return this._initialCapacity;
45
45
  }
46
46
 
47
- set initialCapacity(value: number) {
48
- this._initialCapacity = value;
49
- }
50
-
51
- private _loadFactor: number;
47
+ protected _loadFactor: number;
52
48
 
53
49
  get loadFactor(): number {
54
50
  return this._loadFactor;
55
51
  }
56
52
 
57
- set loadFactor(value: number) {
58
- this._loadFactor = value;
59
- }
60
-
61
- private _capacityMultiplier: number;
53
+ protected _capacityMultiplier: number;
62
54
 
63
55
  get capacityMultiplier(): number {
64
56
  return this._capacityMultiplier;
65
57
  }
66
58
 
67
- set capacityMultiplier(value: number) {
68
- this._capacityMultiplier = value;
69
- }
70
-
71
- private _size: number;
59
+ protected _size: number;
72
60
 
73
61
  get size(): number {
74
62
  return this._size;
75
63
  }
76
64
 
77
- set size(value: number) {
78
- this._size = value;
79
- }
80
-
81
- private _table: Array<Array<[K, V]>>;
65
+ protected _table: Array<Array<[K, V]>>;
82
66
 
83
67
  get table(): Array<Array<[K, V]>> {
84
68
  return this._table;
85
69
  }
86
70
 
87
- set table(value: Array<Array<[K, V]>>) {
88
- this._table = value;
89
- }
90
-
91
- private _hashFn: HashFunction<K>;
71
+ protected _hashFn: HashFunction<K>;
92
72
 
93
73
  get hashFn(): HashFunction<K> {
94
74
  return this._hashFn;
95
75
  }
96
76
 
97
- set hashFn(value: HashFunction<K>) {
98
- this._hashFn = value;
99
- }
100
-
101
77
  set(key: K, value: V): void {
102
78
  const loadFactor = this.size / this.table.length;
103
79
  if (loadFactor >= this.loadFactor) {
@@ -118,7 +94,7 @@ export class HashMap<K, V> {
118
94
  }
119
95
 
120
96
  this.table[index].push([key, value]);
121
- this.size++;
97
+ this._size++;
122
98
  }
123
99
 
124
100
  get(key: K): V | undefined {
@@ -145,7 +121,7 @@ export class HashMap<K, V> {
145
121
  for (let i = 0; i < this.table[index].length; i++) {
146
122
  if (this.table[index][i][0] === key) {
147
123
  this.table[index].splice(i, 1);
148
- this.size--;
124
+ this._size--;
149
125
 
150
126
  // Check if the table needs to be resized down
151
127
  const loadFactor = this.size / this.table.length;
@@ -157,7 +133,7 @@ export class HashMap<K, V> {
157
133
  }
158
134
  }
159
135
 
160
- *entries(): IterableIterator<[K, V]> {
136
+ * entries(): IterableIterator<[K, V]> {
161
137
  for (const bucket of this.table) {
162
138
  if (bucket) {
163
139
  for (const [key, value] of bucket) {
@@ -172,15 +148,15 @@ export class HashMap<K, V> {
172
148
  }
173
149
 
174
150
  clear(): void {
175
- this.size = 0;
176
- this.table = new Array(this.initialCapacity);
151
+ this._size = 0;
152
+ this._table = new Array(this.initialCapacity);
177
153
  }
178
154
 
179
155
  isEmpty(): boolean {
180
156
  return this.size === 0;
181
157
  }
182
158
 
183
- private _hash(key: K): number {
159
+ protected _hash(key: K): number {
184
160
  return this._hashFn(key);
185
161
  }
186
162
 
@@ -190,7 +166,7 @@ export class HashMap<K, V> {
190
166
  * @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
191
167
  * the number of buckets that the new table should have.
192
168
  */
193
- private resizeTable(newCapacity: number): void {
169
+ protected resizeTable(newCapacity: number): void {
194
170
  const newTable = new Array(newCapacity);
195
171
  for (const bucket of this._table) {
196
172
  // Note that this is this._table
@@ -8,12 +8,12 @@
8
8
 
9
9
  export class HashTableNode<K, V> {
10
10
  key: K;
11
- val: V;
11
+ value: V;
12
12
  next: HashTableNode<K, V> | null;
13
13
 
14
- constructor(key: K, val: V) {
14
+ constructor(key: K, value: V) {
15
15
  this.key = key;
16
- this.val = val;
16
+ this.value = value;
17
17
  this.next = null;
18
18
  }
19
19
  }
@@ -21,8 +21,8 @@ export class HashTableNode<K, V> {
21
21
  import {HashFunction} from '../../types';
22
22
 
23
23
  export class HashTable<K, V> {
24
- private static readonly DEFAULT_CAPACITY = 16;
25
- private static readonly LOAD_FACTOR = 0.75;
24
+ protected static readonly DEFAULT_CAPACITY = 16;
25
+ protected static readonly LOAD_FACTOR = 0.75;
26
26
 
27
27
  constructor(capacity: number = HashTable.DEFAULT_CAPACITY, hashFn?: HashFunction<K>) {
28
28
  this._hashFn = hashFn || this._defaultHashFn;
@@ -31,54 +31,42 @@ export class HashTable<K, V> {
31
31
  this._buckets = new Array<HashTableNode<K, V> | null>(this._capacity).fill(null);
32
32
  }
33
33
 
34
- private _capacity: number;
34
+ protected _capacity: number;
35
35
 
36
36
  get capacity(): number {
37
37
  return this._capacity;
38
38
  }
39
39
 
40
- set capacity(value: number) {
41
- this._capacity = value;
42
- }
43
-
44
- private _size: number;
40
+ protected _size: number;
45
41
 
46
42
  get size(): number {
47
43
  return this._size;
48
44
  }
49
45
 
50
- private _buckets: Array<HashTableNode<K, V> | null>;
46
+ protected _buckets: Array<HashTableNode<K, V> | null>;
51
47
 
52
48
  get buckets(): Array<HashTableNode<K, V> | null> {
53
49
  return this._buckets;
54
50
  }
55
51
 
56
- set buckets(value: Array<HashTableNode<K, V> | null>) {
57
- this._buckets = value;
58
- }
59
-
60
- private _hashFn: HashFunction<K>;
52
+ protected _hashFn: HashFunction<K>;
61
53
 
62
54
  get hashFn(): HashFunction<K> {
63
55
  return this._hashFn;
64
56
  }
65
57
 
66
- set hashFn(value: HashFunction<K>) {
67
- this._hashFn = value;
68
- }
69
-
70
58
  /**
71
59
  * The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
72
60
  * @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
73
61
  * table. It is of type K, which is a generic type representing the key's data type.
74
- * @param {V} val - The parameter `val` represents the value that you want to associate with the given key in the hash
62
+ * @param {V} value - The parameter `value` represents the value that you want to associate with the given key in the hash
75
63
  * table.
76
64
  * @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
77
65
  * value.
78
66
  */
79
- set(key: K, val: V): void {
67
+ set(key: K, value: V): void {
80
68
  const index = this._hash(key);
81
- const newNode = new HashTableNode<K, V>(key, val);
69
+ const newNode = new HashTableNode<K, V>(key, value);
82
70
 
83
71
  if (!this._buckets[index]) {
84
72
  this._buckets[index] = newNode;
@@ -88,7 +76,7 @@ export class HashTable<K, V> {
88
76
  while (currentNode) {
89
77
  if (currentNode.key === key) {
90
78
  // If the key already exists, update the value
91
- currentNode.val = val;
79
+ currentNode.value = value;
92
80
  return;
93
81
  }
94
82
  if (!currentNode.next) {
@@ -120,7 +108,7 @@ export class HashTable<K, V> {
120
108
 
121
109
  while (currentNode) {
122
110
  if (currentNode.key === key) {
123
- return currentNode.val;
111
+ return currentNode.value;
124
112
  }
125
113
  currentNode = currentNode.next;
126
114
  }
@@ -259,7 +247,7 @@ export class HashTable<K, V> {
259
247
  let currentNode = bucket;
260
248
  while (currentNode) {
261
249
  const newIndex = this._hash(currentNode.key);
262
- const newNode = new HashTableNode<K, V>(currentNode.key, currentNode.val);
250
+ const newNode = new HashTableNode<K, V>(currentNode.key, currentNode.value);
263
251
 
264
252
  if (!newBuckets[newIndex]) {
265
253
  newBuckets[newIndex] = newNode;
@@ -1 +1,2 @@
1
- export class TreeMap {}
1
+ export class TreeMap {
2
+ }
@@ -1 +1,2 @@
1
- export class TreeSet {}
1
+ export class TreeSet {
2
+ }
@@ -8,17 +8,26 @@
8
8
  import type {Comparator, DFSOrderPattern} from '../../types';
9
9
 
10
10
  export class Heap<E = any> {
11
- protected nodes: E[] = [];
12
- protected readonly comparator: Comparator<E>;
13
-
14
- constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
15
- this.comparator = options.comparator;
11
+ constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
12
+ this._comparator = options.comparator;
16
13
  if (options.nodes && options.nodes.length > 0) {
17
- this.nodes = options.nodes;
14
+ this._nodes = options.nodes;
18
15
  this.fix();
19
16
  }
20
17
  }
21
18
 
19
+ protected _nodes: E[] = [];
20
+
21
+ get nodes(): E[] {
22
+ return this._nodes;
23
+ }
24
+
25
+ protected _comparator: Comparator<E>;
26
+
27
+ get comparator(): Comparator<E> {
28
+ return this._comparator;
29
+ }
30
+
22
31
  /**
23
32
  * Get the size (number of elements) of the heap.
24
33
  */
@@ -39,7 +48,7 @@ export class Heap<E = any> {
39
48
  * @returns A new Heap instance.
40
49
  * @param options
41
50
  */
42
- static heapify<E>(options: {nodes: E[]; comparator: Comparator<E>}): Heap<E> {
51
+ static heapify<E>(options: { nodes: E[]; comparator: Comparator<E> }): Heap<E> {
43
52
  return new Heap<E>(options);
44
53
  }
45
54
 
@@ -110,7 +119,7 @@ export class Heap<E = any> {
110
119
  * Reset the nodes of the heap. Make the nodes empty.
111
120
  */
112
121
  clear() {
113
- this.nodes = [];
122
+ this._nodes = [];
114
123
  }
115
124
 
116
125
  /**
@@ -118,7 +127,7 @@ export class Heap<E = any> {
118
127
  * @param nodes
119
128
  */
120
129
  refill(nodes: E[]) {
121
- this.nodes = nodes;
130
+ this._nodes = nodes;
122
131
  this.fix();
123
132
  }
124
133
 
@@ -181,7 +190,7 @@ export class Heap<E = any> {
181
190
  */
182
191
  clone(): Heap<E> {
183
192
  const clonedHeap = new Heap<E>({comparator: this.comparator});
184
- clonedHeap.nodes = [...this.nodes];
193
+ clonedHeap._nodes = [...this.nodes];
185
194
  return clonedHeap;
186
195
  }
187
196
 
@@ -268,28 +277,47 @@ export class FibonacciHeapNode<E> {
268
277
  }
269
278
 
270
279
  export class FibonacciHeap<E> {
271
- root?: FibonacciHeapNode<E>;
272
- size = 0;
273
- protected min?: FibonacciHeapNode<E>;
274
- protected readonly comparator: Comparator<E>;
275
-
276
280
  constructor(comparator?: Comparator<E>) {
277
281
  this.clear();
278
- this.comparator = comparator || this.defaultComparator;
282
+ this._comparator = comparator || this.defaultComparator;
279
283
 
280
284
  if (typeof this.comparator !== 'function') {
281
285
  throw new Error('FibonacciHeap constructor: given comparator should be a function.');
282
286
  }
283
287
  }
284
288
 
289
+ protected _root?: FibonacciHeapNode<E>;
290
+
291
+ get root(): FibonacciHeapNode<E> | undefined {
292
+ return this._root;
293
+ }
294
+
295
+ protected _size = 0;
296
+
297
+ get size(): number {
298
+ return this._size;
299
+ }
300
+
301
+ protected _min?: FibonacciHeapNode<E>;
302
+
303
+ get min(): FibonacciHeapNode<E> | undefined {
304
+ return this._min;
305
+ }
306
+
307
+ protected _comparator: Comparator<E>;
308
+
309
+ get comparator(): Comparator<E> {
310
+ return this._comparator;
311
+ }
312
+
285
313
  /**
286
314
  * Get the size (number of elements) of the heap.
287
315
  * @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
288
316
  */
289
317
  clear(): void {
290
- this.root = undefined;
291
- this.min = undefined;
292
- this.size = 0;
318
+ this._root = undefined;
319
+ this._min = undefined;
320
+ this._size = 0;
293
321
  }
294
322
 
295
323
  /**
@@ -315,10 +343,10 @@ export class FibonacciHeap<E> {
315
343
  this.mergeWithRoot(node);
316
344
 
317
345
  if (!this.min || this.comparator(node.element, this.min.element) <= 0) {
318
- this.min = node;
346
+ this._min = node;
319
347
  }
320
348
 
321
- this.size++;
349
+ this._size++;
322
350
  return this;
323
351
  }
324
352
 
@@ -405,14 +433,14 @@ export class FibonacciHeap<E> {
405
433
  this.removeFromRoot(z);
406
434
 
407
435
  if (z === z.right) {
408
- this.min = undefined;
409
- this.root = undefined;
436
+ this._min = undefined;
437
+ this._root = undefined;
410
438
  } else {
411
- this.min = z.right;
439
+ this._min = z.right;
412
440
  this.consolidate();
413
441
  }
414
442
 
415
- this.size--;
443
+ this._size--;
416
444
 
417
445
  return z.element;
418
446
  }
@@ -444,11 +472,11 @@ export class FibonacciHeap<E> {
444
472
 
445
473
  // Update the minimum node
446
474
  if (!this.min || (heapToMerge.min && this.comparator(heapToMerge.min.element, this.min.element) < 0)) {
447
- this.min = heapToMerge.min;
475
+ this._min = heapToMerge.min;
448
476
  }
449
477
 
450
478
  // Update the size
451
- this.size += heapToMerge.size;
479
+ this._size += heapToMerge.size;
452
480
 
453
481
  // Clear the heap that was merged
454
482
  heapToMerge.clear();
@@ -481,7 +509,7 @@ export class FibonacciHeap<E> {
481
509
  */
482
510
  protected mergeWithRoot(node: FibonacciHeapNode<E>): void {
483
511
  if (!this.root) {
484
- this.root = node;
512
+ this._root = node;
485
513
  } else {
486
514
  node.right = this.root.right;
487
515
  node.left = this.root;
@@ -497,7 +525,7 @@ export class FibonacciHeap<E> {
497
525
  * @protected
498
526
  */
499
527
  protected removeFromRoot(node: FibonacciHeapNode<E>): void {
500
- if (this.root === node) this.root = node.right;
528
+ if (this.root === node) this._root = node.right;
501
529
  if (node.left) node.left.right = node.right;
502
530
  if (node.right) node.right.left = node.left;
503
531
  }
@@ -554,7 +582,7 @@ export class FibonacciHeap<E> {
554
582
 
555
583
  for (let i = 0; i < this.size; i++) {
556
584
  if (A[i] && this.comparator(A[i]!.element, this.min!.element) <= 0) {
557
- this.min = A[i]!;
585
+ this._min = A[i]!;
558
586
  }
559
587
  }
560
588
  }
@@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
11
11
 
12
12
  export class MaxHeap<E = any> extends Heap<E> {
13
13
  constructor(
14
- options: {comparator: Comparator<E>; nodes?: E[]} = {
14
+ options: { comparator: Comparator<E>; nodes?: E[] } = {
15
15
  comparator: (a: E, b: E) => {
16
16
  if (!(typeof a === 'number' && typeof b === 'number')) {
17
17
  throw new Error('The a, b params of compare function must be number');
@@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
11
11
 
12
12
  export class MinHeap<E = any> extends Heap<E> {
13
13
  constructor(
14
- options: {comparator: Comparator<E>; nodes?: E[]} = {
14
+ options: { comparator: Comparator<E>; nodes?: E[] } = {
15
15
  comparator: (a: E, b: E) => {
16
16
  if (!(typeof a === 'number' && typeof b === 'number')) {
17
17
  throw new Error('The a, b params of compare function must be number');