Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 | 19x 19x 19x 218x 218x 3657x 48x 161x 50x 5810x 142x 108x 142x 7052x 165x 142x 165x 981x 471x 218x 1396x 543x 31x 31x 31x 12x 12x 19x 19x 8x 11x 19x 8x 2x 2x 8x 8x 924x 8x 251x 8x 179x 8x 8x 8x 8x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 12x 12x 12x 5x 5x 72x 72x 72x 72x 72x 5x 45x 45x 45x 45x 45x 45x 42x 42x 42x 42x 24x 24x 24x 10x 14x 14x 24x 18x 18x 18x 18x 18x 18x 8x 10x 18x 42x 42x 42x 6x 6x 6x 14x 14x 6x 65x 65x 65x 65x 65x 65x 65x 65x 570x 570x 218x 570x 287x 570x 65x 5x 5x 5x 5x 5x 5x 5x 5x 201x 80x 80x 121x 121x 80x 80x 80x 80x 80x 80x 80x 41x 5x 5x 6x 6x 125x 125x 296x 296x 125x 125x 9x 9x 9x 9x 9x 29x 20x 9x 24x 24x 24x 24x 38x 14x 24x 12x 12x 12x 12x 12x 44x 44x 44x 44x 12x 12x 12x 44x 44x 44x 44x 12x 14x 14x 14x 14x 252x 252x 119x 119x 119x 14x 8x 8x 8x 8x 128x 128x 128x 128x 128x 8x 8x 18x 18x 18x 247x 34x 34x 34x 34x 333x 312x 96x 96x 216x 21x 2x 2x 19x 247x 247x 43x 43x 204x 204x 22x 22x 6x 16x | /**
* data-structure-typed
*
* @author Tyler Zeng
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
* @license MIT License
*/
import {trampoline} from '../../utils';
import type {
AbstractBinaryTreeNodeNested,
AbstractBinaryTreeNodeProperties,
AbstractBinaryTreeNodeProperty,
BinaryTreeDeletedResult,
BinaryTreeNodeId,
BinaryTreeNodePropertyName,
DFSOrderPattern,
NodeOrPropertyName
} from '../../types';
import {AbstractBinaryTreeOptions, FamilyPosition, LoopType} from '../../types';
import {IAbstractBinaryTree, IAbstractBinaryTreeNode} from '../../interfaces';
export abstract class AbstractBinaryTreeNode<T = any,
NEIGHBOR extends AbstractBinaryTreeNode<T, NEIGHBOR> = AbstractBinaryTreeNodeNested<T>> implements IAbstractBinaryTreeNode<T, NEIGHBOR> {
/**
* The constructor function initializes a BinaryTreeNode object with an id and an optional value.
* @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
* of the binary tree node. It is used to distinguish one node from another in the binary tree.
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It represents the value that will be
* stored in the binary tree node. If no value is provided, it will be set to undefined.
*/
protected constructor(id: BinaryTreeNodeId, val?: T) {
this._id = id;
this._val = val;
}
private _id: BinaryTreeNodeId;
get id(): BinaryTreeNodeId {
return this._id;
}
set id(v: BinaryTreeNodeId) {
this._id = v;
}
private _val: T | undefined;
get val(): T | undefined {
return this._val;
}
set val(value: T | undefined) {
this._val = value;
}
private _left: NEIGHBOR | null | undefined;
get left(): NEIGHBOR | null | undefined {
return this._left;
}
set left(v: NEIGHBOR | null | undefined) {
if (v) {
v.parent = this as unknown as NEIGHBOR;
}
this._left = v;
}
private _right: NEIGHBOR | null | undefined;
get right(): NEIGHBOR | null | undefined {
return this._right;
}
set right(v: NEIGHBOR | null | undefined) {
if (v) {
v.parent = this as unknown as NEIGHBOR;
}
this._right = v;
}
private _parent: NEIGHBOR | null | undefined;
get parent(): NEIGHBOR | null | undefined {
return this._parent;
}
set parent(v: NEIGHBOR | null | undefined) {
this._parent = v;
}
private _height = 0;
get height(): number {
return this._height;
}
set height(v: number) {
this._height = v;
}
/**
* The function determines the position of a node in a family tree structure.
* @returns a value of type `FamilyPosition`.
*/
get familyPosition(): FamilyPosition {
const that = this as unknown as NEIGHBOR;
if (that.parent) {
if (that.parent.left === that) {
Iif (that.left || that.right) {
return FamilyPosition.ROOT_LEFT;
} else {
return FamilyPosition.LEFT;
}
} else if (that.parent.right === that) {
if (that.left || that.right) {
return FamilyPosition.ROOT_RIGHT;
} else {
return FamilyPosition.RIGHT;
}
} else E{
return FamilyPosition.MAL_NODE;
}
} else E{
if (that.left || that.right) {
return FamilyPosition.ROOT;
} else {
return FamilyPosition.ISOLATED;
}
}
}
}
export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val'], N> = AbstractBinaryTreeNode>
implements IAbstractBinaryTree<N> {
/**
* The protected constructor initializes the options for an abstract binary tree.
* @param {AbstractBinaryTreeOptions} [options] - An optional object that contains configuration options for the binary
* tree.
*/
protected constructor(options?: AbstractBinaryTreeOptions) {
if (options !== undefined) {
const {loopType = LoopType.ITERATIVE} = options;
this._loopType = loopType;
}
this.clear();
}
private _root: N | null = null;
get root(): N | null {
return this._root;
}
private _size = 0;
get size(): number {
return this._size;
}
private _loopType: LoopType = LoopType.ITERATIVE;
get loopType(): LoopType {
return this._loopType;
}
private _visitedId: BinaryTreeNodeId[] = [];
get visitedId(): BinaryTreeNodeId[] {
return this._visitedId;
}
private _visitedVal: N['val'][] = [];
get visitedVal(): N['val'][] {
return this._visitedVal;
}
private _visitedNode: N[] = [];
get visitedNode(): N[] {
return this._visitedNode;
}
private _visitedLeftSum: number[] = [];
get visitedLeftSum(): number[] {
return this._visitedLeftSum;
}
abstract createNode(id: BinaryTreeNodeId, val?: N['val']): N | null;
/**
* The `swapLocation` function swaps the location of two nodes in a binary tree.
* @param {N} srcNode - The source node that you want to swap with the destination node.
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
* be swapped to.
* @returns The `destNode` is being returned.
*/
swapLocation(srcNode: N, destNode: N): N {
const {id, val, height} = destNode;
const tempNode = this.createNode(id, val);
if (tempNode) {
tempNode.height = height;
destNode.id = srcNode.id;
destNode.val = srcNode.val;
destNode.height = srcNode.height;
srcNode.id = tempNode.id;
srcNode.val = tempNode.val;
srcNode.height = tempNode.height;
}
return destNode;
}
/**
* The clear() function resets the root, size, and maxId properties to their initial values.
*/
clear() {
this._root = null;
this._size = 0;
this._clearResults();
}
/**
* The function checks if the size of an object is equal to zero and returns a boolean value.
* @returns A boolean value indicating whether the size of the object is 0 or not.
*/
isEmpty(): boolean {
return this.size === 0;
}
/**
* When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
* In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
*/
/**
* The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
* @param {BinaryTreeNodeId | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId`, which
* is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
* node itself. It can also be `null` if no node is specified.
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
* being added to the binary tree.
* @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
*/
add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val']): N | null | undefined {
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
const queue: Array<N | null> = [root];
while (queue.length > 0) {
const cur = queue.shift();
if (cur) {
Iif (newNode && cur.id === newNode.id) return;
const inserted = this._addTo(newNode, cur);
Iif (inserted !== undefined) return inserted;
Iif (cur.left) queue.push(cur.left);
Iif (cur.right) queue.push(cur.right);
} else return;
}
return;
};
let inserted: N | null | undefined, needInsert: N | null;
if (idOrNode === null) {
needInsert = null;
} else if (typeof idOrNode === 'number') {
needInsert = this.createNode(idOrNode, val);
} else if (idOrNode instanceof AbstractBinaryTreeNode) {
needInsert = idOrNode;
} else {
return;
}
const existNode = idOrNode ? this.get(idOrNode, 'id') : undefined;
if (this.root) {
if (existNode) {
existNode.val = val;
inserted = existNode;
} else {
inserted = _bfs(this.root, needInsert);
}
} else {
this._setRoot(needInsert);
if (needInsert !== null) {
this._setSize(1);
} else {
this._setSize(0);
}
inserted = this.root;
}
return inserted;
}
/**
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
* values, and adds them to the binary tree.
* @param {(BinaryTreeNodeId | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeId or BinaryTreeNode
* objects, or null values.
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
* the nodes or node IDs being added. It is used to set the value of each node being added. If `data` is not provided,
* the value of the nodes will be `undefined`.
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
*/
addMany(idsOrNodes: (BinaryTreeNodeId | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[] {
// TODO not sure addMany not be run multi times
const inserted: (N | null | undefined)[] = [];
for (let i = 0; i < idsOrNodes.length; i++) {
const idOrNode = idsOrNodes[i];
Iif (idOrNode instanceof AbstractBinaryTreeNode) {
inserted.push(this.add(idOrNode.id, idOrNode.val));
continue;
}
Iif (idOrNode === null) {
inserted.push(this.add(null));
continue;
}
const val = data?.[i];
inserted.push(this.add(idOrNode, val));
}
return inserted;
}
/**
* The `fill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
* @param {(BinaryTreeNodeId | N)[]} idsOrNodes - The `idsOrNodes` parameter is an array that can contain either
* `BinaryTreeNodeId` or `N` values.
* @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `idsOrNodes`
* array. Each value in the `data` array will be assigned to the
* @returns The method is returning a boolean value.
*/
fill(idsOrNodes: (BinaryTreeNodeId | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean {
this.clear();
return idsOrNodes.length === this.addMany(idsOrNodes, data).length;
}
/**
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent node
* that needs to be balanced.
* @param {N | BinaryTreeNodeId} nodeOrId - The `nodeOrId` parameter can be either a node object (`N`) or a binary tree
* node ID (`BinaryTreeNodeId`).
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
* determines whether to update the left sum of all nodes in the binary tree after removing a node. If
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be updated. If it
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
*/
remove(nodeOrId: N | BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): BinaryTreeDeletedResult<N>[] {
isUpdateAllLeftSum = isUpdateAllLeftSum === undefined ? true : isUpdateAllLeftSum;
// TODO may implement update all left sum
if (isUpdateAllLeftSum) {
}
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
Iif (!this.root) return bstDeletedResult;
const curr: N | null = typeof nodeOrId === 'number' ? this.get(nodeOrId) : nodeOrId;
if (!curr) return bstDeletedResult;
const parent: N | null = curr?.parent ? curr.parent : null;
let needBalanced: N | null = null,
orgCurrent = curr;
if (!curr.left) {
Iif (!parent) {
Iif (curr.right !== undefined) this._setRoot(curr.right);
} else {
const {familyPosition: fp} = curr;
if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
parent.left = curr.right;
} else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
parent.right = curr.right;
}
needBalanced = parent;
}
} else {
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
if (leftSubTreeRightMost) {
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
if (parentOfLeftSubTreeMax) {
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
needBalanced = parentOfLeftSubTreeMax;
}
}
}
this._setSize(this.size - 1);
bstDeletedResult.push({deleted: orgCurrent, needBalanced});
return bstDeletedResult;
}
/**
* The function calculates the depth of a node in a binary tree.
* @param {N | BinaryTreeNodeId | null} beginRoot - The `beginRoot` parameter can be one of the following:
* @returns the depth of the given node or binary tree.
*/
getDepth(beginRoot: N | BinaryTreeNodeId | null): number {
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
let depth = 0;
while (beginRoot?.parent) {
depth++;
beginRoot = beginRoot.parent;
}
return depth;
}
/**
* The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
* @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
* generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
* node), or `null`.
* @returns the height of the binary tree.
*/
getHeight(beginRoot?: N | BinaryTreeNodeId | null): number {
beginRoot = beginRoot ?? this.root;
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
Iif (!beginRoot) return -1;
Iif (this._loopType === LoopType.RECURSIVE) {
const _getMaxHeight = (cur: N | null | undefined): number => {
Iif (!cur) return -1;
const leftHeight = _getMaxHeight(cur.left);
const rightHeight = _getMaxHeight(cur.right);
return Math.max(leftHeight, rightHeight) + 1;
};
return _getMaxHeight(beginRoot);
} else {
Iif (!beginRoot) {
return -1;
}
const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
let maxHeight = 0;
while (stack.length > 0) {
const {node, depth} = stack.pop()!;
if (node.left) {
stack.push({node: node.left, depth: depth + 1});
}
if (node.right) {
stack.push({node: node.right, depth: depth + 1});
}
maxHeight = Math.max(maxHeight, depth);
}
return maxHeight;
}
}
/**
* The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
* approach.
* @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
* represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
* for `beginRoot`, the `this.root` property is used as the default value.
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
*/
getMinHeight(beginRoot?: N | null): number {
beginRoot = beginRoot || this.root;
Iif (!beginRoot) return -1;
Iif (this._loopType === LoopType.RECURSIVE) {
const _getMinHeight = (cur: N | null | undefined): number => {
Iif (!cur) return 0;
Iif (!cur.left && !cur.right) return 0;
const leftMinHeight = _getMinHeight(cur.left);
const rightMinHeight = _getMinHeight(cur.right);
return Math.min(leftMinHeight, rightMinHeight) + 1;
};
return _getMinHeight(beginRoot);
} else {
const stack: N[] = [];
let node: N | null | undefined = beginRoot,
last: N | null = null;
const depths: Map<N, number> = new Map();
while (stack.length > 0 || node) {
if (node) {
stack.push(node);
node = node.left;
} else {
node = stack[stack.length - 1];
if (!node.right || last === node.right) {
node = stack.pop();
if (node) {
const leftMinHeight = node.left ? depths.get(node.left) ?? -1 : -1;
const rightMinHeight = node.right ? depths.get(node.right) ?? -1 : -1;
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
last = node;
node = null;
}
} else node = node.right;
}
}
return depths.get(beginRoot) ?? -1;
}
}
/**
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the height of the
* tree.
* @param {N | null} [beginRoot] - The parameter `beginRoot` is of type `N` or `null`. It represents the root node of a
* tree or null if the tree is empty.
* @returns The method is returning a boolean value.
*/
isPerfectlyBalanced(beginRoot?: N | null): boolean {
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
}
/**
* The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
* generic type `N`. It represents the property of the binary tree node that you want to search for.
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
* specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
* return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
* function will stop traversing the tree and return the first matching node. If `only
* @returns an array of nodes (type N).
*/
getNodes(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): N[] {
Iif (!this.root) return [];
propertyName = propertyName ?? 'id';
const result: N[] = [];
if (this.loopType === LoopType.RECURSIVE) {
const _traverse = (cur: N) => {
Iif (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return;
Iif (!cur.left && !cur.right) return;
cur.left && _traverse(cur.left);
cur.right && _traverse(cur.right);
};
_traverse(this.root);
} else {
const queue: N[] = [this.root];
while (queue.length > 0) {
const cur = queue.shift();
Iif (cur) {
Iif (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return result;
cur.left && queue.push(cur.left);
cur.right && queue.push(cur.right);
}
}
}
return result;
}
/**
* The function checks if a binary tree node has a specific property.
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
* It represents the property of the binary tree node that you want to check.
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'id'.
* @returns a boolean value.
*/
has(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName): boolean {
propertyName = propertyName ?? 'id';
// TODO may support finding node by value equal
return this.getNodes(nodeProperty, propertyName).length > 0;
}
/**
* The function returns the first node that matches the given property name and value, or null if no matching node is
* found.
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
* It represents the property of the binary tree node that you want to search for.
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
* specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
* default value is set to `'id'`.
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
* If no matching node is found, it returns null.
*/
get(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName): N | null {
propertyName = propertyName ?? 'id';
// TODO may support finding node by value equal
return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
}
/**
* The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
* an option to reverse the order of the nodes.
* @param {N} node - The `node` parameter represents a node in a tree structure. It is of type `N`, which could be any
* type that represents a node in your specific implementation.
* @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting
* path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If
* `isReverse` is set to `false` or not provided, the path will
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
*/
getPathToRoot(node: N, isReverse = true): N[] {
// TODO to support get path through passing id
const result: N[] = [];
while (node.parent) {
// Array.push + Array.reverse is more efficient than Array.unshift
// TODO may consider using Deque, so far this is not the performance bottleneck
result.push(node);
node = node.parent;
}
result.push(node);
return isReverse ? result.reverse() : result;
}
/**
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
* no node is specified.
* generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
* node), or `null`.
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
* node is found (
*/
getLeftMost(): N | null;
/**
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
* no node is specified.
* @param {N | BinaryTreeNodeId | null} [node] - The `beginRoot` parameter is optional and can be of type `N` (a
* generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
* node).
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
* node is found (
*/
getLeftMost(node: N): N;
/**
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
* no node is specified.
* @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
* generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
* node), or `null`.
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
* node is found (
*/
getLeftMost(beginRoot?: N | BinaryTreeNodeId | null): N | null {
Iif (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
beginRoot = beginRoot ?? this.root;
Iif (!beginRoot) return beginRoot;
Iif (this._loopType === LoopType.RECURSIVE) {
const _traverse = (cur: N): N => {
Iif (!cur.left) return cur;
return _traverse(cur.left);
};
return _traverse(beginRoot);
} else {
// Indirect implementation of iteration using tail recursion optimization
const _traverse = trampoline((cur: N) => {
if (!cur.left) return cur;
return _traverse.cont(cur.left);
});
return _traverse(beginRoot);
}
}
/**
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
* recursion optimization.
* @returns The `getRightMost` function returns the rightmost node in a binary tree. It returns the
* rightmost node starting from the root of the binary tree.
*/
getRightMost(): N | null;
/**
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
* recursion optimization.
* @param {N | null} [node] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
* starting node from which we want to find the rightmost node. If no node is provided, the function will default to
* using the root node of the data structure.
* @returns The `getRightMost` function returns the rightmost node in a binary tree. It returns the rightmost node
* starting from that node.
*/
getRightMost(node: N): N;
/**
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
* recursion optimization.
* @param {N | null} [node] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
* starting node from which we want to find the rightmost node. If no node is provided, the function will default to
* using the root node of the data structure.
* @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
* it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
* rightmost node starting from the root of the binary tree.
*/
getRightMost(node?: N | null): N | null {
// TODO support get right most by passing id in
node = node ?? this.root;
Iif (!node) return node;
Iif (this._loopType === LoopType.RECURSIVE) {
const _traverse = (cur: N): N => {
Iif (!cur.right) return cur;
return _traverse(cur.right);
};
return _traverse(node);
} else {
// Indirect implementation of iteration using tail recursion optimization
const _traverse = trampoline((cur: N) => {
if (!cur.right) return cur;
return _traverse.cont(cur.right);
});
return _traverse(node);
}
}
/**
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
* @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
* @returns a boolean value.
*/
isSubtreeBST(node: N | null): boolean {
// TODO there is a bug
Iif (!node) return true;
if (this._loopType === LoopType.RECURSIVE) {
const dfs = (cur: N | null | undefined, min: BinaryTreeNodeId, max: BinaryTreeNodeId): boolean => {
Iif (!cur) return true;
Iif (cur.id <= min || cur.id >= max) return false;
return dfs(cur.left, min, cur.id) && dfs(cur.right, cur.id, max);
};
return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
} else {
const stack = [];
let prev = Number.MIN_SAFE_INTEGER,
curr: N | null | undefined = node;
while (curr || stack.length > 0) {
while (curr) {
stack.push(curr);
curr = curr.left;
}
curr = stack.pop()!;
Iif (!curr || prev >= curr.id) return false;
prev = curr.id;
curr = curr.right;
}
return true;
}
}
/**
* The function isBST checks if the binary tree is valid binary search tree.
* @returns The `isBST()` function is returning a boolean value.
*/
isBST(): boolean {
return this.isSubtreeBST(this.root);
}
/**
* The function calculates the size of a subtree by traversing it either recursively or iteratively.
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
* binary tree.
* @returns the size of the subtree rooted at `subTreeRoot`.
*/
getSubTreeSize(subTreeRoot: N | null | undefined) {
// TODO support id passed in
let size = 0;
Iif (!subTreeRoot) return size;
if (this._loopType === LoopType.RECURSIVE) {
const _traverse = (cur: N) => {
size++;
cur.left && _traverse(cur.left);
cur.right && _traverse(cur.right);
};
_traverse(subTreeRoot);
return size;
} else {
const stack: N[] = [subTreeRoot];
while (stack.length > 0) {
const cur = stack.pop()!;
size++;
cur.right && stack.push(cur.right);
cur.left && stack.push(cur.left);
}
return size;
}
}
/**
* The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
* @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
* tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
* @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
* property of the binary tree node to use for calculating the sum. It can be either 'id' or 'val'. If propertyName is
* not provided, it defaults to 'id'.
* @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
*/
subTreeSum(subTreeRoot: N | BinaryTreeNodeId | null, propertyName?: BinaryTreeNodePropertyName): number {
propertyName = propertyName ?? 'id';
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
Iif (!subTreeRoot) return 0;
let sum = 0;
const _sumByProperty = (cur: N) => {
let needSum: number;
switch (propertyName) {
case 'id':
needSum = cur.id;
break;
case 'val':
needSum = typeof cur.val === 'number' ? cur.val : 0;
break;
default:
needSum = cur.id;
break;
}
return needSum;
};
Iif (this._loopType === LoopType.RECURSIVE) {
const _traverse = (cur: N): void => {
sum += _sumByProperty(cur);
cur.left && _traverse(cur.left);
cur.right && _traverse(cur.right);
};
_traverse(subTreeRoot);
} else {
const stack: N[] = [subTreeRoot];
while (stack.length > 0) {
const cur = stack.pop()!;
sum += _sumByProperty(cur);
cur.right && stack.push(cur.right);
cur.left && stack.push(cur.left);
}
}
return sum;
}
/**
* The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
* @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
* each node in the subtree should be incremented.
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'id'.
* @returns a boolean value.
*/
subTreeAdd(
subTreeRoot: N | BinaryTreeNodeId | null,
delta: number,
propertyName?: BinaryTreeNodePropertyName
): boolean {
propertyName = propertyName ?? 'id';
Iif (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
Iif (!subTreeRoot) return false;
const _addByProperty = (cur: N) => {
switch (propertyName) {
case 'id':
cur.id += delta;
break;
default:
cur.id += delta;
break;
}
};
if (this._loopType === LoopType.RECURSIVE) {
const _traverse = (cur: N) => {
_addByProperty(cur);
cur.left && _traverse(cur.left);
cur.right && _traverse(cur.right);
};
_traverse(subTreeRoot);
} else {
const stack: N[] = [subTreeRoot];
while (stack.length > 0) {
const cur = stack.pop()!;
_addByProperty(cur);
cur.right && stack.push(cur.right);
cur.left && stack.push(cur.left);
}
}
return true;
}
/**
* Performs a breadth-first search (BFS) on a binary tree, accumulating properties of each node based on their 'id' property.
* @returns An array of binary tree node IDs.
*/
BFS(): BinaryTreeNodeId[];
/**
* Performs a breadth-first search (BFS) on a binary tree, accumulating properties of each node based on the specified property name.
* @param {'id'} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of values corresponding to the specified property.
*/
BFS(nodeOrPropertyName: 'id'): BinaryTreeNodeId[];
/**
* Performs a breadth-first search (BFS) on a binary tree, accumulating the 'val' property of each node.
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of 'val' properties from each node.
*/
BFS(nodeOrPropertyName: 'val'): N['val'][];
/**
* Performs a breadth-first search (BFS) on a binary tree, accumulating nodes themselves.
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of binary tree nodes.
*/
BFS(nodeOrPropertyName: 'node'): N[];
/**
* The BFS function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
* @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
* If a node is provided, the BFS algorithm will be performed starting from that node.
* If a property name is provided, the BFS algorithm will be performed starting from the root node, accumulating the specified property.
* @returns An instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
*/
BFS(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
nodeOrPropertyName = nodeOrPropertyName ?? 'id';
this._clearResults();
const queue: Array<N | null | undefined> = [this.root];
while (queue.length !== 0) {
const cur = queue.shift();
if (cur) {
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
if (cur?.left !== null) queue.push(cur.left);
if (cur?.right !== null) queue.push(cur.right);
}
}
return this._getResultByPropertyName(nodeOrPropertyName);
}
/**
* Performs a depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on their 'id' property.
* @returns An array of binary tree node IDs.
*/
DFS(): BinaryTreeNodeId[];
/**
* Performs a depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on the specified property name.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of values corresponding to the specified property.
*/
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
/**
* Performs a depth-first search (DFS) traversal on a binary tree and accumulates the 'val' property of each node.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of 'val' properties from each node.
*/
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
/**
* Performs a depth-first search (DFS) traversal on a binary tree and accumulates nodes themselves.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of binary tree nodes.
*/
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
/**
* The DFS function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
* each node based on the specified pattern and property name.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'id'`.
* @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
*/
DFS(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
pattern = pattern ?? 'in';
nodeOrPropertyName = nodeOrPropertyName ?? 'id';
this._clearResults();
const _traverse = (node: N) => {
switch (pattern) {
case 'in':
if (node.left) _traverse(node.left);
this._accumulatedByPropertyName(node, nodeOrPropertyName);
if (node.right) _traverse(node.right);
break;
case 'pre':
this._accumulatedByPropertyName(node, nodeOrPropertyName);
Iif (node.left) _traverse(node.left);
Iif (node.right) _traverse(node.right);
break;
case 'post':
Iif (node.left) _traverse(node.left);
Iif (node.right) _traverse(node.right);
this._accumulatedByPropertyName(node, nodeOrPropertyName);
break;
}
};
this.root && _traverse(this.root);
return this._getResultByPropertyName(nodeOrPropertyName);
}
// --- start additional methods ---
/**
* Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on their 'id' property.
* @returns An array of binary tree node IDs.
*/
DFSIterative(): BinaryTreeNodeId[];
/**
* Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on the specified property name.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of values corresponding to the specified property.
*/
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
/**
* Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates the 'val' property of each node.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of 'val' properties from each node.
*/
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
/**
* Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates nodes themselves.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of binary tree nodes.
*/
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
/**
* The DFSIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
* specify the traversal pattern and the property name to accumulate results by.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'id'`.
* @returns An object of type AbstractBinaryTreeNodeProperties<N>.
*/
DFSIterative(
pattern?: 'in' | 'pre' | 'post',
nodeOrPropertyName?: NodeOrPropertyName
): AbstractBinaryTreeNodeProperties<N> {
pattern = pattern || 'in';
nodeOrPropertyName = nodeOrPropertyName || 'id';
this._clearResults();
Iif (!this.root) return this._getResultByPropertyName(nodeOrPropertyName);
// 0: visit, 1: print
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: this.root}];
while (stack.length > 0) {
const cur = stack.pop();
Iif (!cur || !cur.node) continue;
if (cur.opt === 1) {
this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
} else {
switch (pattern) {
case 'in':
stack.push({opt: 0, node: cur.node.right});
stack.push({opt: 1, node: cur.node});
stack.push({opt: 0, node: cur.node.left});
break;
case 'pre':
stack.push({opt: 0, node: cur.node.right});
stack.push({opt: 0, node: cur.node.left});
stack.push({opt: 1, node: cur.node});
break;
case 'post':
stack.push({opt: 1, node: cur.node});
stack.push({opt: 0, node: cur.node.right});
stack.push({opt: 0, node: cur.node.left});
break;
default:
stack.push({opt: 0, node: cur.node.right});
stack.push({opt: 1, node: cur.node});
stack.push({opt: 0, node: cur.node.left});
break;
}
}
}
return this._getResultByPropertyName(nodeOrPropertyName);
}
/**
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'id' property.
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
* @returns An array of binary tree node IDs.
*/
levelIterative(node: N | null): BinaryTreeNodeId[];
/**
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on the specified property name.
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of values corresponding to the specified property.
*/
levelIterative(node: N | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
/**
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates the 'val' property of each node.
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of 'val' properties from each node.
*/
levelIterative(node: N | null, nodeOrPropertyName?: 'val'): N['val'][];
/**
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates nodes themselves.
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of binary tree nodes.
*/
levelIterative(node: N | null, nodeOrPropertyName?: 'node'): N[];
/**
* The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
* in an array, based on a specified property name.
* @param {N | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
* node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
* the tree is used as the starting node.
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
* can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
* will accumulate results based on that property. If no property name is provided, the function will default to
* accumulating results based on the 'id' property.
* @returns An object of type `AbstractBinaryTreeNodeProperties<N>`.
*/
levelIterative(node: N | null, nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
nodeOrPropertyName = nodeOrPropertyName || 'id';
node = node || this.root;
Iif (!node) return [];
this._clearResults();
const queue: N[] = [node];
while (queue.length > 0) {
const cur = queue.shift();
Iif (cur) {
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
Iif (cur.left) {
queue.push(cur.left);
}
Iif (cur.right) {
queue.push(cur.right);
}
}
}
return this._getResultByPropertyName(nodeOrPropertyName);
}
/**
* Collects nodes from a binary tree by a specified property and organizes them into levels.
* @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
* @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
*/
listLevels(node: N | null): BinaryTreeNodeId[][];
/**
* Collects nodes from a binary tree by a specified property and organizes them into levels.
* @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
* @param {'id} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
* @returns A 2D array of values corresponding to the specified property.
*/
listLevels(node: N | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[][];
/**
* Collects nodes from a binary tree by a specified property and organizes them into levels.
* @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
* @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
* @returns A 2D array of 'val' properties from each node.
*/
listLevels(node: N | null, nodeOrPropertyName?: 'val'): N['val'][][];
/**
* Collects nodes from a binary tree by a specified property and organizes them into levels.
* @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
* @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
* @returns A 2D array of binary tree nodes.
*/
listLevels(node: N | null, nodeOrPropertyName?: 'node'): N[][];
/**
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
* @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following values: 'id', 'val', or 'node'. If not provided, it defaults to 'id'.
* @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
*/
listLevels(node: N | null, nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperty<N>[][] {
nodeOrPropertyName = nodeOrPropertyName || 'id';
node = node || this.root;
Iif (!node) return [];
const levelsNodes: AbstractBinaryTreeNodeProperty<N>[][] = [];
const collectByProperty = (node: N, level: number) => {
switch (nodeOrPropertyName) {
case 'id':
levelsNodes[level].push(node.id);
break;
case 'val':
levelsNodes[level].push(node.val);
break;
case 'node':
levelsNodes[level].push(node);
break;
default:
levelsNodes[level].push(node.id);
break;
}
};
if (this.loopType === LoopType.RECURSIVE) {
const _recursive = (node: N, level: number) => {
Iif (!levelsNodes[level]) levelsNodes[level] = [];
collectByProperty(node, level);
Iif (node.left) _recursive(node.left, level + 1);
Iif (node.right) _recursive(node.right, level + 1);
};
_recursive(node, 0);
} else {
const stack: [N, number][] = [[node, 0]];
while (stack.length > 0) {
const head = stack.pop()!;
const [node, level] = head;
Iif (!levelsNodes[level]) levelsNodes[level] = [];
collectByProperty(node, level);
Iif (node.right) stack.push([node.right, level + 1]);
Iif (node.left) stack.push([node.left, level + 1]);
}
}
return levelsNodes;
}
/**
* The function returns the predecessor of a given node in a binary tree.
* @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
* @returns the predecessor of the given node in a binary tree.
*/
getPredecessor(node: N): N {
if (node.left) {
let predecessor: N | null | undefined = node.left;
while (!predecessor || (predecessor.right && predecessor.right !== node)) {
Iif (predecessor) {
predecessor = predecessor.right;
}
}
return predecessor;
} else {
return node;
}
}
/**
* Time complexity is O(n)
* Space complexity of Iterative DFS equals to recursive DFS which is O(n) because of the stack
*/
/**
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
* @returns An array of binary tree node IDs.
*/
morris(): BinaryTreeNodeId[];
/**
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates properties of each node based on the specified property name.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {'id'} nodeOrPropertyName - The name of the property to accumulate.
* @returns An array of values corresponding to the specified property.
*/
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
/**
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates the 'val' property of each node.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
* @returns An array of 'val' properties from each node.
*/
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
/**
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates nodes themselves.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
* @returns An array of binary tree nodes.
*/
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
/**
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to 'id'.
* @returns An array of AbstractBinaryTreeNodeProperties<N> objects.
*/
morris(
pattern?: 'in' | 'pre' | 'post',
nodeOrPropertyName?: NodeOrPropertyName
): AbstractBinaryTreeNodeProperties<N> {
Iif (this.root === null) return [];
pattern = pattern || 'in';
nodeOrPropertyName = nodeOrPropertyName || 'id';
this._clearResults();
let cur: N | null | undefined = this.root;
const _reverseEdge = (node: N | null | undefined) => {
let pre: N | null | undefined = null;
let next: N | null | undefined = null;
while (node) {
next = node.right;
node.right = pre;
pre = node;
node = next;
}
return pre;
};
const _printEdge = (node: N | null) => {
const tail: N | null | undefined = _reverseEdge(node);
let cur: N | null | undefined = tail;
while (cur) {
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
cur = cur.right;
}
_reverseEdge(tail);
};
switch (pattern) {
case 'in':
while (cur) {
Iif (cur.left) {
const predecessor = this.getPredecessor(cur);
if (!predecessor.right) {
predecessor.right = cur;
cur = cur.left;
continue;
} else {
predecessor.right = null;
}
}
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
cur = cur.right;
}
break;
case 'pre':
while (cur) {
if (cur.left) {
const predecessor = this.getPredecessor(cur);
if (!predecessor.right) {
predecessor.right = cur;
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
cur = cur.left;
continue;
} else {
predecessor.right = null;
}
} else {
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
}
cur = cur.right;
}
break;
case 'post':
while (cur) {
Iif (cur.left) {
const predecessor = this.getPredecessor(cur);
if (predecessor.right === null) {
predecessor.right = cur;
cur = cur.left;
continue;
} else {
predecessor.right = null;
_printEdge(cur.left);
}
}
cur = cur.right;
}
_printEdge(this.root);
break;
}
return this._getResultByPropertyName(nodeOrPropertyName);
}
/**
* The function adds a new node to a binary tree if there is an available position.
* @param {N | null} newNode - The `newNode` parameter is of type `N | null`, which means it can either be a node of
* type `N` or `null`. It represents the node that you want to add to the binary tree.
* @param {N} parent - The parent parameter is of type N, which represents a node in a binary tree.
* @returns either the left or right child node of the parent node, depending on which child is available for adding
* the new node. If a new node is added, the function also updates the size of the binary tree. If neither the left nor
* right child is available, the function returns undefined. If the parent node is null, the function also returns
* undefined.
*/
protected _addTo(newNode: N | null, parent: N): N | null | undefined {
if (parent) {
// When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
// In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
if (parent.left === undefined) {
parent.left = newNode;
Iif (newNode) {
this._setSize(this.size + 1);
}
return parent.left;
} else if (parent.right === undefined) {
parent.right = newNode;
Iif (newNode) {
this._setSize(this.size + 1);
}
return parent.right;
} else {
return;
}
} else {
return;
}
}
/**
* The function sets the loop type for a protected variable.
* @param {LoopType} value - The value parameter is of type LoopType.
*/
protected _setLoopType(value: LoopType) {
this._loopType = value;
}
/**
* The function sets the value of the `_visitedId` property in a protected manner.
* @param {BinaryTreeNodeId[]} value - value is an array of BinaryTreeNodeId values.
*/
protected _setVisitedId(value: BinaryTreeNodeId[]) {
this._visitedId = value;
}
/**
* The function sets the value of the "_visitedVal" property to the given array.
* @param value - An array of type N.
*/
protected _setVisitedVal(value: Array<N>) {
this._visitedVal = value;
}
/**
* The function sets the value of the _visitedNode property.
* @param {N[]} value - N[] is an array of elements of type N.
*/
protected _setVisitedNode(value: N[]) {
this._visitedNode = value;
}
/**
* The function sets the value of the `_visitedLeftSum` property to the provided array.
* @param {number[]} value - An array of numbers that represents the visited left sum.
*/
protected _setVisitedLeftSum(value: number[]) {
this._visitedLeftSum = value;
}
/**
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
* parent property of the value to undefined.
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of type `N` or `null`.
*/
protected _setRoot(v: N | null) {
if (v) {
v.parent = undefined;
}
this._root = v;
}
/**
* The function sets the size of a protected variable.
* @param {number} v - number
*/
protected _setSize(v: number) {
this._size = v;
}
/**
* The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their
* properties.
*/
protected _clearResults() {
this._visitedId = [];
this._visitedVal = [];
this._visitedNode = [];
this._visitedLeftSum = [];
}
/**
* The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
* a result array.
* @param {N} cur - The current node being processed.
* @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeId` or a `N`
* type. It represents the property value that we are comparing against in the switch statement.
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
* specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'id'`
* or `'val'`. If it is not provided or is not equal to `'id'` or `'val'`, the
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
* stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
* `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
* @returns a boolean value indicating whether only one matching node should be pushed into the result array.
*/
protected _pushByPropertyNameStopOrNot(
cur: N,
result: (N | null | undefined)[],
nodeProperty: BinaryTreeNodeId | N,
propertyName?: BinaryTreeNodePropertyName,
onlyOne?: boolean
) {
switch (propertyName) {
case 'id':
if (cur.id === nodeProperty) {
result.push(cur);
return !!onlyOne;
}
break;
case 'val':
if (cur.val === nodeProperty) {
result.push(cur);
return !!onlyOne;
}
break;
default:
Iif (cur.id === nodeProperty) {
result.push(cur);
return !!onlyOne;
}
break;
}
}
/**
* The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name.
* @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure.
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
* can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
* specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
*/
protected _accumulatedByPropertyName(node: N, nodeOrPropertyName?: NodeOrPropertyName) {
nodeOrPropertyName = nodeOrPropertyName ?? 'id';
switch (nodeOrPropertyName) {
case 'id':
this._visitedId.push(node.id);
break;
case 'val':
this._visitedVal.push(node.val);
break;
case 'node':
this._visitedNode.push(node);
break;
default:
this._visitedId.push(node.id);
break;
}
}
/**
* The time complexity of Morris traversal is O(n), it's may slower than others
* The space complexity Morris traversal is O(1) because no using stack
*/
/**
* The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property
* name.
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
* can accept either a `NodeOrPropertyName` type or be undefined.
* @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
*/
protected _getResultByPropertyName(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
nodeOrPropertyName = nodeOrPropertyName ?? 'id';
switch (nodeOrPropertyName) {
case 'id':
return this._visitedId;
case 'val':
return this._visitedVal;
case 'node':
return this._visitedNode;
default:
return this._visitedId;
}
}
// --- end additional methods ---
}
|