data-structure-typed 1.38.9 → 1.39.1

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 (153) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +9 -9
  3. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +2 -2
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  6. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +17 -0
  7. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +86 -32
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js +8 -8
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/bst.d.ts +6 -6
  12. package/dist/cjs/data-structures/binary-tree/bst.js +2 -2
  13. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +2 -2
  16. package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
  17. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +4 -4
  18. package/dist/cjs/data-structures/graph/abstract-graph.js +5 -5
  19. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  20. package/dist/cjs/data-structures/graph/directed-graph.d.ts +4 -4
  21. package/dist/cjs/data-structures/graph/directed-graph.js +6 -6
  22. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  23. package/dist/cjs/data-structures/graph/map-graph.d.ts +1 -1
  24. package/dist/cjs/data-structures/graph/map-graph.js +1 -1
  25. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  26. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +3 -3
  27. package/dist/cjs/data-structures/graph/undirected-graph.js +4 -4
  28. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  29. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  30. package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
  31. package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
  32. package/dist/cjs/data-structures/heap/heap.d.ts +10 -5
  33. package/dist/cjs/data-structures/heap/heap.js +10 -10
  34. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  35. package/dist/cjs/data-structures/heap/max-heap.d.ts +4 -1
  36. package/dist/cjs/data-structures/heap/max-heap.js +9 -7
  37. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  38. package/dist/cjs/data-structures/heap/min-heap.d.ts +4 -1
  39. package/dist/cjs/data-structures/heap/min-heap.js +9 -7
  40. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  41. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  42. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  43. package/dist/cjs/data-structures/matrix/matrix2d.d.ts +1 -2
  44. package/dist/cjs/data-structures/matrix/matrix2d.js +3 -7
  45. package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
  46. package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -1
  47. package/dist/cjs/data-structures/matrix/vector2d.js +0 -1
  48. package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
  49. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
  50. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +9 -7
  51. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  52. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
  53. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +9 -7
  54. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  55. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +5 -2
  56. package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
  57. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  58. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  59. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  60. package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
  61. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
  62. package/dist/cjs/types/helpers.d.ts +1 -4
  63. package/dist/cjs/types/helpers.js.map +1 -1
  64. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +2 -2
  65. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  66. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +17 -0
  67. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +86 -32
  68. package/dist/mjs/data-structures/binary-tree/binary-tree.js +8 -8
  69. package/dist/mjs/data-structures/binary-tree/bst.d.ts +6 -6
  70. package/dist/mjs/data-structures/binary-tree/bst.js +2 -2
  71. package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +2 -2
  72. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +4 -4
  73. package/dist/mjs/data-structures/graph/abstract-graph.js +5 -5
  74. package/dist/mjs/data-structures/graph/directed-graph.d.ts +4 -4
  75. package/dist/mjs/data-structures/graph/directed-graph.js +6 -6
  76. package/dist/mjs/data-structures/graph/map-graph.d.ts +1 -1
  77. package/dist/mjs/data-structures/graph/map-graph.js +1 -1
  78. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +3 -3
  79. package/dist/mjs/data-structures/graph/undirected-graph.js +4 -4
  80. package/dist/mjs/data-structures/heap/heap.d.ts +10 -5
  81. package/dist/mjs/data-structures/heap/heap.js +10 -10
  82. package/dist/mjs/data-structures/heap/max-heap.d.ts +4 -1
  83. package/dist/mjs/data-structures/heap/max-heap.js +9 -7
  84. package/dist/mjs/data-structures/heap/min-heap.d.ts +4 -1
  85. package/dist/mjs/data-structures/heap/min-heap.js +9 -7
  86. package/dist/mjs/data-structures/matrix/matrix2d.d.ts +1 -2
  87. package/dist/mjs/data-structures/matrix/matrix2d.js +3 -7
  88. package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -1
  89. package/dist/mjs/data-structures/matrix/vector2d.js +0 -1
  90. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
  91. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +9 -7
  92. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
  93. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +9 -7
  94. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +5 -2
  95. package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
  96. package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
  97. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
  98. package/dist/mjs/types/helpers.d.ts +1 -4
  99. package/dist/umd/data-structure-typed.min.js +1 -1
  100. package/dist/umd/data-structure-typed.min.js.map +1 -1
  101. package/package.json +5 -5
  102. package/src/data-structures/binary-tree/avl-tree.ts +5 -4
  103. package/src/data-structures/binary-tree/binary-indexed-tree.ts +21 -1
  104. package/src/data-structures/binary-tree/binary-tree.ts +37 -93
  105. package/src/data-structures/binary-tree/bst.ts +11 -17
  106. package/src/data-structures/binary-tree/rb-tree.ts +2 -1
  107. package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
  108. package/src/data-structures/graph/abstract-graph.ts +16 -15
  109. package/src/data-structures/graph/directed-graph.ts +8 -7
  110. package/src/data-structures/graph/map-graph.ts +2 -2
  111. package/src/data-structures/graph/undirected-graph.ts +9 -8
  112. package/src/data-structures/hash/hash-map.ts +1 -1
  113. package/src/data-structures/hash/tree-map.ts +1 -2
  114. package/src/data-structures/hash/tree-set.ts +1 -2
  115. package/src/data-structures/heap/heap.ts +12 -12
  116. package/src/data-structures/heap/max-heap.ts +8 -6
  117. package/src/data-structures/heap/min-heap.ts +8 -6
  118. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  119. package/src/data-structures/matrix/matrix.ts +1 -1
  120. package/src/data-structures/matrix/matrix2d.ts +1 -3
  121. package/src/data-structures/matrix/vector2d.ts +1 -4
  122. package/src/data-structures/priority-queue/max-priority-queue.ts +8 -6
  123. package/src/data-structures/priority-queue/min-priority-queue.ts +8 -6
  124. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  125. package/src/data-structures/queue/deque.ts +4 -5
  126. package/src/data-structures/queue/queue.ts +1 -1
  127. package/src/interfaces/binary-tree.ts +2 -2
  128. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
  129. package/src/types/data-structures/matrix/navigator.ts +1 -1
  130. package/src/types/helpers.ts +1 -7
  131. package/src/types/utils/utils.ts +1 -1
  132. package/src/types/utils/validate-type.ts +2 -2
  133. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +141 -1
  134. package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +35 -0
  135. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +269 -47
  136. package/test/unit/data-structures/binary-tree/bst.test.ts +391 -1
  137. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +73 -7
  138. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +241 -186
  139. package/test/unit/data-structures/graph/directed-graph.test.ts +37 -7
  140. package/test/unit/data-structures/graph/map-graph.test.ts +82 -1
  141. package/test/unit/data-structures/graph/overall.test.ts +2 -2
  142. package/test/unit/data-structures/graph/undirected-graph.test.ts +84 -2
  143. package/test/unit/data-structures/heap/heap.test.ts +2 -2
  144. package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
  145. package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
  146. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +1 -1
  147. package/test/unit/data-structures/matrix/matrix2d.test.ts +207 -0
  148. package/test/unit/data-structures/matrix/navigator.test.ts +166 -1
  149. package/test/unit/data-structures/matrix/vector2d.test.ts +171 -0
  150. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +3 -3
  151. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +10 -10
  152. package/test/unit/data-structures/queue/deque.test.ts +264 -1
  153. package/test/unit/data-structures/queue/queue.test.ts +3 -1
@@ -64,7 +64,8 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
64
64
 
65
65
  export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge>
66
66
  extends AbstractGraph<V, E>
67
- implements IGraph<V, E> {
67
+ implements IGraph<V, E>
68
+ {
68
69
  /**
69
70
  * The constructor function initializes an instance of a class.
70
71
  */
@@ -153,7 +154,7 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
153
154
  * @param {V | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
154
155
  * @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
155
156
  */
156
- removeEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null {
157
+ deleteEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null {
157
158
  const src: V | null = this._getVertex(srcOrKey);
158
159
  const dest: V | null = this._getVertex(destOrKey);
159
160
  let removed: E | null = null;
@@ -177,9 +178,9 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
177
178
  * The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
178
179
  * @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
179
180
  * and `dest`, which represent the source and destination vertices of the edge, respectively.
180
- * @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
181
+ * @returns The method `deleteEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
181
182
  */
182
- removeEdge(edge: E): E | null {
183
+ deleteEdge(edge: E): E | null {
183
184
  let removed: E | null = null;
184
185
  const src = this._getVertex(edge.src);
185
186
  const dest = this._getVertex(edge.dest);
@@ -206,12 +207,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
206
207
  * the second vertex in the edge that needs to be removed.
207
208
  * @returns an array of removed edges (E[]).
208
209
  */
209
- removeEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[] {
210
+ deleteEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[] {
210
211
  const removed: E[] = [];
211
212
 
212
213
  if (v1 && v2) {
213
- const v1ToV2 = this.removeEdgeSrcToDest(v1, v2);
214
- const v2ToV1 = this.removeEdgeSrcToDest(v2, v1);
214
+ const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
215
+ const v2ToV1 = this.deleteEdgeSrcToDest(v2, v1);
215
216
 
216
217
  v1ToV2 && removed.push(v1ToV2);
217
218
  v2ToV1 && removed.push(v2ToV1);
@@ -109,9 +109,9 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
109
109
  */
110
110
  override createVertex(
111
111
  key: VertexKey,
112
- val?: V['val'],
113
112
  lat: number = this.origin[0],
114
- long: number = this.origin[1]
113
+ long: number = this.origin[1],
114
+ val?: V['val']
115
115
  ): V {
116
116
  return new MapVertex(key, lat, long, val) as V;
117
117
  }
@@ -51,11 +51,12 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
51
51
  }
52
52
 
53
53
  export class UndirectedGraph<
54
- V extends UndirectedVertex<any> = UndirectedVertex,
55
- E extends UndirectedEdge<any> = UndirectedEdge
56
- >
54
+ V extends UndirectedVertex<any> = UndirectedVertex,
55
+ E extends UndirectedEdge<any> = UndirectedEdge
56
+ >
57
57
  extends AbstractGraph<V, E>
58
- implements IGraph<V, E> {
58
+ implements IGraph<V, E>
59
+ {
59
60
  /**
60
61
  * The constructor initializes a new Map object to store edges.
61
62
  */
@@ -127,7 +128,7 @@ export class UndirectedGraph<
127
128
  * (VertexKey). It represents the second vertex of the edge that needs to be removed.
128
129
  * @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
129
130
  */
130
- removeEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null {
131
+ deleteEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null {
131
132
  const vertex1: V | null = this._getVertex(v1);
132
133
  const vertex2: V | null = this._getVertex(v2);
133
134
 
@@ -148,12 +149,12 @@ export class UndirectedGraph<
148
149
  }
149
150
 
150
151
  /**
151
- * The removeEdge function removes an edge between two vertices in a graph.
152
+ * The deleteEdge function removes an edge between two vertices in a graph.
152
153
  * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
153
154
  * @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
154
155
  */
155
- removeEdge(edge: E): E | null {
156
- return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
156
+ deleteEdge(edge: E): E | null {
157
+ return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
157
158
  }
158
159
 
159
160
  /**
@@ -157,7 +157,7 @@ export class HashMap<K, V> {
157
157
  }
158
158
  }
159
159
 
160
- * entries(): IterableIterator<[K, V]> {
160
+ *entries(): IterableIterator<[K, V]> {
161
161
  for (const bucket of this.table) {
162
162
  if (bucket) {
163
163
  for (const [key, value] of bucket) {
@@ -1,2 +1 @@
1
- export class TreeMap {
2
- }
1
+ export class TreeMap {}
@@ -1,2 +1 @@
1
- export class TreeSet {
2
- }
1
+ export class TreeSet {}
@@ -7,12 +7,16 @@
7
7
 
8
8
  import type {Comparator, DFSOrderPattern} from '../../types';
9
9
 
10
- export class Heap<E> {
10
+ export class Heap<E = any> {
11
11
  protected nodes: E[] = [];
12
12
  protected readonly comparator: Comparator<E>;
13
13
 
14
- constructor(comparator: Comparator<E>) {
15
- this.comparator = comparator;
14
+ constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
15
+ this.comparator = options.comparator;
16
+ if (options.nodes && options.nodes.length > 0) {
17
+ this.nodes = options.nodes;
18
+ this.fix();
19
+ }
16
20
  }
17
21
 
18
22
  /**
@@ -32,15 +36,11 @@ export class Heap<E> {
32
36
 
33
37
  /**
34
38
  * Static method that creates a binary heap from an array of nodes and a comparison function.
35
- * @param nodes
36
- * @param comparator - Comparison function.
37
39
  * @returns A new Heap instance.
40
+ * @param options
38
41
  */
39
- static heapify<E>(nodes: E[], comparator: Comparator<E>): Heap<E> {
40
- const binaryHeap = new Heap<E>(comparator);
41
- binaryHeap.nodes = [...nodes];
42
- binaryHeap.fix(); // Fix heap properties
43
- return binaryHeap;
42
+ static heapify<E>(options: {nodes: E[]; comparator: Comparator<E>}): Heap<E> {
43
+ return new Heap<E>(options);
44
44
  }
45
45
 
46
46
  /**
@@ -180,7 +180,7 @@ export class Heap<E> {
180
180
  * @returns A new Heap instance containing the same elements.
181
181
  */
182
182
  clone(): Heap<E> {
183
- const clonedHeap = new Heap<E>(this.comparator);
183
+ const clonedHeap = new Heap<E>({comparator: this.comparator});
184
184
  clonedHeap.nodes = [...this.nodes];
185
185
  return clonedHeap;
186
186
  }
@@ -269,7 +269,7 @@ export class FibonacciHeapNode<E> {
269
269
 
270
270
  export class FibonacciHeap<E> {
271
271
  root?: FibonacciHeapNode<E>;
272
- size: number = 0;
272
+ size = 0;
273
273
  protected min?: FibonacciHeapNode<E>;
274
274
  protected readonly comparator: Comparator<E>;
275
275
 
@@ -11,14 +11,16 @@ import type {Comparator} from '../../types';
11
11
 
12
12
  export class MaxHeap<E = any> extends Heap<E> {
13
13
  constructor(
14
- comparator: Comparator<E> = (a: E, b: E) => {
15
- if (!(typeof a === 'number' && typeof b === 'number')) {
16
- throw new Error('The a, b params of compare function must be number');
17
- } else {
18
- return b - a;
14
+ options: {comparator: Comparator<E>; nodes?: E[]} = {
15
+ comparator: (a: E, b: E) => {
16
+ if (!(typeof a === 'number' && typeof b === 'number')) {
17
+ throw new Error('The a, b params of compare function must be number');
18
+ } else {
19
+ return b - a;
20
+ }
19
21
  }
20
22
  }
21
23
  ) {
22
- super(comparator);
24
+ super(options);
23
25
  }
24
26
  }
@@ -11,14 +11,16 @@ import type {Comparator} from '../../types';
11
11
 
12
12
  export class MinHeap<E = any> extends Heap<E> {
13
13
  constructor(
14
- comparator: Comparator<E> = (a: E, b: E) => {
15
- if (!(typeof a === 'number' && typeof b === 'number')) {
16
- throw new Error('The a, b params of compare function must be number');
17
- } else {
18
- return a - b;
14
+ options: {comparator: Comparator<E>; nodes?: E[]} = {
15
+ comparator: (a: E, b: E) => {
16
+ if (!(typeof a === 'number' && typeof b === 'number')) {
17
+ throw new Error('The a, b params of compare function must be number');
18
+ } else {
19
+ return a - b;
20
+ }
19
21
  }
20
22
  }
21
23
  ) {
22
- super(comparator);
24
+ super(options);
23
25
  }
24
26
  }
@@ -485,7 +485,7 @@ export class SinglyLinkedList<E = any> {
485
485
  return count;
486
486
  }
487
487
 
488
- * [Symbol.iterator]() {
488
+ *[Symbol.iterator]() {
489
489
  let current = this.head;
490
490
 
491
491
  while (current) {
@@ -14,7 +14,7 @@ export class MatrixNTI2D<V = any> {
14
14
  * given initial value or 0 if not provided.
15
15
  * @param options - An object containing the following properties:
16
16
  */
17
- constructor(options: { row: number; col: number; initialVal?: V }) {
17
+ constructor(options: {row: number; col: number; initialVal?: V}) {
18
18
  const {row, col, initialVal} = options;
19
19
  this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
20
20
  }
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import Vector2D from './vector2d';
8
+ import {Vector2D} from './vector2d';
9
9
 
10
10
  export class Matrix2D {
11
11
  private readonly _matrix: number[][];
@@ -209,5 +209,3 @@ export class Matrix2D {
209
209
  return new Vector2D(this._matrix[0][0], this._matrix[1][0]);
210
210
  }
211
211
  }
212
-
213
- export default Matrix2D;
@@ -10,8 +10,7 @@ export class Vector2D {
10
10
  public x: number = 0,
11
11
  public y: number = 0,
12
12
  public w: number = 1 // needed for matrix multiplication
13
- ) {
14
- }
13
+ ) {}
15
14
 
16
15
  /**
17
16
  * The function checks if the x and y values of a point are both zero.
@@ -313,5 +312,3 @@ export class Vector2D {
313
312
  this.y = 0;
314
313
  }
315
314
  }
316
-
317
- export default Vector2D;
@@ -10,14 +10,16 @@ import type {Comparator} from '../../types';
10
10
 
11
11
  export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
12
12
  constructor(
13
- compare: Comparator<E> = (a: E, b: E) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- } else {
17
- return b - a;
13
+ options: {comparator: Comparator<E>; nodes?: E[]} = {
14
+ comparator: (a: E, b: E) => {
15
+ if (!(typeof a === 'number' && typeof b === 'number')) {
16
+ throw new Error('The a, b params of compare function must be number');
17
+ } else {
18
+ return b - a;
19
+ }
18
20
  }
19
21
  }
20
22
  ) {
21
- super(compare);
23
+ super(options);
22
24
  }
23
25
  }
@@ -10,14 +10,16 @@ import type {Comparator} from '../../types';
10
10
 
11
11
  export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
12
12
  constructor(
13
- compare: Comparator<E> = (a: E, b: E) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- } else {
17
- return a - b;
13
+ options: {comparator: Comparator<E>; nodes?: E[]} = {
14
+ comparator: (a: E, b: E) => {
15
+ if (!(typeof a === 'number' && typeof b === 'number')) {
16
+ throw new Error('The a, b params of compare function must be number');
17
+ } else {
18
+ return a - b;
19
+ }
18
20
  }
19
21
  }
20
22
  ) {
21
- super(compare);
23
+ super(options);
22
24
  }
23
25
  }
@@ -9,8 +9,8 @@
9
9
  import {Heap} from '../heap';
10
10
  import {Comparator} from '../../types';
11
11
 
12
- export class PriorityQueue<E> extends Heap<E> {
13
- constructor(comparator: Comparator<E>) {
14
- super(comparator);
12
+ export class PriorityQueue<E = any> extends Heap<E> {
13
+ constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
14
+ super(options);
15
15
  }
16
16
  }
@@ -9,8 +9,7 @@ import {DoublyLinkedList} from '../linked-list';
9
9
 
10
10
  // O(n) time complexity of obtaining the value
11
11
  // O(1) time complexity of adding at the beginning and the end
12
- export class Deque<E = any> extends DoublyLinkedList<E> {
13
- }
12
+ export class Deque<E = any> extends DoublyLinkedList<E> {}
14
13
 
15
14
  // O(1) time complexity of obtaining the value
16
15
  // O(n) time complexity of adding at the beginning and the end
@@ -20,9 +19,9 @@ export class ObjectDeque<E = number> {
20
19
  if (capacity !== undefined) this._capacity = capacity;
21
20
  }
22
21
 
23
- private _nodes: { [key: number]: E } = {};
22
+ private _nodes: {[key: number]: E} = {};
24
23
 
25
- get nodes(): { [p: number]: E } {
24
+ get nodes(): {[p: number]: E} {
26
25
  return this._nodes;
27
26
  }
28
27
 
@@ -157,7 +156,7 @@ export class ObjectDeque<E = number> {
157
156
  return this._size <= 0;
158
157
  }
159
158
 
160
- protected _seNodes(value: { [p: number]: E }) {
159
+ protected _seNodes(value: {[p: number]: E}) {
161
160
  this._nodes = value;
162
161
  }
163
162
 
@@ -183,7 +183,7 @@ export class Queue<E = any> {
183
183
  return new Queue(this.nodes.slice(this.offset));
184
184
  }
185
185
 
186
- * [Symbol.iterator]() {
186
+ *[Symbol.iterator]() {
187
187
  for (const item of this.nodes) {
188
188
  yield item;
189
189
  }
@@ -1,10 +1,10 @@
1
1
  import {BinaryTreeNode} from '../data-structures';
2
- import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, MapCallback} from '../types';
2
+ import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, OneParamCallback} from '../types';
3
3
 
4
4
  export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
5
5
  createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
6
6
 
7
7
  add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
8
8
 
9
- delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
9
+ delete<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
10
10
  }
@@ -24,10 +24,6 @@ export enum FamilyPosition {
24
24
 
25
25
  export type BinaryTreeNodeKey = number;
26
26
 
27
- export type BFSCallback<N, D = any> = (node: N, level?: number) => D;
28
-
29
- export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
30
-
31
27
  export type BinaryTreeDeletedResult<N> = { deleted: N | null | undefined; needBalanced: N | null };
32
28
 
33
29
  export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
@@ -1,6 +1,6 @@
1
1
  export type Direction = 'up' | 'right' | 'down' | 'left';
2
2
 
3
- export type Turning = { [key in Direction]: Direction };
3
+ export type Turning = {[key in Direction]: Direction};
4
4
 
5
5
  export type NavigatorParams<T = any> = {
6
6
  matrix: T[][];
@@ -1,14 +1,8 @@
1
- import {BinaryTreeNodeKey} from './data-structures';
2
-
3
1
  export type Comparator<T> = (a: T, b: T) => number;
4
2
 
5
3
  export type DFSOrderPattern = 'pre' | 'in' | 'post';
6
4
 
7
- export type MapCallback<N, D = any> = (node: N) => D;
8
-
9
- export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
10
-
11
- export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
5
+ export type OneParamCallback<N, D = any> = (node: N) => D;
12
6
 
13
7
  export enum CP {
14
8
  lt = 'lt',
@@ -1,5 +1,5 @@
1
1
  export type ToThunkFn = () => ReturnType<TrlFn>;
2
- export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: symbol };
2
+ export type Thunk = () => ReturnType<ToThunkFn> & {__THUNK__: symbol};
3
3
  export type TrlFn = (...args: any[]) => any;
4
4
  export type TrlAsyncFn = (...args: any[]) => any;
5
5
 
@@ -1,6 +1,6 @@
1
- export type KeyValueObject = { [key: string]: any };
1
+ export type KeyValueObject = {[key: string]: any};
2
2
 
3
- export type KeyValueObjectWithKey = { [key: string]: any; key: string | number | symbol };
3
+ export type KeyValueObjectWithKey = {[key: string]: any; key: string | number | symbol};
4
4
 
5
5
  export type NonNumberNonObjectButDefined = string | boolean | symbol | null;
6
6
 
@@ -1,4 +1,4 @@
1
- import {AVLTree, AVLTreeNode, CP} from '../../../../src';
1
+ import {AVLTree, AVLTreeNode, CP, IterationType} from '../../../../src';
2
2
 
3
3
  describe('AVL Tree Test', () => {
4
4
  it('should perform various operations on a AVL Tree', () => {
@@ -109,6 +109,115 @@ describe('AVL Tree Test', () => {
109
109
  });
110
110
  });
111
111
 
112
+ describe('AVL Tree Test recursively', () => {
113
+ it('should perform various operations on a AVL Tree', () => {
114
+ const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
115
+ const tree = new AVLTree<number>({iterationType: IterationType.RECURSIVE});
116
+
117
+ for (const i of arr) tree.add(i, i);
118
+
119
+ const node6 = tree.get(6);
120
+
121
+ expect(node6 && tree.getHeight(node6)).toBe(3);
122
+ expect(node6 && tree.getDepth(node6)).toBe(1);
123
+
124
+ const getNodeById = tree.get(10);
125
+ expect(getNodeById?.key).toBe(10);
126
+
127
+ const getMinNodeByRoot = tree.getLeftMost();
128
+ expect(getMinNodeByRoot?.key).toBe(1);
129
+
130
+ const node15 = tree.get(15);
131
+ const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15);
132
+ expect(getMinNodeBySpecificNode?.key).toBe(12);
133
+
134
+ let subTreeSum = 0;
135
+ node15 && tree.subTreeTraverse(node => (subTreeSum += node.key), node15);
136
+ expect(subTreeSum).toBe(70);
137
+
138
+ let lesserSum = 0;
139
+ tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), CP.lt, 10);
140
+ expect(lesserSum).toBe(45);
141
+
142
+ // node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
143
+ expect(node15?.val).toBe(15);
144
+
145
+ const dfs = tree.dfs(node => node, 'in');
146
+ expect(dfs[0].key).toBe(1);
147
+ expect(dfs[dfs.length - 1].key).toBe(16);
148
+
149
+ tree.perfectlyBalance();
150
+ const bfs = tree.bfs(node => node);
151
+ expect(tree.isPerfectlyBalanced()).toBe(true);
152
+ expect(bfs[0].key).toBe(8);
153
+ expect(bfs[bfs.length - 1].key).toBe(16);
154
+
155
+ expect(tree.delete(11)[0].deleted?.key).toBe(11);
156
+ expect(tree.isAVLBalanced()).toBe(true);
157
+ expect(node15 && tree.getHeight(node15)).toBe(2);
158
+
159
+ expect(tree.delete(1)[0].deleted?.key).toBe(1);
160
+ expect(tree.isAVLBalanced()).toBe(true);
161
+ expect(tree.getHeight()).toBe(4);
162
+
163
+ expect(tree.delete(4)[0].deleted?.key).toBe(4);
164
+ expect(tree.isAVLBalanced()).toBe(true);
165
+ expect(tree.getHeight()).toBe(4);
166
+
167
+ expect(tree.delete(10)[0].deleted?.key).toBe(10);
168
+ expect(tree.isAVLBalanced()).toBe(true);
169
+ expect(tree.getHeight()).toBe(3);
170
+
171
+ expect(tree.delete(15)[0].deleted?.key).toBe(15);
172
+ expect(tree.isAVLBalanced()).toBe(true);
173
+
174
+ expect(tree.getHeight()).toBe(3);
175
+
176
+ expect(tree.delete(5)[0].deleted?.key).toBe(5);
177
+ expect(tree.isAVLBalanced()).toBe(true);
178
+ expect(tree.getHeight()).toBe(3);
179
+
180
+ expect(tree.delete(13)[0].deleted?.key).toBe(13);
181
+ expect(tree.isAVLBalanced()).toBe(true);
182
+ expect(tree.getHeight()).toBe(3);
183
+
184
+ expect(tree.delete(3)[0].deleted?.key).toBe(3);
185
+ expect(tree.isAVLBalanced()).toBe(true);
186
+ expect(tree.getHeight()).toBe(3);
187
+
188
+ expect(tree.delete(8)[0].deleted?.key).toBe(8);
189
+ expect(tree.isAVLBalanced()).toBe(true);
190
+ expect(tree.getHeight()).toBe(3);
191
+
192
+ expect(tree.delete(6)[0].deleted?.key).toBe(6);
193
+ expect(tree.delete(6).length).toBe(0);
194
+ expect(tree.isAVLBalanced()).toBe(true);
195
+ expect(tree.getHeight()).toBe(2);
196
+
197
+ expect(tree.delete(7)[0].deleted?.key).toBe(7);
198
+ expect(tree.isAVLBalanced()).toBe(true);
199
+ expect(tree.getHeight()).toBe(2);
200
+
201
+ expect(tree.delete(9)[0].deleted?.key).toBe(9);
202
+ expect(tree.isAVLBalanced()).toBe(true);
203
+ expect(tree.getHeight()).toBe(2);
204
+ expect(tree.delete(14)[0].deleted?.key).toBe(14);
205
+ expect(tree.isAVLBalanced()).toBe(true);
206
+ expect(tree.getHeight()).toBe(1);
207
+
208
+ expect(tree.isAVLBalanced()).toBe(true);
209
+ const lastBFSIds = tree.bfs();
210
+ expect(lastBFSIds[0]).toBe(12);
211
+ expect(lastBFSIds[1]).toBe(2);
212
+ expect(lastBFSIds[2]).toBe(16);
213
+
214
+ const lastBFSNodes = tree.bfs(node => node);
215
+ expect(lastBFSNodes[0].key).toBe(12);
216
+ expect(lastBFSNodes[1].key).toBe(2);
217
+ expect(lastBFSNodes[2].key).toBe(16);
218
+ });
219
+ });
220
+
112
221
  describe('AVLTree APIs test', () => {
113
222
  const avl = new AVLTree<{id: number; text: string}>();
114
223
  beforeEach(() => {
@@ -127,3 +236,34 @@ describe('AVLTree APIs test', () => {
127
236
  expect(bfsRes[0]).toBe(2);
128
237
  });
129
238
  });
239
+
240
+ describe('AVLTree', () => {
241
+ it('should balance the tree using _balanceLR when nodes are added', () => {
242
+ const avlTree = new AVLTree();
243
+ avlTree.add(10, 'A');
244
+ avlTree.add(5, 'B');
245
+ avlTree.add(15, 'C');
246
+ avlTree.add(3, 'D');
247
+ avlTree.add(7, 'E');
248
+
249
+ // Adding nodes to trigger _balanceLR
250
+ avlTree.add(12, 'F');
251
+
252
+ // You can add more specific assertions to check the tree's balance and structure.
253
+ });
254
+
255
+ it('should balance the tree using _balanceLR when nodes are deleted', () => {
256
+ const avlTree = new AVLTree();
257
+ avlTree.add(10, 'A');
258
+ avlTree.add(5, 'B');
259
+ avlTree.add(15, 'C');
260
+ avlTree.add(3, 'D');
261
+ avlTree.add(7, 'E');
262
+ avlTree.add(12, 'F');
263
+
264
+ // Deleting nodes to trigger _balanceLR
265
+ avlTree.delete(3);
266
+
267
+ // You can add more specific assertions to check the tree's balance and structure.
268
+ });
269
+ });
@@ -283,3 +283,38 @@ function loopLowerBoundTests(bit: BinaryIndexedTree, values: number[]) {
283
283
  }
284
284
  }
285
285
  }
286
+
287
+ describe('', () => {
288
+ class NumArrayDC {
289
+ private _tree: BinaryIndexedTree;
290
+ private readonly _nums: number[];
291
+
292
+ constructor(nums: number[]) {
293
+ this._nums = nums;
294
+ this._tree = new BinaryIndexedTree({max: nums.length + 1});
295
+ for (let i = 0; i < nums.length; i++) {
296
+ this._tree.update(i + 1, nums[i]);
297
+ }
298
+ }
299
+
300
+ update(index: number, val: number): void {
301
+ this._tree.update(index + 1, val - this._nums[index]);
302
+ this._nums[index] = val;
303
+ }
304
+
305
+ sumRange(left: number, right: number): number {
306
+ return this._tree.getPrefixSum(right + 1) - this._tree.getPrefixSum(left);
307
+ }
308
+ }
309
+
310
+ it('', () => {
311
+ const numArray = new NumArrayDC([1, 3, 5, 8, 2, 9, 4, 5, 8, 1, 3, 2]);
312
+ expect(numArray.sumRange(0, 8)).toBe(45);
313
+ expect(numArray.sumRange(0, 2)).toBe(9);
314
+ numArray.update(1, 2);
315
+ expect(numArray.sumRange(0, 2)).toBe(8);
316
+ expect(numArray.sumRange(3, 4)).toBe(10);
317
+ numArray.update(3, 2);
318
+ expect(numArray.sumRange(3, 4)).toBe(4);
319
+ });
320
+ });