Skip to content

Instantly share code, notes, and snippets.

View aryanpingle's full-sized avatar
😄
Always bet on JavaScript

Aryan Pingle aryanpingle

😄
Always bet on JavaScript
View GitHub Profile

Drawing a Binary Tree in O(N) Time

A Quick Introduction

In 1981, Edward M. Reingold & John S. Tilford published a paper titled, "Tidier Drawings of Trees". It proves that it is possible to assign coordinates to nodes and edges of a binary tree such that the coordinates result in an aesthetically pleasing 2-D drawing of the binary tree. This paper (and the algorithm outlined within) has been cited in hundreds of papers, and has since been generalized to work with N-ary trees.

I came across this algorithm while writing a Python package to visualize data structures (and in a bizarre coincidence, it was simultaneously being taught in my university course on Computational Geometry!). It's a really cool algorithm, and is explained really well in the paper itself. I want to focus on the practical aspect, so this post will be discussing my Python implementation of the Reingold-Tilford Algorithm.

Before I show you the code, let me convince you that this algorithm i