docs: Complete the documentation for the print method in BinaryTree. Correct the unit tests for the print method.

This commit is contained in:
Revone 2024-10-31 16:44:01 +13:00
parent 9ace395564
commit 6ef4b47b0b
2 changed files with 31 additions and 32 deletions

View file

@ -1703,25 +1703,25 @@ export class BinaryTree<
return output;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The function `print` in TypeScript overrides the default print behavior to log a visual
* representation of the binary tree to the console.
* @param {BinaryTreePrintOptions} [options] - The `options` parameter is used to specify the
* printing options for the binary tree. It is an optional parameter that allows you to customize how
* the binary tree is printed, such as choosing between different traversal orders or formatting
* options.
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
* `override print` method is used to specify the starting point for printing the binary tree. It can
* be either a key, a node, an entry, or the root of the tree. If no specific starting point is
* provided, the default value is set to
*/
override print(options?: BinaryTreePrintOptions, beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root) {
console.log(this.toVisual(beginRoot, options));
}
protected _dfs<C extends BTNCallback<NODE>>(
callback?: C,
pattern?: DFSOrderPattern,
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
iterationType?: IterationType
): ReturnType<C>[];
protected _dfs<C extends BTNCallback<NODE | null>>(
callback?: C,
pattern?: DFSOrderPattern,
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
iterationType?: IterationType,
includeNull?: boolean
): ReturnType<C>[];
/**
* Time complexity: O(n)
* Space complexity: O(n)

View file

@ -319,27 +319,26 @@ describe('BinaryTree', () => {
it('should isSubtreeBST', () => {
expect(tree.toVisual()).toBe('');
tree.addMany([4, 2, 6, 1, 3, 5, 7, 4]);
expect(tree.toVisual()).toBe(
expect(tree.toVisual()).toBe('N for null\n' +
' ___4___ \n' +
' / \\ \n' +
' _2_ _6_ \n' +
' / \\ / \\ \n' +
' 1 3 5 7 \n' +
' \n'
);
' / \\ \n' +
' _2_ _6_ \n' +
' / \\ / \\ \n' +
' 1 3 5 7 \n' +
' \n');
const visualized = tree.toVisual(undefined, { isShowUndefined: true, isShowNull: true, isShowRedBlackNIL: true });
expect(visualized).toBe(
'U for undefined\n' +
' N for null\n' +
' S for Sentinel Node(NIL)\n' +
' _______4_______ \n' +
' / \\ \n' +
' ___2___ ___6___ \n' +
' / \\ / \\ \n' +
' _1_ _3_ _5_ _7_ \n' +
' / \\ / \\ / \\ / \\ \n' +
' U U U U U U U U \n' +
' \n'
'N for null\n' +
'S for Sentinel Node(NIL)\n' +
' _______4_______ \n' +
' / \\ \n' +
' ___2___ ___6___ \n' +
' / \\ / \\ \n' +
' _1_ _3_ _5_ _7_ \n' +
' / \\ / \\ / \\ / \\ \n' +
' U U U U U U U U \n' +
' \n'
);
expect(tree.isBST(tree.getNode(4), 'RECURSIVE')).toBe(true);