An Introduction to Trees

By: O. Wolfson

Introduction to Trees

A tree is a hierarchical data structure that consists of nodes connected by edges. The nodes in a tree represent some kind of data, and the edges represent relationships between the nodes.

In a tree, there is one node called the root node, which is the topmost node in the hierarchy. Each node in the tree has zero or more child nodes, which are connected to it by edges. Nodes with no children are called leaf nodes.

Read a detailed introduction to the tree data structure: Tree Data Structure

Why Trees?

Trees are useful data structures for storing data that has a hierarchical relationship. For example, a file system on a computer is a tree, where the root node represents the root directory, and each child node represents a file or directory in the root directory.

Creating a Tree in JavaScript

In JavaScript, we can represent a tree using objects. Each node in the tree is an object with properties that represent the node's data and its children.

Here's an example of how to create a simple tree in JavaScript:

class Node {
  constructor(data) { = data;
    this.children = [];

  addChild(node) {

let root = new Node("A");
let nodeB = new Node("B");
let nodeC = new Node("C");
let nodeD = new Node("D");
let nodeE = new Node("E");


In this example, we define a Node class that has a data property and a children property, which is an array of child nodes. We also define an addChild method that adds a child node to the node's children array.

We then create a root node with the data 'A', and four child nodes with the data 'B', 'C', 'D', and 'E'. We add the child nodes to the root node and to the 'B' node using the addChild method.

Traversing a Tree

There are several ways to traverse a tree, which means visiting all the nodes in the tree in a specific order. Two common traversal methods are depth-first traversal and breadth-first traversal.

Depth-First Traversal

In a depth-first traversal, we visit the deepest nodes in the tree first before visiting shallower nodes. There are three ways to do a depth-first traversal: pre-order, in-order, and post-order.

Pre-Order Traversal

In a pre-order traversal, we visit the current node before visiting its children. Here's how to do a pre-order traversal in JavaScript:

function preOrder(node) {
  for (let child of node.children) {

preOrder(root); // prints 'A', 'B', 'D', 'E', 'C'

In this example, we define a preOrder function that takes a node as an argument and prints the node's data to the console. We then call the preOrder function on the root node, which recursively calls itself on each child node.

In-Order Traversal

In an in-order traversal, we visit the current node between its left and right children. However, in a tree, there is no "left" or "right" child, so we can't do an in-order traversal in the traditional sense. Instead, we can only do a pre-order traversal.

Post-Order Traversal

In a post-order traversal, we visit the current node after visiting its children. Here's how to do a post-order traversal in JavaScript:

function postOrder(node) {
  for (let child of node.children) {

postOrder(root); // prints 'D', 'E', 'B', 'C', '