BinaryTreeVisualizer: Turning LeetCode Strings Into Moving Trees

A plain-JavaScript SVG engine that parses tree dialects, maps nodes, and animates BST and heap operations with almost no machinery.

8 min read • View on GitHub • More from Rishi-269

A serialized tree input stream on the left becomes a clean node graph on the right, with a visible handoff between text and structure. The image explains that the project is really about translation, not just drawing circles and lines.
BinaryTreeVisualizer’s core move is to turn platform-specific tree syntax into a live structure the browser can animate.
Key Takeaways

Why tree visualizers fail at the one thing students need

Tree problems are hard for a boring reason. The hard part is often not the algorithm, it is the representation. Students copy a string like [1,null,2,3] or a GeeksforGeeks style preorder input, then spend half their time asking what the string even means.

BinaryTreeVisualizer starts there. It does not assume the user already has a node graph in their head. It treats the serialization itself as the thing to understand, then turns that string into motion.

The real invention is the parser bridge

The center of the repo is constructTree.js, which normalizes different tree dialects into one in-memory structure. That matters because the browser only needs to know one shape after parsing: nodes, pointers, and stable IDs.

The interesting problem is not drawing a tree. It is translating multiple input formats into one graph that stays addressable from parse to animation.

The repo’s key trick is a nodeMap. Each node gets a stable identity, so later code can target the same object when it needs to highlight, swap, or search. That is a small detail with a large payoff, because animation is easier when structure does not drift underneath it.

A close-up view of tokenized tree inputs feeding into a shared mapping layer, with node identities attaching as each node is created. The image explains how the repository normalizes incompatible serializations into one addressable structure.
The bridge layer makes different input formats feel like one system instead of a pile of special cases.

How the renderer stays lightweight without feeling primitive

Once the tree is normalized, the rendering path is straightforward. treeGraphics/main.js calculates the canvas size from tree height, then places nodes recursively with shrinking horizontal gaps. It is classic SVG math, not a framework problem disguised as a UI problem.

const width = (1 << maxHeight) * nodeRadius * 4;

while (pause) {
  await new Promise(resolve => setTimeout(resolve, 100));
}

That simplicity is the point. CSS transitions handle the smooth movement of circles and lines, while async JavaScript handles timing. The browser does the expensive part, so the repo can stay readable.

A tree is laid out recursively across the page while a second layer shows a node moving through a swap and highlight sequence. The image explains how SVG positioning and browser transitions work together to create legible animation.
The animation stack is intentionally small. Math places the nodes, and the browser smooths the motion.

Insertion, deletion, heapify: where the browser becomes a teacher

The animation files, including BSTAnimation.js and heapAnimation.js, turn state changes into visible steps. Insert, search, delete, bubble up, and sift down are all easier to follow when the browser pauses at each decision point instead of jumping to the end state.

That is where the repo stops being a diagram and becomes a lesson. A node turns gray, then highlighted, then found. Two nodes swap places. A subtree gets rebalanced visually, not just described in prose.

A comparison scene of a binary search tree operation being stepped through in small visible stages, with one node highlighted, one node replaced, and a heap swap shown in the background. The image explains that the browser is acting like a teaching tool for algorithm state changes.
BinaryTreeVisualizer makes intermediate states visible, which is exactly where students usually get lost.

Why this feels better than the obvious alternatives

The cleanest comparison is not feature count. It is teaching fit. Static diagrams are easy to scan, but they stop where the real confusion starts. Framework-heavy visualizers can look polished, but they add abstraction the student never asked for.

ApproachStrengthWeaknessWhat BinaryTreeVisualizer does differently
Static textbook diagramsFast to scanNo motion, no sequenceTurns serialization into a live process
Framework-heavy visualizersPolished UI and extensibilityMore baggage than the task needsUses native browser primitives only
Generic tree librariesFlexible renderingNot education-first or dialect-awareUnderstands coding-platform input formats first

BinaryTreeVisualizer is narrower, and that is why it works. It is built for one job: help a student move from text input to mental model without switching tools or learning a new stack.

A split editorial scene contrasts a heavy layered software stack on one side with a stripped-down HTML, SVG, and JavaScript workbench on the other. The image explains why the repo’s restraint is a feature for a narrow educational job.
For this use case, less machinery means less friction.

The constraints are the design

This is a solo, dependency-free project, and the code reads like it. There is global state in index.js, a homegrown queue, and a lot of direct DOM work. In a bigger product, those would be liabilities. Here they are evidence that the author optimized for clarity over ceremony.

That trade-off also explains the tone of the repo. It feels like an educational tool built by someone who cares more about the student getting unstuck than about architectural purity. For this job, that is the right bias.