Binary Tree Visualizer


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';
const root = new BinaryTreeNode(100);
root.setLeft(new BinaryTreeNode(50));
root.left.setLeft(new BinaryTreeNode(30));
root.setRight(new BinaryTreeNode(145));
drawBinaryTree(root, document.querySelector('canvas'));
Options
drawBinaryTree can take a 3rd options argument
import { VisualizationType } from 'binary-tree-visualizer';
type options = {
type?: VisualizationType.SIMPLE | VisualizationType.EXPANDABLE | VisualizationType.PRETTY | VisualizationType.HIGHLIGHT,
maxHeight?: number,
maxWidth?: number
};
Simple

Pretty

Expandable

Theme
Theme can be tweaked as well
import { setTheme } from 'binary-tree-visualizer';
setTheme(options);
type options = {
radius?: number,
growthAndShrinkTimes: number,
leafNodeSpace?: number,
lineHeight?: number,
fontSize: number,
textFont?: string,
strokeColor?: string,
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