npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

print-trees

v2.2.1

Published

![version](https://img.shields.io/npm/v/print-trees?label=print-trees) ![license](https://img.shields.io/npm/l/print-trees) ![downloads](https://img.shields.io/npm/dw/print-trees)

Downloads

4

Readme

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