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

binary-tree-visualizer

v2.1.5

Published

A binary tree visualizer

Downloads

511

Readme

Binary Tree Visualizer Build Publish

Binary tree

This is a visualizer for binary trees.

Use the BinaryTreeNode and BinarySearchTreeNode classes provided in the library to create a binary tree or extend it to create a different type of binary tree. To visualize it just pass the root node and the html canvas element to the drawBinaryTree function.

Installation

Via NPM

npm i binary-tree-visualizer

Usage

Default

<style>
  body, html {
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
  }
</style>

<body>
  <canvas></canvas>
</body>
import { BinaryTreeNode, drawBinaryTree } from 'binary-tree-visualizer';

// Init a new root binary tree node
const root = new BinaryTreeNode(100);

// Setting the left child
root.setLeft(new BinaryTreeNode(50));

// Setting the left > left child
root.left.setLeft(new BinaryTreeNode(30));

// Setting the right child
root.setRight(new BinaryTreeNode(145));

// Draw a binary tree with all default config
// Canvas will take up entire height and width of the screen
drawBinaryTree(root, document.querySelector('canvas'));

Options

drawBinaryTree can take a 3rd options argument

import { VisualizationType } from 'binary-tree-visualizer';

type options = {
  // SIMPLE: Taken by default. It assumes that max number of leaf nodes are present and decides the spacing accordingly.
  // PRETTY: Spacing is dynamic according to the nodes.
  // HIGHLIGHT: This is the same as PRETTY. Only difference is that the nodes expand when they are hovered over.
  // EXPANDABLE: Only one child can be viewed at a time.
  type?:  VisualizationType.SIMPLE |  VisualizationType.EXPANDABLE | VisualizationType.PRETTY | VisualizationType.HIGHLIGHT,
  // SUGGESTION: Max height of the canvas. If required more will be taken
  maxHeight?: number,
  // SUGGESTION: Max width of the canvas. If required more will be taken
  maxWidth?: number
};

Simple

Simple

Pretty

Pretty

Expandable

Expandable

Theme

Theme can be tweaked as well

import { setTheme } from 'binary-tree-visualizer';

setTheme(options);
type options = {
  // Radius of nodes (DEFAULT 20)
  radius?: number,
  // By how many times radius can grow or shrink (DEFAULT 1.25)
  growthAndShrinkTimes: number,
  // Minimum leaf node spacing (DEFAULT 60)
  leafNodeSpace?: number,
  // Minimum vertical spacing (DEFAULT 90)
  lineHeight?: number,
  // The font size of the value shown inside the node
  fontSize: number,
  // Font family shown on canvas (DEFAULT 'Poppins') (import required)
  textFont?: string,
  // The color of lines/strokes (DEFAULT '#f56042')
  strokeColor?: string,
  // A random color that is selected for each node circle
  // (DEFAULT [{bgColor: '#fff2e0', borderColor: '#f56042'}])
  colorArray?: {
    borderColor: string
    bgColor: string
  }[],
};

Binary Search Tree

A Binary search tree implementation is also given out of the box

import { BinarySearchTreeNode, drawBinaryTree } from 'binary-tree-visualizer';

const root = new BinarySearchTreeNode(100);
[50, 145, 150, 130, 120, 140, 30, 70, 75].forEach((num) => root.insert(num));

drawBinaryTree(root, document.querySelector('canvas'));

Demo

  • Code sandbox: https://codesandbox.io/s/binary-tree-visualizer-coqx6?file=/src/index.ts
  • Example: https://github.com/tirthamouli/example-binary-tree-visualizer
  • Video documentation: https://youtube.com/playlist?list=PLDKunvJxaiVWxG4Ow5l8g6aHkqC9ZYy8x