Welcome to treetown! Population: JavaScript

In any computer science education, you'll get the importance of tree data structures hammered into you through books, professors, and midterms. Almost every job interview throws a problem at you where they expect you to use a tree to achieve that coveted Θ(log n) lookup time. Why isn't there a good standardized tree-library in JavaScript then? Good question.

It turns out it isn't too hard to implement a decent one using nothing but plain old JavaScript objects, but it is somewhat awkward to use without helper functions. Here's an example of what I typically use when working with trees in JS.

I implemented this using iteration vs recursion since it performs and deals with large datasets better in JavaScript. It also makes it trivial to switch between depth first search (DFS) and breadth first search (BFS). It'd be easy to extend this to become one of the many flavors of trees, such as a binary tree, red-black tree, etc.

Bret Lowrey

Code is like a war - the best code is one never written.

Florida, USA