Skip to content

Commit

Permalink
docs: Complete the documentation for the print method in BinaryTree. …
Browse files Browse the repository at this point in the history
…Correct the unit tests for the print method.
  • Loading branch information
zrwusa committed Oct 31, 2024
1 parent 9ace395 commit 6ef4b47
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
30 changes: 15 additions & 15 deletions src/data-structures/binary-tree/binary-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 16 additions & 17 deletions test/unit/data-structures/binary-tree/binary-tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6ef4b47

Please sign in to comment.