2.2.1 • Published 4 years ago

print-trees v2.2.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

print-tree

version license downloads

In terminal print tree data structure

在学习数据结构中,二叉树显然是个非常重要的知识点,为了更方便的查看一个二叉树的结构,所以基于node-canvasterminal-image绘制出可视化的二叉树到控制台中,便于学习理解二叉树的一些算法。

使用

yarn add print-trees # or npm install print-trees

Doc

import { BinaryTreeInfo } = 'print-trees'
BinaryTreeInfo.print(tree, options)

// tree 实例需要实现该 interface
interface IBinaryTreeInfo {
  getRoot(): Node // 返回树的根节点
  getLeft(node): Node // 返回左节点
  getRight(node): Node // 返回右节点
  getString(node): string // 节点要展示的内容
}

interface IOptions {
  canvasWidth: number
  canvasHeight: number
  output: ['log', 'image'] // log: 打印到控制台 or image: 输出一个binaryInfo.png文件
  terminalImageOptions: object // 参考 terminal-image 库
}

Example

const { BinaryTreeInfo } = require('../')

class BinarySearchTree {
  constructor(compare) {
    this.size = 0
    this.compare = compare
    this.root = null
  }
  isEmpty() {
    return this.size === 0
  }
  getSize() {
    return this.size
  }
  add(e) {
    if (this.isEmpty()) {
      this.root = new BinarySearchTreeNode(e)
      this.size ++
    } else {
      let node = this.root
      let parent = null
      let compareRes
      while (node !== null) {
        compareRes = this.compare(node.element, e)
        parent = node
        if (compareRes > 0) {
          node = node.left
        } else if (compareRes < 0) {
          node = node.right
        } else {
          // 值相同 直接返回
          return
        }
      }
      const newNode = new BinarySearchTreeNode(e, null, null, parent)
      if (compareRes > 0) {
        parent.left = newNode
      } else {
        parent.right = newNode
      }
      this.size ++
    }
  }

  // print
  getRoot() {
    return this.root
  }
  getLeft(node) {
    return node.left
  }
  getRight(node) {
    return node.right
  }
  getString(node) {
    return node.element.toString()
  }
}

class BinarySearchTreeNode {
  constructor(
    element, 
    left = null,
    right = null,
    parent = null
  ) {
    this.element = element
    this.left = left
    this.right = right
    this.parent = parent
  }
}

const tree = new BinarySearchTree((a, b) => a - b)
for (let i = 0; i < 10; i ++) {
  tree.add(~~(Math.random() * 100))
}
BinaryTreeInfo.print(tree, {
  canvasWidth: 1600,
  canvasHeight: 400,
  output: 'image'
})

运行效果

image.png

2.2.1

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago