Giter Site home page Giter Site logo

ascii-binary-tree's Introduction

ascii-binary-tree

Print simple ascii binary trees in JavasScript

Example

Install

npm install ascii-binary-tree

Examples

Constructing from nodes

const { Node } = require('ascii-binary-tree')

// layer 1
const root = Node.from('A')

// layer 2
root.left = Node.from('B')
root.right = Node.from('C')

// layer 3
root.left.left = Node.from('D')
root.left.right = Node.from('E')
root.right.left = Node.from('F')
root.right.right = Node.from('G')

// layer 4
root.left.left.left = Node.from('H')
root.left.left.right = Node.from('I')
root.left.right.left = Node.from('J')
root.left.right.right = Node.from('K')
root.right.left.left = Node.from('L')
root.right.left.right = Node.from('M')
root.right.right.left = Node.from('N')
root.right.right.right = Node.from('O')

// layer 5
root.left.left.left.left = Node.from('P')
root.left.left.left.right = Node.from('Q')
root.left.left.right.left = Node.from('R')
root.left.left.right.right = Node.from('S')
root.left.right.left.left = Node.from('T')
root.left.right.left.right = Node.from('U')
root.left.right.right.left = Node.from('V')
root.left.right.right.right = Node.from('W')
root.right.left.left.left = Node.from('X')
root.right.left.left.right = Node.from('Y')
root.right.left.right.left = Node.from('Z')

root.print()

Output

                  ______________A______________
                 /                             \
          ______B______                   ______C______
         /             \                 /             \
      __D__           __E__           __F__           __G__
     /     \         /     \         /     \         /     \
    H       I       J       K       L       M       N       O
   / \     / \     / \     / \     / \     /
  P   Q   R   S   T   U   V   W   X   Y   Z

Constructing from tree object

const { Tree } = require('ascii-binary-tree')
const tree = {
  A: {
    B: {
      D: {
        H: {
          P: true,
          Q: true
        },
        I: {
          R: true,
          S: true
        }
      },
      E: {
        J: {
          T: true,
          U: true
        },
        K: {
          V: true,
          W: true
        }
      }
    },
    C: {
      F: {
        L: {
          X: true,
          Y: true
        },
        M: {
          Z: true
        }
      },
      G: {
        N: true,
        O: true
      }
    }
  }
}

const root = Tree.from(tree)
root.print()

Output

                  ______________A______________
                 /                             \
          ______B______                   ______C______
         /             \                 /             \
      __D__           __E__           __F__           __G__
     /     \         /     \         /     \         /     \
    H       I       J       K       L       M       N       O
   / \     / \     / \     / \     / \     /
  P   Q   R   S   T   U   V   W   X   Y   Z

Get ascii string

const root = Tree.from({ ... })
const output = root.render()
console.log(output)

License

MIT

ascii-binary-tree's People

Contributors

miguelmota avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

moriarty47 gsej

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.