Binary Trees and Javascript

Gary Cordero Rosa
2 min readDec 27, 2020

--

A tree is a hierarchical data structure composed of nodes. At the top of this data structure, is the root node pointing to a subtree of children. A good example of a tree data structure is the file system used by our OS.

File System

A binary tree is the kind of tree where each node has at most two children. A binary tree node is composed of a value and two pointers (usually refer as left and right pointers).

Binary Tree

Implementing a Binary Tree in Javascript

To implement a binary tree in javascript we would need to create two classes, the Node Class and the BinaryTree Class.

As discuss before each node has a value and two pointers.

Now we can implement our binary tree in the following way.

Once we implement these classes we can use our binary three, by passing a root node to our BinaryTree constructor and assigning the child nodes as needed.

If we log our root node we can see how the tree structure is being created

--

--

Gary Cordero Rosa
Gary Cordero Rosa

Written by Gary Cordero Rosa

Flatiron School Software Engineering Student

No responses yet