Giter Site home page Giter Site logo

binary-tree-visualizer-'s Introduction

Binary Tree Visualization

Add and search for nodes in a binary tree with an easy-to-use, web-based visualization

Example of balanced tree

Inspired by Coding Train's Binary Tree Visualization Challenge

What is a Binary Tree

A binary tree is a data type where every node in the graph can have at most two children. The node to the left of any parent node must contain a value less than the value of the parent node, and the node to the right must contain a value greater than the value of the parent node.

Binary Tree Example

For more information, see this explanation from Carnegie Mellon University

Using the Visualization

When the visualization is first opened, the screen will be empty

Blank visualization

To add a node to the tree, click the Add button, and enter an integer for the new node to hold. Watch as the animation adds the node to the tree. In this case, green represents the node that was just added. To pan on the visualization, click and drag. To zoom in and out on the visualization, scroll up or down.

Single node

Click the Add button a few more times to add more nodes to the tree, and watch as their insertion is animated. To make the animation progress faster, move the Animation Speed slider farther to the right. Blue represents nodes that must be visited to insert the node into the correct location

Multiple nodes

To watch the binary tree search for a value in itself, slick the Search button, and enter an integer that you previously inserted into the tree. Blue represents nodes that must be visited to find the value, red represents sections of the tree where the value will not be found, and green represents the node that was being searched for. Note that if no node is green, the value was not found.

Search Example

To clear the entire tree, click the Clear button. This will make your screen appear as it did when the visualization was first opened

Rather than manually adding nodes, you can fill the entire tree with a certain number of nodes. To do this, click the Fill button, enter the desired number of nodes in the prompt, and watch as nodes are added one at a time. Note: this clears the current tree

If you do not want to wait for the insertion animation, click the Quick Fill button, which functions the same as the Fill button, but does not animate the insertion process. Note: this also clears the current tree

Fill Example

Code Structure

  • Node.js - Defines the Node class, the building block of the binary tree. Nodes store their value, references to both of their child nodes, and information to to draw them on-screen (e.g. x and y-coordinates, colors, etc.)
  • Tree.js - Defines the Tree class, which serves as both a wrapper for the root node of the binary tree (e.g. providing functions for searching the tree for values), and the primary class responsible for animating the binary tree
  • Controls.js - Defines the Controls class, which connects the buttons (e.g. Clear, Quick Fill, etc.) to the animation functions of the Tree class
  • Explorer.js - Defines the Explorer class, which adjusts the size and position of the tree to allow for panning and zooming over the entire tree
  • sketch.js - Instantiates all the necessary objects to run the visualization

See each file for more detailed documentation, and in-depth explanations of how each class functions

Built with

  • p5.js - A library for creating visualizations using the canvas

Future Ideas

  • Make the tree self-balancing using AVL tree rotation
  • Add an animation for removing a node from the tree
  • Improve the performance of the Explorer class for larger trees with svg

binary-tree-visualizer-'s People

Contributors

lovelesh26 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.