0.0.5 • Published 2 months ago

@n3bula/bin-tree v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@n3bula/bin-tree

A tool used for printing a binary tree structure in a visualizing way.

Install

npm install @n3bula/bin-tree
# or
yarn add @n3bula/bin-tree
# or
pnpm add @n3bula/bin-tree

Use

ESModule / CommonJS

import BinTree from '@n3bula/bin-tree';
// const BinTree = require('@n3bula/bin-tree');

console.log(
  BinTree.print(
    {
      value: 1,
      left: null,
      right: {
        value: 2,
        left: {
          value: 3,
          left: null,
          right: null,
        },
        right: {
          value: 4,
          left: null,
          right: null,
        },
      },
    },
    {
      minLength: 5,
      marks: {
        dash: '-',
        lb: '^',
        joint: '^',
      },
    },
  ),
);
console.log(
  BinTree.print({
    value: 1,
    left: {
      value: 2,
      left: {
        value: 4,
        left: {
          value: 8,
          left: null,
          right: null,
        },
        right: {
          value: 9,
          left: null,
          right: null,
        },
      },
      right: {
        value: 5,
        left: {
          value: 10,
          left: null,
          right: null,
        },
        right: {
          value: 11,
          left: null,
          right: null,
        },
      },
    },
    right: {
      value: 3,
      left: {
        value: 6,
        left: {
          value: 12,
          left: null,
          right: null,
        },
        right: {
          value: 13,
          left: null,
          right: null,
        },
      },
      right: {
        value: 7,
        left: {
          value: 14,
          left: null,
          right: null,
        },
        right: {
          value: 15,
          left: null,
          right: null,
        },
      },
    },
  }),
);

result

import BinTree from '@n3bula/bin-tree';
// const BinTree = require('@n3bula/bin-tree');

const tree = BinTree.create([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
console.log(tree);
// TreeNode {value: 1, left: TreeNode, right: TreeNode}
console.log(tree.preorderTraversal());
// [1, 2, 4, 8, 9, 5, 10, 11, 3, 6, 12, 13, 7, 14, 15]
console.log(tree.inorderTraversal());
// [8, 4, 9, 2, 10, 5, 11, 1, 12, 6, 13, 3, 14, 7, 15]
console.log(tree.left.postorderTraversal());
// [8, 9, 4, 10, 11, 5, 2]
console.log(tree.levelorderTraversal());
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
console.log(tree.getDepth()); // 4
console.log(tree.print());
console.log(tree.left.print());
console.log(tree.left.left.print());
console.log(tree.left.left.left.print());

result

UMD

<body>
  <script src="../bin-tree/bundle.min.js"></script>
  <script>
    console.log(
      BinTree.print(
        {
          value: 1,
          left: null,
          right: {
            value: '😀',
            left: {
              value: 3,
              left: null,
              right: null,
            },
            right: {
              value: 4,
              left: null,
              right: null,
            },
          },
        },
        {
          minLength: 5,
          marks: {
            dash: '-',
            lb: '^',
            joint: '^',
          },
        },
      ),
    );
  </script>
</body>

Options

optionvaluedescription
minLengthnumberpadding length (default: 2)
marksobjecttree branches placeholder
marks.dashstring(default: '━')
marks.ltstring(default: '┏')
marks.rtstring(default: '┓')
marks.lbstring(default: '┗')
marks.rbstring(default: '┛')
marks.jointstring(default: '┻')

Author

License

The MIT License(MIT)

0.0.5

2 months ago

0.0.5-alpha.1

8 months ago

0.0.4

8 months ago

0.0.3

9 months ago

0.0.2

9 months ago