data-structure-typed 1.36.8 → 1.36.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 (74) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/README.md +8 -0
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -5
  4. package/dist/data-structures/binary-tree/avl-tree.js +6 -6
  5. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +18 -85
  7. package/dist/data-structures/binary-tree/binary-tree.js +76 -128
  8. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  9. package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -16
  10. package/dist/data-structures/binary-tree/tree-multiset.js +10 -20
  11. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  12. package/dist/data-structures/hash/hash-map.d.ts +1 -1
  13. package/dist/data-structures/hash/hash-map.js +1 -1
  14. package/dist/data-structures/hash/hash-table.d.ts +3 -3
  15. package/dist/data-structures/hash/hash-table.js +3 -3
  16. package/dist/data-structures/heap/heap.js.map +1 -1
  17. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -3
  18. package/dist/data-structures/linked-list/skip-linked-list.js +3 -3
  19. package/dist/data-structures/queue/deque.d.ts +2 -2
  20. package/dist/data-structures/queue/deque.js +2 -2
  21. package/dist/data-structures/queue/queue.js +1 -1
  22. package/dist/data-structures/trie/trie.d.ts +2 -2
  23. package/dist/data-structures/trie/trie.js +2 -2
  24. package/dist/interfaces/binary-tree.d.ts +1 -1
  25. package/lib/data-structures/binary-tree/avl-tree.d.ts +5 -5
  26. package/lib/data-structures/binary-tree/avl-tree.js +6 -6
  27. package/lib/data-structures/binary-tree/binary-tree.d.ts +18 -85
  28. package/lib/data-structures/binary-tree/binary-tree.js +76 -128
  29. package/lib/data-structures/binary-tree/tree-multiset.d.ts +9 -16
  30. package/lib/data-structures/binary-tree/tree-multiset.js +10 -20
  31. package/lib/data-structures/hash/hash-map.d.ts +1 -1
  32. package/lib/data-structures/hash/hash-map.js +1 -1
  33. package/lib/data-structures/hash/hash-table.d.ts +3 -3
  34. package/lib/data-structures/hash/hash-table.js +3 -3
  35. package/lib/data-structures/linked-list/skip-linked-list.d.ts +3 -3
  36. package/lib/data-structures/linked-list/skip-linked-list.js +3 -3
  37. package/lib/data-structures/queue/deque.d.ts +2 -2
  38. package/lib/data-structures/queue/deque.js +2 -2
  39. package/lib/data-structures/queue/queue.js +1 -1
  40. package/lib/data-structures/trie/trie.d.ts +2 -2
  41. package/lib/data-structures/trie/trie.js +2 -2
  42. package/lib/interfaces/binary-tree.d.ts +1 -1
  43. package/package.json +6 -6
  44. package/src/data-structures/binary-tree/avl-tree.ts +6 -6
  45. package/src/data-structures/binary-tree/binary-tree.ts +79 -214
  46. package/src/data-structures/binary-tree/rb-tree.ts +3 -3
  47. package/src/data-structures/binary-tree/tree-multiset.ts +10 -21
  48. package/src/data-structures/hash/hash-map.ts +1 -1
  49. package/src/data-structures/hash/hash-table.ts +3 -3
  50. package/src/data-structures/heap/heap.ts +5 -2
  51. package/src/data-structures/linked-list/skip-linked-list.ts +3 -3
  52. package/src/data-structures/queue/deque.ts +2 -2
  53. package/src/data-structures/queue/queue.ts +1 -1
  54. package/src/data-structures/trie/trie.ts +2 -2
  55. package/src/interfaces/binary-tree.ts +1 -1
  56. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -14
  57. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
  58. package/test/unit/data-structures/binary-tree/bst.test.ts +28 -28
  59. package/test/unit/data-structures/binary-tree/overall.test.ts +3 -3
  60. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +1 -1
  61. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +28 -28
  62. package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
  63. package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
  64. package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
  65. package/test/unit/data-structures/hash/hash-table.test.ts +5 -5
  66. package/test/unit/data-structures/heap/heap.test.ts +15 -12
  67. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +7 -7
  68. package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
  69. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
  70. package/test/unit/data-structures/queue/deque.test.ts +3 -3
  71. package/test/unit/data-structures/trie/trie.test.ts +5 -5
  72. package/test/utils/big-o.ts +61 -55
  73. package/umd/bundle.min.js +1 -1
  74. package/umd/bundle.min.js.map +1 -1
@@ -1,5 +1,5 @@
1
1
  import {FibonacciHeap, MaxHeap, MinHeap} from '../../../../src';
2
- import {logBigOMetricsWrap} from "../../../utils";
2
+ import {logBigOMetricsWrap} from '../../../utils';
3
3
 
4
4
  describe('Heap Operation Test', () => {
5
5
  it('should numeric heap work well', function () {
@@ -201,10 +201,8 @@ describe('FibonacciHeap', () => {
201
201
  });
202
202
  });
203
203
 
204
-
205
204
  describe('FibonacciHeap Stress Test', () => {
206
205
  it('should handle a large number of elements efficiently', () => {
207
-
208
206
  const testByMagnitude = (magnitude: number) => {
209
207
  const heap = new FibonacciHeap<number>();
210
208
 
@@ -229,7 +227,7 @@ describe('FibonacciHeap Stress Test', () => {
229
227
 
230
228
  // Verify that the heap is now empty
231
229
  expect(heap.size).toBe(0);
232
- }
230
+ };
233
231
 
234
232
  testByMagnitude(1000);
235
233
 
@@ -238,13 +236,18 @@ describe('FibonacciHeap Stress Test', () => {
238
236
  // 150000, 200000, 250000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, 1000000
239
237
  // ].forEach(m => logBigOMetricsWrap<typeof testByMagnitude>(testByMagnitude, [m]));
240
238
  [
241
- 10, 100, 1000, 5000, 10000, 20000, 50000, 75000, 100000,
242
- 150000, 200000, 250000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, 1000000
243
- ].forEach(m => logBigOMetricsWrap((c: number) => {
244
- const result: number[] = [];
245
- for (let i = 0; i < c; i++) result.push(i);
246
- return result;
247
- } , [m], 'loopPush'));
248
-
239
+ 10, 100, 1000, 5000, 10000, 20000, 50000, 75000, 100000, 150000, 200000, 250000, 300000, 400000, 500000, 600000,
240
+ 700000, 800000, 900000, 1000000
241
+ ].forEach(m =>
242
+ logBigOMetricsWrap(
243
+ (c: number) => {
244
+ const result: number[] = [];
245
+ for (let i = 0; i < c; i++) result.push(i);
246
+ return result;
247
+ },
248
+ [m],
249
+ 'loopPush'
250
+ )
251
+ );
249
252
  });
250
253
  });
@@ -18,7 +18,7 @@ describe('SinglyLinkedList Operation Test', () => {
18
18
  });
19
19
 
20
20
  describe('pop', () => {
21
- it('should remove and return the last element of the list', () => {
21
+ it('should delete and return the last element of the list', () => {
22
22
  list.push(1);
23
23
  list.push(2);
24
24
  const popped = list.pop();
@@ -33,7 +33,7 @@ describe('SinglyLinkedList Operation Test', () => {
33
33
  });
34
34
 
35
35
  describe('shift', () => {
36
- it('should remove and return the first element of the list', () => {
36
+ it('should delete and return the first element of the list', () => {
37
37
  list.push(1);
38
38
  list.push(2);
39
39
  const shifted = list.shift();
@@ -109,7 +109,7 @@ describe('SinglyLinkedList Operation Test', () => {
109
109
  });
110
110
 
111
111
  describe('removeValue', () => {
112
- it('should remove the first occurrence of a value from the list', () => {
112
+ it('should delete the first occurrence of a value from the list', () => {
113
113
  list.push(1);
114
114
  list.push(2);
115
115
  list.push(3);
@@ -240,8 +240,8 @@ describe('SinglyLinkedList Operation Test', () => {
240
240
  });
241
241
  });
242
242
 
243
- describe('remove', () => {
244
- it('should remove and return the element at the specified index', () => {
243
+ describe('delete', () => {
244
+ it('should delete and return the element at the specified index', () => {
245
245
  list.push(1);
246
246
  list.push(2);
247
247
  list.push(3);
@@ -256,7 +256,7 @@ describe('SinglyLinkedList Operation Test', () => {
256
256
  expect(removed).toBeUndefined();
257
257
  });
258
258
 
259
- it('should remove and return the first element', () => {
259
+ it('should delete and return the first element', () => {
260
260
  list.push(1);
261
261
  list.push(2);
262
262
  const removed = list.deleteAt(0);
@@ -264,7 +264,7 @@ describe('SinglyLinkedList Operation Test', () => {
264
264
  expect(list.toArray()).toEqual([2]);
265
265
  });
266
266
 
267
- it('should remove and return the last element', () => {
267
+ it('should delete and return the last element', () => {
268
268
  list.push(1);
269
269
  list.push(2);
270
270
  const removed = list.deleteAt(1);
@@ -25,12 +25,12 @@ describe('SkipList', () => {
25
25
  expect(skipList.get(0)).toBeUndefined();
26
26
  });
27
27
 
28
- it('should remove elements correctly', () => {
28
+ it('should delete elements correctly', () => {
29
29
  skipList.add(1, 'One');
30
30
  skipList.add(2, 'Two');
31
31
  skipList.add(3, 'Three');
32
32
 
33
- skipList.remove(2);
33
+ skipList.delete(2);
34
34
 
35
35
  expect(skipList.get(2)).toBeUndefined(); // 修改这里的断言
36
36
  });
@@ -27,7 +27,7 @@ describe('MaxPriorityQueue Operation Test', () => {
27
27
  expect(priorityQueue.poll()?.keyA).toBe(1);
28
28
  });
29
29
 
30
- it('should return and remove the smallest element', () => {
30
+ it('should return and delete the smallest element', () => {
31
31
  const priorityQueue = new MaxPriorityQueue<number>();
32
32
  priorityQueue.add(5);
33
33
  priorityQueue.add(3);
@@ -16,7 +16,7 @@ describe('Deque Tests', () => {
16
16
  expect(deque.peekLast()).toBe(2);
17
17
  });
18
18
 
19
- it('should remove elements from the beginning and end', () => {
19
+ it('should delete elements from the beginning and end', () => {
20
20
  deque.addFirst(1);
21
21
  deque.addLast(2);
22
22
  deque.pollFirst();
@@ -69,7 +69,7 @@ describe('Deque Tests', () => {
69
69
  expect(objectDeque.peekLast()).toBe('two');
70
70
  });
71
71
 
72
- it('should remove elements from the beginning and end', () => {
72
+ it('should delete elements from the beginning and end', () => {
73
73
  objectDeque.addFirst('one');
74
74
  objectDeque.addLast('two');
75
75
  objectDeque.pollFirst();
@@ -106,7 +106,7 @@ describe('Deque Tests', () => {
106
106
  expect(arrayDeque.peekLast()).toBe(2);
107
107
  });
108
108
 
109
- it('should remove elements from the beginning and end', () => {
109
+ it('should delete elements from the beginning and end', () => {
110
110
  arrayDeque.addFirst(1);
111
111
  arrayDeque.addLast(2);
112
112
  arrayDeque.pollFirst();
@@ -81,15 +81,15 @@ describe('Trie', () => {
81
81
  expect(words).toEqual(['apple', 'application', 'app']);
82
82
  });
83
83
 
84
- it('should remove words from Trie', () => {
84
+ it('should delete words from Trie', () => {
85
85
  const trie = new Trie();
86
86
  trie.add('apple');
87
87
  trie.add('app');
88
88
  expect(trie.has('apple')).toBe(true);
89
- trie.remove('apple');
89
+ trie.delete('apple');
90
90
  expect(trie.has('apple')).toBe(false);
91
91
  expect(trie.has('app')).toBe(true);
92
- trie.remove('app');
92
+ trie.delete('app');
93
93
  expect(trie.has('app')).toBe(false);
94
94
  });
95
95
 
@@ -772,9 +772,9 @@ describe('Trie operations', () => {
772
772
  test('Remove Words', () => {
773
773
  trie.add('apple');
774
774
  trie.add('banana');
775
- expect(trie.remove('apple')).toBe(true);
775
+ expect(trie.delete('apple')).toBe(true);
776
776
  expect(trie.has('apple')).toBe(false);
777
- expect(trie.remove('cherry')).toBe(false);
777
+ expect(trie.delete('cherry')).toBe(false);
778
778
  });
779
779
 
780
780
  test('Case Sensitivity', () => {
@@ -1,4 +1,4 @@
1
- import {AnyFunction} from "../types";
1
+ import {AnyFunction} from '../types';
2
2
 
3
3
  const orderReducedBy = 2; // reduction of bigO's order compared to the baseline bigO
4
4
 
@@ -24,7 +24,7 @@ export const bigO = {
24
24
 
25
25
  function findPotentialN(input: any): number {
26
26
  let longestArray: any[] = [];
27
- let mostProperties: { [key: string]: any } = {};
27
+ let mostProperties: {[key: string]: any} = {};
28
28
 
29
29
  function recurse(obj: any) {
30
30
  if (Array.isArray(obj)) {
@@ -36,14 +36,14 @@ function findPotentialN(input: any): number {
36
36
  if (keys.length > Object.keys(mostProperties).length) {
37
37
  mostProperties = obj;
38
38
  }
39
- keys.forEach((key) => {
39
+ keys.forEach(key => {
40
40
  recurse(obj[key]);
41
41
  });
42
42
  }
43
43
  }
44
44
 
45
45
  if (Array.isArray(input)) {
46
- input.forEach((item) => {
46
+ input.forEach(item => {
47
47
  recurse(item);
48
48
  });
49
49
  } else {
@@ -66,20 +66,20 @@ function linearRegression(x: number[], y: number[]) {
66
66
  const slope = (n * sumXY - sumX * sumY) / (n * sumXSquared - sumX ** 2);
67
67
  const intercept = (sumY - slope * sumX) / n;
68
68
 
69
- const yHat = x.map((val) => slope * val + intercept);
69
+ const yHat = x.map(val => slope * val + intercept);
70
70
 
71
71
  const totalVariation = y.map((val, i) => (val - yHat[i]) ** 2).reduce((acc, val) => acc + val, 0);
72
- const explainedVariation = y.map((val) => (val - (sumY / n)) ** 2).reduce((acc, val) => acc + val, 0);
72
+ const explainedVariation = y.map(val => (val - sumY / n) ** 2).reduce((acc, val) => acc + val, 0);
73
73
 
74
74
  const rSquared = 1 - totalVariation / explainedVariation;
75
75
 
76
- return { slope, intercept, rSquared };
76
+ return {slope, intercept, rSquared};
77
77
  }
78
78
 
79
79
  function estimateBigO(runtimes: number[], dataSizes: number[]): string {
80
80
  // Make sure the input runtimes and data sizes have the same length
81
81
  if (runtimes.length !== dataSizes.length) {
82
- return "输入数组的长度不匹配";
82
+ return 'Lengths of input arrays do not match';
83
83
  }
84
84
 
85
85
  // Create an array to store the computational complexity of each data point
@@ -87,32 +87,32 @@ function estimateBigO(runtimes: number[], dataSizes: number[]): string {
87
87
 
88
88
  // Traverse different possible complexities
89
89
  const complexitiesToCheck: string[] = [
90
- "O(1)", // constant time complexity
91
- "O(log n)", // Logarithmic time complexity
92
- "O(n)", // linear time complexity
93
- "O(n log n)", // linear logarithmic time complexity
94
- "O(n^2)", // squared time complexity
90
+ 'O(1)', // constant time complexity
91
+ 'O(log n)', // Logarithmic time complexity
92
+ 'O(n)', // linear time complexity
93
+ 'O(n log n)', // linear logarithmic time complexity
94
+ 'O(n^2)' // squared time complexity
95
95
  ];
96
96
 
97
97
  for (const complexity of complexitiesToCheck) {
98
- // Calculate data points for fitting
99
- const fittedData: number[] = dataSizes.map((size) => {
100
- if (complexity === "O(1)") {
98
+ // Calculate data points for fitting
99
+ const fittedData: number[] = dataSizes.map(size => {
100
+ if (complexity === 'O(1)') {
101
101
  return 1; // constant time complexity
102
- } else if (complexity === "O(log n)") {
102
+ } else if (complexity === 'O(log n)') {
103
103
  return Math.log(size);
104
- } else if (complexity === "O(n)") {
104
+ } else if (complexity === 'O(n)') {
105
105
  return size;
106
- } else if (complexity === "O(n log n)") {
106
+ } else if (complexity === 'O(n log n)') {
107
107
  return size * Math.log(size);
108
- } else if (complexity === "O(n^2)") {
108
+ } else if (complexity === 'O(n^2)') {
109
109
  return size ** 2;
110
110
  } else {
111
- return size ** 10
111
+ return size ** 10;
112
112
  }
113
113
  });
114
114
 
115
- // Fit the data points using linear regression analysis
115
+ // Fit the data points using linear regression analysis
116
116
  const regressionResult = linearRegression(fittedData, runtimes);
117
117
 
118
118
  // Check the R-squared value of the fit. It is usually considered a valid fit if it is greater than 0.9.
@@ -123,13 +123,43 @@ function estimateBigO(runtimes: number[], dataSizes: number[]): string {
123
123
 
124
124
  // If there is no valid fitting result, return "cannot estimate", otherwise return the estimated time complexity
125
125
  if (complexities.length === 0) {
126
- return "Unable to estimate";
126
+ return 'Unable to estimate';
127
127
  } else {
128
- return complexities.join(" or ");
128
+ return complexities.join(' or ');
129
129
  }
130
130
  }
131
131
 
132
- const methodLogs: Map<string, [number, number][] > = new Map();
132
+ const methodLogs: Map<string, [number, number][]> = new Map();
133
+
134
+ export function logBigOMetricsWrap<F extends AnyFunction>(fn: F, args: Parameters<F>, fnName: string) {
135
+ const startTime = performance.now();
136
+ const result = fn(args);
137
+ const endTime = performance.now();
138
+ const runTime = endTime - startTime;
139
+ const methodName = `${fnName}`;
140
+ if (!methodLogs.has(methodName)) {
141
+ methodLogs.set(methodName, []);
142
+ }
143
+
144
+ const methodLog = methodLogs.get(methodName);
145
+
146
+ const maxDataSize = args.length === 1 && typeof args[0] === 'number' ? args[0] : findPotentialN(args);
147
+ if (methodLog) {
148
+ methodLog.push([runTime, maxDataSize]);
149
+
150
+ if (methodLog.length >= 20) {
151
+ console.log('triggered', methodName, methodLog);
152
+ const bigO = estimateBigO(
153
+ methodLog.map(([runTime]) => runTime),
154
+ methodLog.map(([runTime]) => runTime)
155
+ );
156
+ console.log(`Estimated Big O: ${bigO}`);
157
+ methodLogs.delete(methodName);
158
+ }
159
+ }
160
+
161
+ return result;
162
+ }
133
163
 
134
164
  export function logBigOMetrics(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
135
165
  const originalMethod = descriptor.value;
@@ -147,15 +177,18 @@ export function logBigOMetrics(target: any, propertyKey: string, descriptor: Pro
147
177
 
148
178
  const methodLog = methodLogs.get(methodName);
149
179
 
150
- const maxDataSize = args.length === 1 && typeof args[0] === "number" ? args[0] : findPotentialN(args);
180
+ const maxDataSize = args.length === 1 && typeof args[0] === 'number' ? args[0] : findPotentialN(args);
151
181
  if (methodLog) {
152
182
  methodLog.push([runTime, maxDataSize]);
153
183
 
154
184
  if (methodLog.length >= 20) {
155
185
  console.log('triggered', methodName, methodLog);
156
- const bigO = estimateBigO(methodLog.map(([runTime,]) => runTime), methodLog.map(([runTime,]) => runTime));
186
+ const bigO = estimateBigO(
187
+ methodLog.map(([runTime]) => runTime),
188
+ methodLog.map(([runTime]) => runTime)
189
+ );
157
190
  console.log(`Estimated Big O: ${bigO}`);
158
- methodLogs.delete(methodName)
191
+ methodLogs.delete(methodName);
159
192
  }
160
193
  }
161
194
 
@@ -164,30 +197,3 @@ export function logBigOMetrics(target: any, propertyKey: string, descriptor: Pro
164
197
 
165
198
  return descriptor;
166
199
  }
167
-
168
- export function logBigOMetricsWrap<F extends AnyFunction>(fn: F, args: Parameters<F>, fnName: string) {
169
- const startTime = performance.now();
170
- const result = fn(args);
171
- const endTime = performance.now();
172
- const runTime = endTime - startTime;
173
- const methodName = `${fnName}`;
174
- if (!methodLogs.has(methodName)) {
175
- methodLogs.set(methodName, []);
176
- }
177
-
178
- const methodLog = methodLogs.get(methodName);
179
-
180
- const maxDataSize = args.length === 1 && typeof args[0] === "number" ? args[0] : findPotentialN(args);
181
- if (methodLog) {
182
- methodLog.push([runTime, maxDataSize]);
183
-
184
- if (methodLog.length >= 20) {
185
- console.log('triggered', methodName, methodLog);
186
- const bigO = estimateBigO(methodLog.map(([runTime,]) => runTime), methodLog.map(([runTime,]) => runTime));
187
- console.log(`Estimated Big O: ${bigO}`);
188
- methodLogs.delete(methodName)
189
- }
190
- }
191
-
192
- return result;
193
- }