1.0.4 ā€¢ Published 2 years ago

tree-visualization v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Binary Tree

About

A Reactive Binary tree visualizer that can visualize a Binary search tree, the library has the Binary search tree data structure out of the box, it can be used and the binaryTreeDrawer function can draw it via d3. The library supports a lot of options such as animation and styling via CSS classes, click nodes, as well as zoom the tree in and out, and many more ...

You are welcome to report bugs, Features or create pull requests.

Documentation

Installation

# npm
npm i tree-visualization

Qiuck Usage

The HTML where the Binary Search tree will be rendered

<!DOCTYPE html>
<html lang="en">
  <body>
    <div id="binarySearchTree"></div>
    // Import the Typescript/Javascript code. For this example, it is located in the index.ts file.
    <script type="module" src="./index.ts"></script>
  </body>
</html>

Create BinarySearchTree data Structure and then draw it via draw() function.

import { BinaryTree, binaryTreeDrawer } from "tree-visualization";

const bst: BinaryTree<number> = new BinaryTree<number>(100);
bst.addNode(51);
bst.addNode(150);
bst.addNode(12);
bst.addNode(152);
bst.addNode(2);
bst.addNode(144);
bst.addNode(12);
bst.addNode(61);
bst.addNode(62);
bst.addNode(63);
bst.addNode(234);
bst.addNode(22);
bst.addNode(123);
bst.addNode(122);
bst.addNode(125);
bst.addNode(57);
bst.addNode(233);
bst.addNode(235);
bst.addNode(149);
// Draw the Binary Search Tree by giving the draw function the Element Id where 
// the Binary Search tree will render and then the created Binary Search data structure.
binaryTreeDrawer().draw("#binarySearchTree", bst);

Usage With More Options

// Example for testing 
import { BinaryTree, binaryTreeDrawer } from "tree-visualization";

const bst: BinaryTree<number> = new BinaryTree<number>(100);
bst.addNode(51);
bst.addNode(150);
bst.addNode(12);
bst.addNode(152);

const treeOptions = {
  height: window.innerHeight - 135,
  linkStyleOptions: {
    strokeWidth: "5px",
  },
  nodeStyleOptions: {
    strokeWidth: "5px",
  },
};

binaryTreeDrawer().draw("#binarySearchTree", bst, treeOptions);
binaryTreeDrawer().selectNode(bst.root);
// onNodeClick with callback 
binaryTreeDrawer().onNodeClick((node: any) => {
  console.log("selected Node: ", node);
})

Functions for Binary Tree (binaryTreeDrawer)

FunctionDescription
draw()The Function will draw the Binary Tree that is Created, the Function accepts 3 Parameters. The First one: "HTML element where the Binary Tree should be drawn, this can be a Tag, Class, or Id". Second one: The Created Binary Tree. Third one: the options for the Binary Tree
onNodeClick()Event listener for a Node when it clicks. The function has a Callback function which will be triggered when the Node is clicked and it will return the current selected Node
animateNode()This Function will animate a Node. Just give a Function the Node Value you want to Animate and for animation options the function accepts also a parameter for that
animatePath()This Function will animate a Connection. Just give a Function the Node Value that the Path is from Top to down connected to and the Function has also a parameter for animation options
resetTree()Resetting the Binary Tree to the created a state
removeTree()Remove the Binary Tree from DOM
selectNode()The function for selecting any Node after Binary Tree is Created accepts the Node object. To use just give a function the Node you want to be selected
setFreeze()This function will freeze the Binary Tree and Nothing can be changed until you set it back to false. Note: the function will not stop the animation if it is already running

Binary Tree Options

NameTypevalueDescription
widthnumberDefault: window.innerWidthA width for the Binary Tree
heightnumberDefault: window.innerHeightA height for the Binary Tree
transformTreeFromTopnumberDefault: 0Move the Binary Tree itself from the Top, this property will move just the Binary tree and not the Tree Container
transformTreeFromLeftnumberDefault: 0Move the Binary Tree itself from the Left, this property will move just the Binary tree and not the Tree Container
zoombooleanDefault: true.Zoom the Binary Tree in and out
autoFitTextbooleanDefault: true.This property will auto-expand the Nodes Radius based on the node value length, so that the value of the node can fit inside the Node
drawNodesbooleanDefault: true.Draw the Nodes, if it is set to the false the Nodes will not be drawn
drawNodevaluebooleanDefault: true.Draw Node value, if it is set to the false the Node will display without its value
drawConnectionsbooleanDefault: true.Draw the Connections between the Nodes, if it is set to the false the Nodes will display without Connections
sizeBetweenNodesnumberDefault: 180.The size between the Nodes in the Binary Tree
autoExpandTreeSizebooleanDefault: false.This property will auto-expand the tree, based on how many nodes are in the Binary Tree and the screen size
addMouseHoverToNodesbooleanDefault: true.While moving the mouse to the Node it will be a pointer
animationbooleanDefault: false.Animate the Binary Tree while building it
durationnumberDefault: false.The animation duration to build the Binary Tree
nodeStyleOptionsObjectDefault: new NodeStyleOptions().The style options for the Nodes
linkStyleOptionsObjectDefault: new LinkStyleOptions().The style options for the Connections

Node Style Options

NameTypevalueDescription
radiusnumberDefault: 10The radius of the Node or the size of the Node
fillCollorstringDefault: #fffThe Color of the Node
onMouseHoverColorstringDefault: undefinedChange the Node Color while Mouse is over a Node
selectedNodeColorstringDefault: #09c372 Which is GreenThe current selected Node Color
strokeColorstringDefault: #09c372 Which is GreenThe Border radius or Stroke Color of the Nodes
strokeWidthstringDefault: 3pxThe width of the Node Border
styleClassstringDefault: undefinedAdd your own CSS class for the Node, NOTE: this will maybe disable some other default options for the Nodes
addAnimationNodesbooleanDefault: falseDuplicate the Nodes on Top of the default Nodes for Animation. Note: The Id for those Nodes will be the animationNode and the Node Value Example: animationsNode7

Connection/Link Style Options

NameTypevalueDescription
strokeColorstringDefault: #cccThe Color of the Connection
strokeWidthstringDefault: 3pxThe width of the Connection
styleClassstringDefault: undefinedAdd your own CSS class for the Connection, NOTE: this will maybe disable some other default options for the Connections
addAnimationNodesbooleanDefault: falseDuplicate the Connections on Top of the default Connections for Animation. Note: The Id for those Connections will be the animationPath and the Node Value Example: animationsPath10

Author

šŸ‘¤ Bassam/Martin Seydo

Show your support

Give a ā­ļø if this project helped you!

šŸ“ License

Copyright Ā© 2022 Bassam/Martin Seydo. This project is licensed under the MIT.