data-structure-typed 1.52.8 → 1.53.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.
- package/CHANGELOG.md +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +151 -151
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +22 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +64 -47
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +28 -26
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +240 -141
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +395 -269
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/cjs/data-structures/binary-tree/bst.js +114 -91
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +35 -31
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +22 -23
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +59 -48
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.js +3 -3
- package/dist/cjs/interfaces/binary-tree.d.ts +5 -5
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +22 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +64 -46
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +28 -25
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +240 -141
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +395 -268
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/mjs/data-structures/binary-tree/bst.js +114 -89
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +35 -30
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +22 -23
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +59 -47
- package/dist/mjs/data-structures/trie/trie.js +3 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +5 -5
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/umd/data-structure-typed.js +617 -482
- package/dist/umd/data-structure-typed.min.js +5 -5
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +60 -55
- package/src/data-structures/binary-tree/avl-tree.ts +31 -35
- package/src/data-structures/binary-tree/binary-tree.ts +461 -385
- package/src/data-structures/binary-tree/bst.ts +155 -128
- package/src/data-structures/binary-tree/rb-tree.ts +37 -39
- package/src/data-structures/binary-tree/tree-multi-map.ts +57 -60
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/interfaces/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/binary-tree.ts +14 -15
- package/src/types/data-structures/binary-tree/bst.ts +4 -4
- package/test/integration/bst.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +113 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +107 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +169 -26
- package/test/unit/data-structures/binary-tree/bst.test.ts +326 -21
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +139 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BinaryTreeNode, BST, BSTNode } from '../../../../src';
|
|
2
|
-
import { isDebugTest,
|
|
2
|
+
import { isDebugTest, isTestStackOverflow, SYSTEM_MAX_CALL_STACK } from '../../../config';
|
|
3
3
|
|
|
4
4
|
const isDebug = isDebugTest;
|
|
5
5
|
|
|
@@ -16,22 +16,27 @@ describe('BST operations test', () => {
|
|
|
16
16
|
expect(isAdd0).toBe(true);
|
|
17
17
|
expect(bst.get(0)).toBe('0');
|
|
18
18
|
});
|
|
19
|
+
|
|
19
20
|
it('should addMany undefined and null', () => {
|
|
20
21
|
const bst = new BST<number, string>();
|
|
21
22
|
const addManyWithUndefined = bst.addMany([1, undefined, 3]);
|
|
22
|
-
|
|
23
|
-
// expect(addManyWithUndefined).toEqual([true, false, true]);
|
|
24
|
-
expect(addManyWithUndefined).toEqual([true, true]);
|
|
23
|
+
expect(addManyWithUndefined).toEqual([true, false, true]);
|
|
25
24
|
expect(bst.get(undefined)).toBe(undefined);
|
|
26
25
|
const addManyWithNull = bst.addMany([1, null, 3, 4]);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
expect(addManyWithNull).toEqual([true, false, true, true]);
|
|
27
|
+
const addManyEntriesWithNull = bst.addMany([
|
|
28
|
+
[1, '1'],
|
|
29
|
+
[null, 'null'],
|
|
30
|
+
[3, '3'],
|
|
31
|
+
[4, '4']
|
|
32
|
+
]);
|
|
33
|
+
expect(addManyEntriesWithNull).toEqual([true, false, true, true]);
|
|
30
34
|
expect(bst.get(null)).toBe(undefined);
|
|
31
35
|
const node0 = bst.add(0, '0');
|
|
32
36
|
expect(node0).toBe(true);
|
|
33
37
|
expect(bst.get(0)).toBe('0');
|
|
34
38
|
});
|
|
39
|
+
|
|
35
40
|
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
36
41
|
const bst = new BST<number, number>();
|
|
37
42
|
expect(bst).toBeInstanceOf(BST);
|
|
@@ -445,22 +450,25 @@ describe('BST operations test', () => {
|
|
|
445
450
|
expect(bfsNodes[2].key).toBe(16);
|
|
446
451
|
});
|
|
447
452
|
|
|
448
|
-
it('should
|
|
453
|
+
it('should keyValueNodeEntryRawToNodeAndValue', () => {
|
|
449
454
|
const bst = new BST<number>();
|
|
450
|
-
const node0 = bst.
|
|
451
|
-
expect(node0).toEqual(
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
455
|
+
const node0 = bst.keyValueNodeEntryRawToNodeAndValue(0);
|
|
456
|
+
expect(node0).toEqual([
|
|
457
|
+
{
|
|
458
|
+
_left: undefined,
|
|
459
|
+
_right: undefined,
|
|
460
|
+
key: 0,
|
|
461
|
+
parent: undefined,
|
|
462
|
+
value: undefined
|
|
463
|
+
},
|
|
464
|
+
undefined
|
|
465
|
+
]);
|
|
458
466
|
|
|
459
|
-
const nodeUndefined = bst.
|
|
460
|
-
expect(nodeUndefined).
|
|
467
|
+
const nodeUndefined = bst.keyValueNodeEntryRawToNodeAndValue(undefined);
|
|
468
|
+
expect(nodeUndefined).toEqual([undefined, undefined]);
|
|
461
469
|
|
|
462
|
-
const nodeNull = bst.
|
|
463
|
-
expect(nodeNull).
|
|
470
|
+
const nodeNull = bst.keyValueNodeEntryRawToNodeAndValue(null);
|
|
471
|
+
expect(nodeNull).toEqual([undefined, undefined]);
|
|
464
472
|
});
|
|
465
473
|
});
|
|
466
474
|
|
|
@@ -491,7 +499,7 @@ describe('BST operations test recursively', () => {
|
|
|
491
499
|
expect(nodeId10?.key).toBe(10);
|
|
492
500
|
|
|
493
501
|
const nodeVal9 = bst.getNode(node => node.value === 9);
|
|
494
|
-
expect(nodeVal9?.key).toBe(undefined);
|
|
502
|
+
expect(bst.get(nodeVal9?.key)).toBe(undefined);
|
|
495
503
|
|
|
496
504
|
const leftMost = bst.getLeftMost();
|
|
497
505
|
expect(leftMost).toBe(1);
|
|
@@ -1205,3 +1213,300 @@ describe('BST iterative methods test', () => {
|
|
|
1205
1213
|
expect(balanced.leaves(node => node?.value)).toEqual(['a', 'f', 'd', 'i']);
|
|
1206
1214
|
});
|
|
1207
1215
|
});
|
|
1216
|
+
|
|
1217
|
+
describe('BST operations map mode test', () => {
|
|
1218
|
+
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
1219
|
+
const bst = new BST<number, number>([], { isMapMode: true });
|
|
1220
|
+
expect(bst).toBeInstanceOf(BST);
|
|
1221
|
+
bst.add([11, 11]);
|
|
1222
|
+
bst.add([3, 3]);
|
|
1223
|
+
const idsAndValues: [number, number][] = [
|
|
1224
|
+
[15, 15],
|
|
1225
|
+
[1, 1],
|
|
1226
|
+
[8, 8],
|
|
1227
|
+
[13, 13],
|
|
1228
|
+
[16, 16],
|
|
1229
|
+
[2, 2],
|
|
1230
|
+
[6, 6],
|
|
1231
|
+
[9, 9],
|
|
1232
|
+
[12, 12],
|
|
1233
|
+
[14, 14],
|
|
1234
|
+
[4, 4],
|
|
1235
|
+
[7, 7],
|
|
1236
|
+
[10, 10],
|
|
1237
|
+
[5, 5]
|
|
1238
|
+
];
|
|
1239
|
+
bst.addMany(idsAndValues, undefined, false);
|
|
1240
|
+
expect(bst.root).toBeInstanceOf(BSTNode);
|
|
1241
|
+
|
|
1242
|
+
if (bst.root) expect(bst.root.key).toBe(11);
|
|
1243
|
+
|
|
1244
|
+
expect(bst.size).toBe(16);
|
|
1245
|
+
|
|
1246
|
+
expect(bst.has(6)).toBe(true);
|
|
1247
|
+
|
|
1248
|
+
const node6 = bst.getNode(6);
|
|
1249
|
+
expect(node6 && bst.getHeight(6)).toBe(2);
|
|
1250
|
+
expect(node6 && bst.getDepth(6)).toBe(3);
|
|
1251
|
+
|
|
1252
|
+
const nodeId10 = bst.getNode(10);
|
|
1253
|
+
expect(nodeId10?.key).toBe(10);
|
|
1254
|
+
|
|
1255
|
+
const nodeVal9 = bst.getNode(node => node.key === 9);
|
|
1256
|
+
expect(nodeVal9?.key).toBe(9);
|
|
1257
|
+
|
|
1258
|
+
const leftMost = bst.getLeftMost();
|
|
1259
|
+
expect(leftMost).toBe(1);
|
|
1260
|
+
|
|
1261
|
+
expect(bst.isBST()).toBe(true);
|
|
1262
|
+
|
|
1263
|
+
const node15 = bst.getNode(15);
|
|
1264
|
+
const minNodeBySpecificNode = node15 && bst.getLeftMost(node => node, node15);
|
|
1265
|
+
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
1266
|
+
|
|
1267
|
+
const nodes = bst.getNodes(node => node.key === 15);
|
|
1268
|
+
expect(nodes.map(node => node.key)).toEqual([15]);
|
|
1269
|
+
});
|
|
1270
|
+
|
|
1271
|
+
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
1272
|
+
const objBST = new BST<number, { name: string; age: number }>([], { isMapMode: true });
|
|
1273
|
+
expect(objBST).toBeInstanceOf(BST);
|
|
1274
|
+
objBST.add([11, { name: '11', age: 11 }]);
|
|
1275
|
+
objBST.add([3, { name: '3', age: 3 }]);
|
|
1276
|
+
|
|
1277
|
+
objBST.addMany(
|
|
1278
|
+
[15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5],
|
|
1279
|
+
[
|
|
1280
|
+
{ name: 'Alice', age: 15 },
|
|
1281
|
+
{ name: 'Bob', age: 1 },
|
|
1282
|
+
{ name: 'Charlie', age: 8 },
|
|
1283
|
+
{ name: 'David', age: 13 },
|
|
1284
|
+
{ name: 'Emma', age: 16 },
|
|
1285
|
+
{ name: 'Frank', age: 2 },
|
|
1286
|
+
{ name: 'Grace', age: 6 },
|
|
1287
|
+
{ name: 'Hannah', age: 9 },
|
|
1288
|
+
{ name: 'Isaac', age: 12 },
|
|
1289
|
+
{ name: 'Jack', age: 14 },
|
|
1290
|
+
{ name: 'Katie', age: 4 },
|
|
1291
|
+
{ name: 'Liam', age: 7 },
|
|
1292
|
+
{ name: 'Mia', age: 10 },
|
|
1293
|
+
{ name: 'Noah', age: 5 }
|
|
1294
|
+
],
|
|
1295
|
+
false
|
|
1296
|
+
);
|
|
1297
|
+
|
|
1298
|
+
expect(objBST.root).toBeInstanceOf(BSTNode);
|
|
1299
|
+
|
|
1300
|
+
if (objBST.root) expect(objBST.root.key).toBe(11);
|
|
1301
|
+
|
|
1302
|
+
expect(objBST.has(6)).toBe(true);
|
|
1303
|
+
|
|
1304
|
+
const node6 = objBST.getNode(6);
|
|
1305
|
+
expect(node6 && objBST.getHeight(node6)).toBe(2);
|
|
1306
|
+
expect(node6 && objBST.getDepth(node6)).toBe(3);
|
|
1307
|
+
|
|
1308
|
+
const nodeId10 = objBST.getNode(10);
|
|
1309
|
+
expect(nodeId10?.key).toBe(10);
|
|
1310
|
+
|
|
1311
|
+
const nodeVal9 = objBST.getNode(9);
|
|
1312
|
+
expect(nodeVal9?.key).toBe(9);
|
|
1313
|
+
|
|
1314
|
+
const leftMost = objBST.getLeftMost();
|
|
1315
|
+
expect(leftMost).toBe(1);
|
|
1316
|
+
|
|
1317
|
+
const node15 = objBST.getNode(15);
|
|
1318
|
+
expect(objBST.get(node15)).toEqual({
|
|
1319
|
+
name: 'Alice',
|
|
1320
|
+
age: 15
|
|
1321
|
+
});
|
|
1322
|
+
});
|
|
1323
|
+
|
|
1324
|
+
it('should keyValueNodeEntryRawToNodeAndValue', () => {
|
|
1325
|
+
const bst = new BST<number>([], { isMapMode: true });
|
|
1326
|
+
const node0 = bst.keyValueNodeEntryRawToNodeAndValue(0);
|
|
1327
|
+
expect(node0).toEqual([
|
|
1328
|
+
{
|
|
1329
|
+
_left: undefined,
|
|
1330
|
+
_right: undefined,
|
|
1331
|
+
key: 0,
|
|
1332
|
+
parent: undefined,
|
|
1333
|
+
value: undefined
|
|
1334
|
+
},
|
|
1335
|
+
undefined
|
|
1336
|
+
]);
|
|
1337
|
+
|
|
1338
|
+
const nodeUndefined = bst.keyValueNodeEntryRawToNodeAndValue(undefined);
|
|
1339
|
+
expect(nodeUndefined).toEqual([undefined, undefined]);
|
|
1340
|
+
|
|
1341
|
+
const nodeNull = bst.keyValueNodeEntryRawToNodeAndValue(null);
|
|
1342
|
+
expect(nodeNull).toEqual([undefined, undefined]);
|
|
1343
|
+
});
|
|
1344
|
+
});
|
|
1345
|
+
|
|
1346
|
+
describe('BST operations map mode test recursively', () => {
|
|
1347
|
+
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
1348
|
+
const bst = new BST<number>([], {
|
|
1349
|
+
iterationType: 'RECURSIVE',
|
|
1350
|
+
isMapMode: true
|
|
1351
|
+
});
|
|
1352
|
+
expect(bst).toBeInstanceOf(BST);
|
|
1353
|
+
bst.add([11, 11]);
|
|
1354
|
+
bst.add([3, 3]);
|
|
1355
|
+
const idsAndValues = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
1356
|
+
bst.addMany(idsAndValues, undefined, false);
|
|
1357
|
+
expect(bst.root).toBeInstanceOf(BSTNode);
|
|
1358
|
+
|
|
1359
|
+
if (bst.root) expect(bst.root.key).toBe(11);
|
|
1360
|
+
|
|
1361
|
+
expect(bst.size).toBe(16);
|
|
1362
|
+
|
|
1363
|
+
expect(bst.has(6)).toBe(true);
|
|
1364
|
+
|
|
1365
|
+
const node6 = bst.getNode(6);
|
|
1366
|
+
expect(node6 && bst.getHeight(6)).toBe(2);
|
|
1367
|
+
expect(node6 && bst.getDepth(6)).toBe(3);
|
|
1368
|
+
|
|
1369
|
+
const nodeId10 = bst.getNode(10);
|
|
1370
|
+
expect(bst.get(10)).toBe(undefined);
|
|
1371
|
+
expect(nodeId10?.key).toBe(10);
|
|
1372
|
+
|
|
1373
|
+
const nodeVal9 = bst.getNode(node => node.key === 9);
|
|
1374
|
+
expect(bst.get(nodeVal9?.key)).toBe(undefined);
|
|
1375
|
+
});
|
|
1376
|
+
|
|
1377
|
+
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
1378
|
+
const objBST = new BST<number, { key: number; keyA: number }>([], { isMapMode: true });
|
|
1379
|
+
expect(objBST).toBeInstanceOf(BST);
|
|
1380
|
+
objBST.add([11, { key: 11, keyA: 11 }]);
|
|
1381
|
+
objBST.add([3, { key: 3, keyA: 3 }]);
|
|
1382
|
+
const entries: [number, { key: number; keyA: number }][] = [
|
|
1383
|
+
[15, { key: 15, keyA: 15 }],
|
|
1384
|
+
[1, { key: 1, keyA: 1 }],
|
|
1385
|
+
[8, { key: 8, keyA: 8 }],
|
|
1386
|
+
[13, { key: 13, keyA: 13 }],
|
|
1387
|
+
[16, { key: 16, keyA: 16 }],
|
|
1388
|
+
[2, { key: 2, keyA: 2 }],
|
|
1389
|
+
[6, { key: 6, keyA: 6 }],
|
|
1390
|
+
[9, { key: 9, keyA: 9 }],
|
|
1391
|
+
[12, { key: 12, keyA: 12 }],
|
|
1392
|
+
[14, { key: 14, keyA: 14 }],
|
|
1393
|
+
[4, { key: 4, keyA: 4 }],
|
|
1394
|
+
[7, { key: 7, keyA: 7 }],
|
|
1395
|
+
[10, { key: 10, keyA: 10 }],
|
|
1396
|
+
[5, { key: 5, keyA: 5 }]
|
|
1397
|
+
];
|
|
1398
|
+
|
|
1399
|
+
objBST.addMany(entries, undefined, false);
|
|
1400
|
+
|
|
1401
|
+
expect(objBST.root).toBeInstanceOf(BSTNode);
|
|
1402
|
+
|
|
1403
|
+
if (objBST.root) expect(objBST.root.key).toBe(11);
|
|
1404
|
+
|
|
1405
|
+
expect(objBST.has(6)).toBe(true);
|
|
1406
|
+
|
|
1407
|
+
const node6 = objBST.getNode(6);
|
|
1408
|
+
expect(objBST.get(6)).toEqual({
|
|
1409
|
+
key: 6,
|
|
1410
|
+
keyA: 6
|
|
1411
|
+
});
|
|
1412
|
+
expect(node6 && objBST.getHeight(node6)).toBe(2);
|
|
1413
|
+
expect(node6 && objBST.getDepth(node6)).toBe(3);
|
|
1414
|
+
|
|
1415
|
+
const nodeId10 = objBST.getNode(10);
|
|
1416
|
+
expect(nodeId10?.key).toBe(10);
|
|
1417
|
+
|
|
1418
|
+
const nodeVal9 = objBST.getNode(9);
|
|
1419
|
+
expect(nodeVal9?.key).toBe(9);
|
|
1420
|
+
|
|
1421
|
+
const leftMost = objBST.getLeftMost();
|
|
1422
|
+
expect(leftMost).toBe(1);
|
|
1423
|
+
|
|
1424
|
+
const node15 = objBST.getNode(15);
|
|
1425
|
+
expect(objBST.get(node15)).toEqual({
|
|
1426
|
+
key: 15,
|
|
1427
|
+
keyA: 15
|
|
1428
|
+
});
|
|
1429
|
+
});
|
|
1430
|
+
});
|
|
1431
|
+
|
|
1432
|
+
describe('BST iterative methods map mode test', () => {
|
|
1433
|
+
let bst: BST<number, string>;
|
|
1434
|
+
beforeEach(() => {
|
|
1435
|
+
bst = new BST();
|
|
1436
|
+
bst.addMany(
|
|
1437
|
+
[
|
|
1438
|
+
[1, 'a'],
|
|
1439
|
+
[2, 'b'],
|
|
1440
|
+
[3, 'c']
|
|
1441
|
+
],
|
|
1442
|
+
[],
|
|
1443
|
+
false
|
|
1444
|
+
);
|
|
1445
|
+
});
|
|
1446
|
+
|
|
1447
|
+
it('should clone work well', () => {
|
|
1448
|
+
const cloned = bst.clone();
|
|
1449
|
+
expect(cloned.root?.left).toBe(undefined);
|
|
1450
|
+
expect(cloned.get(cloned.root?.right)).toBe('b');
|
|
1451
|
+
});
|
|
1452
|
+
|
|
1453
|
+
it('should collapsed, unbalanced, balanced bst leaves', () => {
|
|
1454
|
+
const collapsedToLinkedList = new BST();
|
|
1455
|
+
collapsedToLinkedList.addMany(
|
|
1456
|
+
[
|
|
1457
|
+
[1, 'a'],
|
|
1458
|
+
[2, 'b'],
|
|
1459
|
+
[3, 'c'],
|
|
1460
|
+
[4, 'd'],
|
|
1461
|
+
[5, 'e'],
|
|
1462
|
+
[6, 'f'],
|
|
1463
|
+
[7, 'g'],
|
|
1464
|
+
[8, 'h'],
|
|
1465
|
+
[9, 'i']
|
|
1466
|
+
],
|
|
1467
|
+
[],
|
|
1468
|
+
false
|
|
1469
|
+
);
|
|
1470
|
+
|
|
1471
|
+
expect(collapsedToLinkedList.leaves()).toEqual([9]);
|
|
1472
|
+
|
|
1473
|
+
const unbalanced = new BST();
|
|
1474
|
+
unbalanced.addMany(
|
|
1475
|
+
[
|
|
1476
|
+
[2, 'b'],
|
|
1477
|
+
[1, 'a'],
|
|
1478
|
+
[3, 'c'],
|
|
1479
|
+
[4, 'd'],
|
|
1480
|
+
[5, 'e'],
|
|
1481
|
+
[6, 'f'],
|
|
1482
|
+
[7, 'g'],
|
|
1483
|
+
[8, 'h'],
|
|
1484
|
+
[9, 'i']
|
|
1485
|
+
],
|
|
1486
|
+
[],
|
|
1487
|
+
false
|
|
1488
|
+
);
|
|
1489
|
+
|
|
1490
|
+
expect(unbalanced.leaves()).toEqual([1, 9]);
|
|
1491
|
+
|
|
1492
|
+
const balanced = new BST();
|
|
1493
|
+
balanced.addMany(
|
|
1494
|
+
[
|
|
1495
|
+
[2, 'b'],
|
|
1496
|
+
[1, 'a'],
|
|
1497
|
+
[3, 'c'],
|
|
1498
|
+
[4, 'd'],
|
|
1499
|
+
[5, 'e'],
|
|
1500
|
+
[6, 'f'],
|
|
1501
|
+
[7, 'g'],
|
|
1502
|
+
[8, 'h'],
|
|
1503
|
+
[9, 'i']
|
|
1504
|
+
],
|
|
1505
|
+
[],
|
|
1506
|
+
true
|
|
1507
|
+
);
|
|
1508
|
+
|
|
1509
|
+
expect(balanced.leaves()).toEqual([1, 6, 4, 9]);
|
|
1510
|
+
expect(balanced.leaves(node => balanced.get(node?.key))).toEqual(['a', 'f', 'd', 'i']);
|
|
1511
|
+
});
|
|
1512
|
+
});
|
|
@@ -817,7 +817,8 @@ describe('TreeMultiMap iterative methods test', () => {
|
|
|
817
817
|
expect(treeMM.getComputedCount()).toBe(21);
|
|
818
818
|
const cloned = treeMM.clone();
|
|
819
819
|
expect(cloned.root?.left?.key).toBe(1);
|
|
820
|
-
expect(cloned.root?.right
|
|
820
|
+
if (cloned.isMapMode) expect(cloned.get(cloned.root?.right)).toBe('c');
|
|
821
|
+
else expect(cloned.root?.right?.value).toBe(undefined);
|
|
821
822
|
});
|
|
822
823
|
|
|
823
824
|
it('should keys', () => {
|
|
@@ -835,3 +836,140 @@ describe('TreeMultiMap iterative methods test', () => {
|
|
|
835
836
|
expect(leaves).toEqual([1, 3]);
|
|
836
837
|
});
|
|
837
838
|
});
|
|
839
|
+
|
|
840
|
+
describe('TreeMultiMap count map mode', () => {
|
|
841
|
+
let tmm: TreeMultiMap<number>;
|
|
842
|
+
beforeEach(() => {
|
|
843
|
+
tmm = new TreeMultiMap<number>([], { isMapMode: true });
|
|
844
|
+
});
|
|
845
|
+
|
|
846
|
+
it('Should added node count ', () => {
|
|
847
|
+
tmm.addMany([
|
|
848
|
+
[1, 1],
|
|
849
|
+
[2, 2],
|
|
850
|
+
[3, 3],
|
|
851
|
+
[4, 4],
|
|
852
|
+
[5, 5]
|
|
853
|
+
]);
|
|
854
|
+
const newNode = new TreeMultiMapNode(3, undefined, 10);
|
|
855
|
+
tmm.add(newNode, 33, 20);
|
|
856
|
+
// TODO expect(tmm.count).toBe(25);
|
|
857
|
+
expect(tmm.count).toBe(15);
|
|
858
|
+
expect(tmm.getComputedCount()).toBe(15);
|
|
859
|
+
expect(tmm.getNode(3)?.count).toBe(11);
|
|
860
|
+
});
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
describe('TreeMultiMap operations test1 map mode', () => {
|
|
864
|
+
it('should perform various operations on a Binary Search Tree with numeric values1', () => {
|
|
865
|
+
const tmm = new TreeMultiMap<number, number>([], { isMapMode: true });
|
|
866
|
+
|
|
867
|
+
expect(tmm instanceof TreeMultiMap);
|
|
868
|
+
|
|
869
|
+
tmm.add([11, 11]);
|
|
870
|
+
tmm.add([3, 3]);
|
|
871
|
+
const idAndValues: [number, number][] = [
|
|
872
|
+
[11, 11],
|
|
873
|
+
[3, 3],
|
|
874
|
+
[15, 15],
|
|
875
|
+
[1, 1],
|
|
876
|
+
[8, 8],
|
|
877
|
+
[13, 13],
|
|
878
|
+
[16, 16],
|
|
879
|
+
[2, 2],
|
|
880
|
+
[6, 6],
|
|
881
|
+
[9, 9],
|
|
882
|
+
[12, 12],
|
|
883
|
+
[14, 14],
|
|
884
|
+
[4, 4],
|
|
885
|
+
[7, 7],
|
|
886
|
+
[10, 10],
|
|
887
|
+
[5, 5]
|
|
888
|
+
];
|
|
889
|
+
tmm.addMany(idAndValues);
|
|
890
|
+
expect(tmm.root instanceof TreeMultiMapNode);
|
|
891
|
+
|
|
892
|
+
if (tmm.root) expect(tmm.root.key == 11);
|
|
893
|
+
|
|
894
|
+
expect(tmm.size).toBe(16);
|
|
895
|
+
expect(tmm.count).toBe(18);
|
|
896
|
+
expect(tmm.getComputedCount()).toBe(18);
|
|
897
|
+
|
|
898
|
+
expect(tmm.has(6));
|
|
899
|
+
if (isDebug) tmm.print();
|
|
900
|
+
expect(tmm.getHeight(6)).toBe(1);
|
|
901
|
+
expect(tmm.getDepth(6)).toBe(3);
|
|
902
|
+
const nodeId10 = tmm.getNode(10);
|
|
903
|
+
expect(nodeId10?.key).toBe(10);
|
|
904
|
+
|
|
905
|
+
const nodeVal9 = tmm.getNode(node => node.key === 9);
|
|
906
|
+
expect(nodeVal9?.key).toBe(9);
|
|
907
|
+
});
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
describe('TreeMultiMap operations test recursively1 map mode', () => {
|
|
911
|
+
it('should perform various operations on a Binary Search Tree with numeric values1', () => {
|
|
912
|
+
const tmm = new TreeMultiMap<number>([], {
|
|
913
|
+
iterationType: 'RECURSIVE',
|
|
914
|
+
isMapMode: true
|
|
915
|
+
});
|
|
916
|
+
|
|
917
|
+
expect(tmm instanceof TreeMultiMap);
|
|
918
|
+
tmm.add([11, 11]);
|
|
919
|
+
tmm.add([3, 3]);
|
|
920
|
+
const idAndValues: [number, number][] = [
|
|
921
|
+
[11, 11],
|
|
922
|
+
[3, 3],
|
|
923
|
+
[15, 15],
|
|
924
|
+
[1, 1],
|
|
925
|
+
[8, 8],
|
|
926
|
+
[13, 13],
|
|
927
|
+
[16, 16],
|
|
928
|
+
[2, 2],
|
|
929
|
+
[6, 6],
|
|
930
|
+
[9, 9],
|
|
931
|
+
[12, 12],
|
|
932
|
+
[14, 14],
|
|
933
|
+
[4, 4],
|
|
934
|
+
[7, 7],
|
|
935
|
+
[10, 10],
|
|
936
|
+
[5, 5]
|
|
937
|
+
];
|
|
938
|
+
tmm.addMany(idAndValues);
|
|
939
|
+
expect(tmm.root).toBeInstanceOf(TreeMultiMapNode);
|
|
940
|
+
|
|
941
|
+
if (tmm.root) expect(tmm.root.key).toBe(5);
|
|
942
|
+
|
|
943
|
+
expect(tmm.size).toBe(16);
|
|
944
|
+
expect(tmm.count).toBe(18);
|
|
945
|
+
expect(tmm.getComputedCount()).toBe(18);
|
|
946
|
+
|
|
947
|
+
expect(tmm.has(6));
|
|
948
|
+
|
|
949
|
+
expect(tmm.getHeight(6)).toBe(1);
|
|
950
|
+
expect(tmm.getDepth(6)).toBe(3);
|
|
951
|
+
const nodeId10 = tmm.getNode(10);
|
|
952
|
+
expect(nodeId10?.key).toBe(10);
|
|
953
|
+
|
|
954
|
+
const nodeVal9 = tmm.getNode(node => node.key === 9);
|
|
955
|
+
expect(nodeVal9?.key).toBe(9);
|
|
956
|
+
});
|
|
957
|
+
});
|
|
958
|
+
|
|
959
|
+
describe('TreeMultiMap iterative methods testm ap mode', () => {
|
|
960
|
+
let treeMM: TreeMultiMap<number, string>;
|
|
961
|
+
beforeEach(() => {
|
|
962
|
+
treeMM = new TreeMultiMap<number, string>([], { isMapMode: true });
|
|
963
|
+
treeMM.add(1, 'a', 10);
|
|
964
|
+
treeMM.add([2, 'b'], undefined, 10);
|
|
965
|
+
treeMM.add([3, 'c'], undefined, 1);
|
|
966
|
+
});
|
|
967
|
+
|
|
968
|
+
it('should clone work well', () => {
|
|
969
|
+
expect(treeMM.count).toBe(21);
|
|
970
|
+
expect(treeMM.getComputedCount()).toBe(21);
|
|
971
|
+
const cloned = treeMM.clone();
|
|
972
|
+
expect(cloned.root?.left?.key).toBe(1);
|
|
973
|
+
expect(cloned.get(cloned.root?.right)).toBe('c');
|
|
974
|
+
});
|
|
975
|
+
});
|