data-structure-typed 1.47.6 → 1.47.8
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.
- package/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +188 -32
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/cjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/cjs/data-structures/graph/abstract-graph.js +30 -30
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/cjs/data-structures/graph/directed-graph.js +28 -28
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/cjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/cjs/data-structures/queue/queue.js +13 -13
- package/dist/cjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/cjs/data-structures/stack/stack.js +7 -7
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +6 -1
- package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +133 -128
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/mjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +59 -138
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/mjs/data-structures/graph/abstract-graph.js +30 -30
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/mjs/data-structures/graph/directed-graph.js +28 -28
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/mjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/mjs/data-structures/queue/queue.js +13 -13
- package/dist/mjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/mjs/data-structures/stack/stack.js +7 -7
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +6 -1
- package/dist/mjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/umd/data-structure-typed.js +583 -627
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/segment-tree.ts +10 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/graph/abstract-graph.ts +46 -46
- package/src/data-structures/graph/directed-graph.ts +40 -40
- package/src/data-structures/graph/undirected-graph.ts +26 -26
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
- package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
- package/src/data-structures/queue/queue.ts +13 -13
- package/src/data-structures/stack/stack.ts +9 -9
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/graph/abstract-graph.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +1 -2
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +158 -2
- package/test/performance/data-structures/comparison/comparison.test.ts +5 -5
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -51
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/data-structures/graph/abstract-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +10 -10
- package/test/unit/data-structures/graph/undirected-graph.test.ts +3 -3
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -3
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +61 -5
- package/tsconfig-cjs.json +1 -1
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
<meta charset='UTF-8'>
|
|
5
5
|
<title>CDN Test</title>
|
|
6
6
|
<!-- <script src="../../dist/umd/data-structure-typed.min.js"></script>-->
|
|
7
|
-
<script src="../../dist/umd/data-structure-typed.js"></script
|
|
8
|
-
|
|
7
|
+
<!-- <script src="../../dist/umd/data-structure-typed.js"></script>-->
|
|
8
|
+
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>
|
|
9
9
|
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.42.2/dist/umd/data-structure-typed.min.js'></script>-->
|
|
10
10
|
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.43.3/dist/umd/data-structure-typed.min.js'></script>-->
|
|
11
11
|
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.44.0/dist/umd/data-structure-typed.min.js'></script>-->
|
|
@@ -215,6 +215,162 @@
|
|
|
215
215
|
} catch (e) {
|
|
216
216
|
console.error(e);
|
|
217
217
|
}
|
|
218
|
+
|
|
219
|
+
try {
|
|
220
|
+
const {
|
|
221
|
+
AVLTree,
|
|
222
|
+
BinaryTree,
|
|
223
|
+
BST,
|
|
224
|
+
Deque,
|
|
225
|
+
DoublyLinkedList,
|
|
226
|
+
HashMap,
|
|
227
|
+
Heap,
|
|
228
|
+
MaxPriorityQueue,
|
|
229
|
+
MinHeap,
|
|
230
|
+
MinPriorityQueue,
|
|
231
|
+
Queue,
|
|
232
|
+
RedBlackTree,
|
|
233
|
+
SinglyLinkedList,
|
|
234
|
+
Stack,
|
|
235
|
+
TreeMultimap,
|
|
236
|
+
Trie
|
|
237
|
+
} = dataStructureTyped;
|
|
238
|
+
const orgArr = [6, 1, 2, 7, 5, 3, 4, 9, 8];
|
|
239
|
+
const orgStrArr = ["trie", "trial", "trick", "trip", "tree", "trend", "triangle", "track", "trace", "transmit"];
|
|
240
|
+
const entries = [[6, 6], [1, 1], [2, 2], [7, 7], [5, 5], [3, 3], [4, 4], [9, 9], [8, 8]];
|
|
241
|
+
|
|
242
|
+
const queue = new Queue(orgArr);
|
|
243
|
+
queue.print(); // [6, 1, 2, 7, 5, 3, 4, 9, 8]
|
|
244
|
+
|
|
245
|
+
const deque = new Deque(orgArr);
|
|
246
|
+
deque.print(); // [6, 1, 2, 7, 5, 3, 4, 9, 8]
|
|
247
|
+
|
|
248
|
+
const sList = new SinglyLinkedList(orgArr);
|
|
249
|
+
sList.print(); // [6, 1, 2, 7, 5, 3, 4, 9, 8]
|
|
250
|
+
|
|
251
|
+
const dList = new DoublyLinkedList(orgArr);
|
|
252
|
+
dList.print(); // [6, 1, 2, 7, 5, 3, 4, 9, 8]
|
|
253
|
+
|
|
254
|
+
const stack = new Stack(orgArr);
|
|
255
|
+
stack.print(); // [6, 1, 2, 7, 5, 3, 4, 9, 8]
|
|
256
|
+
|
|
257
|
+
const minHeap = new MinHeap(orgArr);
|
|
258
|
+
minHeap.print(); // [1, 5, 2, 7, 6, 3, 4, 9, 8]
|
|
259
|
+
|
|
260
|
+
const maxPQ = new MaxPriorityQueue(orgArr);
|
|
261
|
+
maxPQ.print(); // [9, 8, 4, 7, 5, 2, 3, 1, 6]
|
|
262
|
+
|
|
263
|
+
const biTree = new BinaryTree(entries);
|
|
264
|
+
biTree.print();
|
|
265
|
+
// ___6___
|
|
266
|
+
// / \
|
|
267
|
+
// ___1_ _2_
|
|
268
|
+
// / \ / \
|
|
269
|
+
// _7_ 5 3 4
|
|
270
|
+
// / \
|
|
271
|
+
// 9 8
|
|
272
|
+
|
|
273
|
+
const bst = new BST(entries);
|
|
274
|
+
bst.print();
|
|
275
|
+
// _____5___
|
|
276
|
+
// / \
|
|
277
|
+
// _2_ _7_
|
|
278
|
+
// / \ / \
|
|
279
|
+
// 1 3_ 6 8_
|
|
280
|
+
// \ \
|
|
281
|
+
// 4 9
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
const rbTree = new RedBlackTree(entries);
|
|
285
|
+
rbTree.print();
|
|
286
|
+
// ___4___
|
|
287
|
+
// / \
|
|
288
|
+
// _2_ _6___
|
|
289
|
+
// / \ / \
|
|
290
|
+
// 1 3 5 _8_
|
|
291
|
+
// / \
|
|
292
|
+
// 7 9
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
const avl = new AVLTree(entries);
|
|
296
|
+
avl.print();
|
|
297
|
+
// ___4___
|
|
298
|
+
// / \
|
|
299
|
+
// _2_ _6___
|
|
300
|
+
// / \ / \
|
|
301
|
+
// 1 3 5 _8_
|
|
302
|
+
// / \
|
|
303
|
+
// 7 9
|
|
304
|
+
|
|
305
|
+
const treeMulti = new TreeMultimap(entries);
|
|
306
|
+
treeMulti.print();
|
|
307
|
+
// ___4___
|
|
308
|
+
// / \
|
|
309
|
+
// _2_ _6___
|
|
310
|
+
// / \ / \
|
|
311
|
+
// 1 3 5 _8_
|
|
312
|
+
// / \
|
|
313
|
+
// 7 9
|
|
314
|
+
|
|
315
|
+
const hm = new HashMap(entries);
|
|
316
|
+
hm.print() // [[6, 6], [1, 1], [2, 2], [7, 7], [5, 5], [3, 3], [4, 4], [9, 9], [8, 8]]
|
|
317
|
+
|
|
318
|
+
const rbTreeH = new RedBlackTree(hm);
|
|
319
|
+
rbTreeH.print();
|
|
320
|
+
// ___4___
|
|
321
|
+
// / \
|
|
322
|
+
// _2_ _6___
|
|
323
|
+
// / \ / \
|
|
324
|
+
// 1 3 5 _8_
|
|
325
|
+
// / \
|
|
326
|
+
// 7 9
|
|
327
|
+
|
|
328
|
+
const pq = new MinPriorityQueue(orgArr);
|
|
329
|
+
pq.print(); // [1, 5, 2, 7, 6, 3, 4, 9, 8]
|
|
330
|
+
|
|
331
|
+
const bst1 = new BST(pq);
|
|
332
|
+
bst1.print();
|
|
333
|
+
// _____5___
|
|
334
|
+
// / \
|
|
335
|
+
// _2_ _7_
|
|
336
|
+
// / \ / \
|
|
337
|
+
// 1 3_ 6 8_
|
|
338
|
+
// \ \
|
|
339
|
+
// 4 9
|
|
340
|
+
|
|
341
|
+
const dq1 = new Deque(orgArr);
|
|
342
|
+
dq1.print(); // [6, 1, 2, 7, 5, 3, 4, 9, 8]
|
|
343
|
+
const rbTree1 = new RedBlackTree(dq1);
|
|
344
|
+
rbTree1.print();
|
|
345
|
+
// _____5___
|
|
346
|
+
// / \
|
|
347
|
+
// _2___ _7___
|
|
348
|
+
// / \ / \
|
|
349
|
+
// 1 _4 6 _9
|
|
350
|
+
// / /
|
|
351
|
+
// 3 8
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
const trie2 = new Trie(orgStrArr);
|
|
355
|
+
trie2.print(); // ['trie', 'trial', 'triangle', 'trick', 'trip', 'tree', 'trend', 'track', 'trace', 'transmit']
|
|
356
|
+
const heap2 = new Heap(trie2, { comparator: (a, b) => Number(a) - Number(b) });
|
|
357
|
+
heap2.print(); // ['transmit', 'trace', 'tree', 'trend', 'track', 'trial', 'trip', 'trie', 'trick', 'triangle']
|
|
358
|
+
const dq2 = new Deque(heap2);
|
|
359
|
+
dq2.print(); // ['transmit', 'trace', 'tree', 'trend', 'track', 'trial', 'trip', 'trie', 'trick', 'triangle']
|
|
360
|
+
const entries2 = dq2.map((el, i) => [i, el]);
|
|
361
|
+
const avl2 = new AVLTree(entries2);
|
|
362
|
+
avl2.print();
|
|
363
|
+
// ___3_______
|
|
364
|
+
// / \
|
|
365
|
+
// _1_ ___7_
|
|
366
|
+
// / \ / \
|
|
367
|
+
// 0 2 _5_ 8_
|
|
368
|
+
// / \ \
|
|
369
|
+
// 4 6 9
|
|
370
|
+
|
|
371
|
+
} catch (e) {
|
|
372
|
+
console.error(e);
|
|
373
|
+
}
|
|
218
374
|
</script>
|
|
219
375
|
|
|
220
376
|
</body>
|
|
@@ -23,18 +23,18 @@ const arrHundredThousand = getRandomIntArray(HUNDRED_THOUSAND, 0, HUNDRED_THOUSA
|
|
|
23
23
|
suite
|
|
24
24
|
.add(`SRC PQ ${TEN_THOUSAND.toLocaleString()} add`, () => {
|
|
25
25
|
|
|
26
|
-
const pq = new SRCPriorityQueue<number>(
|
|
26
|
+
const pq = new SRCPriorityQueue<number>();
|
|
27
27
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
28
28
|
})
|
|
29
29
|
.add(`CJS PQ ${TEN_THOUSAND.toLocaleString()} add`, () => {
|
|
30
30
|
|
|
31
31
|
|
|
32
|
-
const pq = new CJSPriorityQueue<number>(
|
|
32
|
+
const pq = new CJSPriorityQueue<number>();
|
|
33
33
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
34
34
|
})
|
|
35
35
|
.add(`MJS PQ ${TEN_THOUSAND.toLocaleString()} add`, () => {
|
|
36
36
|
|
|
37
|
-
const pq = new MJSPriorityQueue<number>([],{ comparator: (a, b) => b - a });
|
|
37
|
+
const pq = new MJSPriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
38
38
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
39
39
|
});
|
|
40
40
|
|
|
@@ -54,13 +54,13 @@ suite
|
|
|
54
54
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.pop();
|
|
55
55
|
})
|
|
56
56
|
.add(`CJS PQ ${TEN_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
57
|
-
const pq = new CJSPriorityQueue<number>([],{ comparator: (a, b) => b - a });
|
|
57
|
+
const pq = new CJSPriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
58
58
|
|
|
59
59
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
60
60
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.pop();
|
|
61
61
|
})
|
|
62
62
|
.add(`MJS PQ ${TEN_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
63
|
-
const pq = new MJSPriorityQueue<number>([],{ comparator: (a, b) => b - a });
|
|
63
|
+
const pq = new MJSPriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
64
64
|
|
|
65
65
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
66
66
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.pop();
|
|
@@ -57,7 +57,7 @@ suite
|
|
|
57
57
|
})
|
|
58
58
|
.add(`${LINEAR.toLocaleString()} insertBefore`, () => {
|
|
59
59
|
const doublyList = new DoublyLinkedList<number>();
|
|
60
|
-
let midNode: DoublyLinkedListNode |
|
|
60
|
+
let midNode: DoublyLinkedListNode | undefined;
|
|
61
61
|
const midIndex = Math.floor(LINEAR / 2);
|
|
62
62
|
for (let i = 0; i < LINEAR; i++) {
|
|
63
63
|
doublyList.push(i);
|
|
@@ -19,7 +19,7 @@ suite
|
|
|
19
19
|
})
|
|
20
20
|
.add(`${TEN_THOUSAND.toLocaleString()} insertBefore`, () => {
|
|
21
21
|
const singlyList = new SinglyLinkedList<number>();
|
|
22
|
-
let midSinglyNode: SinglyLinkedListNode |
|
|
22
|
+
let midSinglyNode: SinglyLinkedListNode | undefined;
|
|
23
23
|
const midIndex = Math.floor(TEN_THOUSAND / 2);
|
|
24
24
|
for (let i = 0; i < TEN_THOUSAND; i++) {
|
|
25
25
|
singlyList.push(i);
|
|
@@ -5,7 +5,7 @@ describe('AVL Tree Test', () => {
|
|
|
5
5
|
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
6
6
|
const tree = new AVLTree<number>();
|
|
7
7
|
|
|
8
|
-
for (const i of arr) tree.add(i, i);
|
|
8
|
+
for (const i of arr) tree.add([i, i]);
|
|
9
9
|
|
|
10
10
|
tree.add(null);
|
|
11
11
|
const node6 = tree.getNode(6);
|
|
@@ -114,7 +114,7 @@ describe('AVL Tree Test recursively', () => {
|
|
|
114
114
|
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
115
115
|
const tree = new AVLTree<number>([], { iterationType: IterationType.RECURSIVE });
|
|
116
116
|
|
|
117
|
-
for (const i of arr) tree.add(i, i);
|
|
117
|
+
for (const i of arr) tree.add([i, i]);
|
|
118
118
|
|
|
119
119
|
const node6 = tree.getNode(6);
|
|
120
120
|
|
|
@@ -230,7 +230,7 @@ describe('AVLTree APIs test', () => {
|
|
|
230
230
|
avl.add(node2);
|
|
231
231
|
const node3 = new AVLTreeNode(3, { id: 3, text: 'text3' });
|
|
232
232
|
avl.add(node3);
|
|
233
|
-
avl.add(
|
|
233
|
+
avl.add([3, { id: 3, text: 'text33' }]);
|
|
234
234
|
|
|
235
235
|
const bfsRes = avl.bfs(node => node.key);
|
|
236
236
|
expect(bfsRes[0]).toBe(2);
|
|
@@ -240,26 +240,26 @@ describe('AVLTree APIs test', () => {
|
|
|
240
240
|
describe('AVLTree', () => {
|
|
241
241
|
it('should balance the tree using _balanceLR when nodes are added', () => {
|
|
242
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');
|
|
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
248
|
|
|
249
249
|
// Adding nodes to trigger _balanceLR
|
|
250
|
-
avlTree.add(12, 'F');
|
|
250
|
+
avlTree.add([12, 'F']);
|
|
251
251
|
|
|
252
252
|
// You can add more specific assertions to check the tree's balance and structure.
|
|
253
253
|
});
|
|
254
254
|
|
|
255
255
|
it('should balance the tree using _balanceLR when nodes are deleted', () => {
|
|
256
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');
|
|
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
263
|
|
|
264
264
|
// Deleting nodes to trigger _balanceLR
|
|
265
265
|
avlTree.delete(3);
|
|
@@ -279,7 +279,7 @@ describe('AVLTree', () => {
|
|
|
279
279
|
avl.add(node2);
|
|
280
280
|
const node3 = new AVLTreeNode(3, { id: 3, text: 'text3' });
|
|
281
281
|
avl.add(node3);
|
|
282
|
-
avl.add(
|
|
282
|
+
avl.add([3, { id: 3, text: 'text33' }]);
|
|
283
283
|
|
|
284
284
|
const bfsRes = avl.bfs(node => node);
|
|
285
285
|
expect(bfsRes[0]?.key).toBe(2);
|
|
@@ -291,9 +291,9 @@ describe('AVLTree iterative methods test', () => {
|
|
|
291
291
|
let avl: AVLTree<string>;
|
|
292
292
|
beforeEach(() => {
|
|
293
293
|
avl = new AVLTree();
|
|
294
|
-
avl.add(1, 'a');
|
|
295
|
-
avl.add(2, 'b');
|
|
296
|
-
avl.add(3, 'c');
|
|
294
|
+
avl.add([1, 'a']);
|
|
295
|
+
avl.add([2, 'b']);
|
|
296
|
+
avl.add([3, 'c']);
|
|
297
297
|
});
|
|
298
298
|
|
|
299
299
|
test('The node obtained by get Node should match the node type', () => {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { BinaryTree, BinaryTreeNode, IterationType } from '../../../../src';
|
|
1
|
+
import { BinaryTree, BinaryTreeNode, FamilyPosition, IterationType } from '../../../../src';
|
|
2
2
|
import { getRandomIntArray } from '../../../utils';
|
|
3
|
-
import { FamilyPosition } from 'binary-tree-typed';
|
|
4
3
|
// import {isDebugTest} from '../../../config';
|
|
5
4
|
|
|
6
5
|
// const isDebug = isDebugTest;
|
|
@@ -137,10 +136,10 @@ describe('BinaryTree', () => {
|
|
|
137
136
|
});
|
|
138
137
|
|
|
139
138
|
it('should add and find nodes', () => {
|
|
140
|
-
tree.add(1, 1);
|
|
139
|
+
tree.add([1, 1]);
|
|
141
140
|
tree.add(undefined);
|
|
142
|
-
tree.add(2, 2);
|
|
143
|
-
tree.add(3, 3);
|
|
141
|
+
tree.add([2, 2]);
|
|
142
|
+
tree.add([3, 3]);
|
|
144
143
|
|
|
145
144
|
expect(tree.has(1)).toBe(true);
|
|
146
145
|
expect(tree.has(2)).toBe(true);
|
|
@@ -395,9 +394,9 @@ describe('BinaryTree', () => {
|
|
|
395
394
|
});
|
|
396
395
|
|
|
397
396
|
it('should add nodes to the tree', () => {
|
|
398
|
-
tree.add(5, 'A');
|
|
399
|
-
tree.add(3, 'B');
|
|
400
|
-
tree.add(7, 'C');
|
|
397
|
+
tree.add([5, 'A']);
|
|
398
|
+
tree.add([3, 'B']);
|
|
399
|
+
tree.add([7, 'C']);
|
|
401
400
|
|
|
402
401
|
expect(tree.size).toBe(3);
|
|
403
402
|
expect(tree.isEmpty()).toBe(false);
|
|
@@ -405,9 +404,9 @@ describe('BinaryTree', () => {
|
|
|
405
404
|
});
|
|
406
405
|
|
|
407
406
|
it('should clear the BinaryTree', () => {
|
|
408
|
-
tree.add(5, 'A');
|
|
409
|
-
tree.add(3, 'B');
|
|
410
|
-
tree.add(7, 'C');
|
|
407
|
+
tree.add([5, 'A']);
|
|
408
|
+
tree.add([3, 'B']);
|
|
409
|
+
tree.add([7, 'C']);
|
|
411
410
|
|
|
412
411
|
tree.clear();
|
|
413
412
|
|
|
@@ -417,9 +416,9 @@ describe('BinaryTree', () => {
|
|
|
417
416
|
});
|
|
418
417
|
|
|
419
418
|
it('should get nodes by key', () => {
|
|
420
|
-
tree.add(5, 'A');
|
|
421
|
-
tree.add(3, 'B');
|
|
422
|
-
tree.add(7, 'C');
|
|
419
|
+
tree.add([5, 'A']);
|
|
420
|
+
tree.add([3, 'B']);
|
|
421
|
+
tree.add([7, 'C']);
|
|
423
422
|
|
|
424
423
|
const nodeA = tree.getNode(5);
|
|
425
424
|
const nodeB = tree.getNode(3);
|
|
@@ -431,7 +430,7 @@ describe('BinaryTree', () => {
|
|
|
431
430
|
});
|
|
432
431
|
|
|
433
432
|
it('should return null when getting a non-existent node', () => {
|
|
434
|
-
tree.add(5, 'A');
|
|
433
|
+
tree.add([5, 'A']);
|
|
435
434
|
|
|
436
435
|
const node = tree.getNode(3);
|
|
437
436
|
|
|
@@ -439,18 +438,18 @@ describe('BinaryTree', () => {
|
|
|
439
438
|
});
|
|
440
439
|
|
|
441
440
|
it('should get the depth of a node', () => {
|
|
442
|
-
tree.add(5, 'A');
|
|
443
|
-
tree.add(3, 'B');
|
|
444
|
-
tree.add(7, 'C');
|
|
441
|
+
tree.add([5, 'A']);
|
|
442
|
+
tree.add([3, 'B']);
|
|
443
|
+
tree.add([7, 'C']);
|
|
445
444
|
|
|
446
445
|
expect(tree.getDepth(7)).toBe(1);
|
|
447
446
|
expect(tree.getDepth(3)).toBe(1);
|
|
448
447
|
});
|
|
449
448
|
|
|
450
449
|
it('should get the height of the tree', () => {
|
|
451
|
-
tree.add(5, 'A');
|
|
452
|
-
tree.add(3, 'B');
|
|
453
|
-
tree.add(7, 'C');
|
|
450
|
+
tree.add([5, 'A']);
|
|
451
|
+
tree.add([3, 'B']);
|
|
452
|
+
tree.add([7, 'C']);
|
|
454
453
|
|
|
455
454
|
expect(tree.getHeight()).toBe(1);
|
|
456
455
|
expect(tree.getHeight(undefined, IterationType.RECURSIVE)).toBe(1);
|
|
@@ -458,17 +457,17 @@ describe('BinaryTree', () => {
|
|
|
458
457
|
});
|
|
459
458
|
|
|
460
459
|
it('should check if the tree is a binary search tree', () => {
|
|
461
|
-
tree.add(5, 'A');
|
|
462
|
-
tree.add(3, 'B');
|
|
463
|
-
tree.add(7, 'C');
|
|
460
|
+
tree.add([5, 'A']);
|
|
461
|
+
tree.add([3, 'B']);
|
|
462
|
+
tree.add([7, 'C']);
|
|
464
463
|
|
|
465
464
|
expect(tree.isBST()).toBe(true);
|
|
466
465
|
});
|
|
467
466
|
|
|
468
467
|
it('should perform a depth-first traversal', () => {
|
|
469
|
-
tree.add(5, 'A');
|
|
470
|
-
tree.add(3, 'B');
|
|
471
|
-
tree.add(7, 'C');
|
|
468
|
+
tree.add([5, 'A']);
|
|
469
|
+
tree.add([3, 'B']);
|
|
470
|
+
tree.add([7, 'C']);
|
|
472
471
|
|
|
473
472
|
const result = tree.dfs();
|
|
474
473
|
expect(result).toEqual([3, 5, 7]);
|
|
@@ -476,9 +475,9 @@ describe('BinaryTree', () => {
|
|
|
476
475
|
});
|
|
477
476
|
|
|
478
477
|
it('should perform a breadth-first traversal', () => {
|
|
479
|
-
tree.add(5, 'A');
|
|
480
|
-
tree.add(3, 'B');
|
|
481
|
-
tree.add(7, 'C');
|
|
478
|
+
tree.add([5, 'A']);
|
|
479
|
+
tree.add([3, 'B']);
|
|
480
|
+
tree.add([7, 'C']);
|
|
482
481
|
|
|
483
482
|
const result = tree.bfs(node => node.key);
|
|
484
483
|
expect(result).toEqual([5, 3, 7]);
|
|
@@ -486,9 +485,9 @@ describe('BinaryTree', () => {
|
|
|
486
485
|
});
|
|
487
486
|
|
|
488
487
|
it('should list levels of the tree', () => {
|
|
489
|
-
tree.add(5, 'A');
|
|
490
|
-
tree.add(3, 'B');
|
|
491
|
-
tree.add(7, 'C');
|
|
488
|
+
tree.add([5, 'A']);
|
|
489
|
+
tree.add([3, 'B']);
|
|
490
|
+
tree.add([7, 'C']);
|
|
492
491
|
|
|
493
492
|
const levels = tree.listLevels();
|
|
494
493
|
expect(levels).toEqual([[5], [3, 7]]);
|
|
@@ -496,9 +495,9 @@ describe('BinaryTree', () => {
|
|
|
496
495
|
});
|
|
497
496
|
|
|
498
497
|
it('should delete nodes from the tree', () => {
|
|
499
|
-
tree.add(5, 'A');
|
|
500
|
-
tree.add(3, 'B');
|
|
501
|
-
tree.add(7, 'C');
|
|
498
|
+
tree.add([5, 'A']);
|
|
499
|
+
tree.add([3, 'B']);
|
|
500
|
+
tree.add([7, 'C']);
|
|
502
501
|
|
|
503
502
|
tree.delete(3);
|
|
504
503
|
|
|
@@ -507,17 +506,17 @@ describe('BinaryTree', () => {
|
|
|
507
506
|
});
|
|
508
507
|
|
|
509
508
|
it('should check if the tree is perfectly balanced', () => {
|
|
510
|
-
tree.add(5, 'A');
|
|
511
|
-
tree.add(3, 'B');
|
|
512
|
-
tree.add(7, 'C');
|
|
509
|
+
tree.add([5, 'A']);
|
|
510
|
+
tree.add([3, 'B']);
|
|
511
|
+
tree.add([7, 'C']);
|
|
513
512
|
|
|
514
513
|
expect(tree.isPerfectlyBalanced()).toBe(true);
|
|
515
514
|
});
|
|
516
515
|
|
|
517
516
|
it('should get nodes by a custom callback', () => {
|
|
518
|
-
tree.add(5, 'A');
|
|
519
|
-
tree.add(3, 'B');
|
|
520
|
-
tree.add(7, 'C');
|
|
517
|
+
tree.add([5, 'A']);
|
|
518
|
+
tree.add([3, 'B']);
|
|
519
|
+
tree.add([7, 'C']);
|
|
521
520
|
|
|
522
521
|
const nodes = tree.getNodes('B', (node: BinaryTreeNode<string>) => node.value);
|
|
523
522
|
|
|
@@ -537,9 +536,9 @@ describe('BinaryTree', () => {
|
|
|
537
536
|
});
|
|
538
537
|
|
|
539
538
|
it('should perform Morris traversal', () => {
|
|
540
|
-
tree.add(5, 'A');
|
|
541
|
-
tree.add(3, 'B');
|
|
542
|
-
tree.add(7, 'C');
|
|
539
|
+
tree.add([5, 'A']);
|
|
540
|
+
tree.add([3, 'B']);
|
|
541
|
+
tree.add([7, 'C']);
|
|
543
542
|
|
|
544
543
|
tree.iterationType = IterationType.ITERATIVE;
|
|
545
544
|
expect([...tree]).toEqual([[3, "B"], [5, "A"], [7, "C"]]);
|
|
@@ -553,9 +552,9 @@ describe('BinaryTree', () => {
|
|
|
553
552
|
});
|
|
554
553
|
|
|
555
554
|
it('should perform delete all', () => {
|
|
556
|
-
tree.add(5, 'A');
|
|
557
|
-
tree.add(3, 'B');
|
|
558
|
-
tree.add(7, 'C');
|
|
555
|
+
tree.add([5, 'A']);
|
|
556
|
+
tree.add([3, 'B']);
|
|
557
|
+
tree.add([7, 'C']);
|
|
559
558
|
|
|
560
559
|
tree.delete(5);
|
|
561
560
|
tree.delete(7);
|
|
@@ -570,9 +569,9 @@ describe('BinaryTree iterative methods test', () => {
|
|
|
570
569
|
let binaryTree: BinaryTree<string>;
|
|
571
570
|
beforeEach(() => {
|
|
572
571
|
binaryTree = new BinaryTree();
|
|
573
|
-
binaryTree.add(1, 'a');
|
|
574
|
-
binaryTree.add(2, 'b');
|
|
575
|
-
binaryTree.add(3, 'c');
|
|
572
|
+
binaryTree.add([1, 'a']);
|
|
573
|
+
binaryTree.add([2, 'b']);
|
|
574
|
+
binaryTree.add([3, 'c']);
|
|
576
575
|
});
|
|
577
576
|
|
|
578
577
|
test('The node obtained by get Node should match the node type', () => {
|