How to build, traverse and map simple trees

Trees have funny definitions like this:

A tree is either nothing or something creating more trees.

Here is some terminology.

 Node. A structure holding some data. Edge. A connection from a parent node to a child node. Parent of a node X. Any node that creates X. Child of a node X. Any node that is created by X. Degree of a node X. Number of children of X. Sibling of a node X. Any node which is another child of a parent of X. Ancestor of a node X. Any node that is a parent of X or an ancestor of a parent of X. Descendant of a node X. Any node that is a child of X or a descendant of a child of X. Root node. Any node without parents. Leaf node. Any node without children. Internal node. Any node which is not a root nor a leaf. Level of a node X. 1 + Number of edges from a root to X. Height of a node X. Number of edges from X to a leaf.

We call simple trees those trees where there is only one root and all other nodes have only one parent. There are many things that would be better represented by trees with multiple roots and multiple parents. Unfortunately their complexity is not really different from directed acyclic graphs, so the former are seen as a special case of the latter and treated accordingly.

Building

In JavaScript, we can build simple trees like this:

{[ .creation | 1.hilite(=javascript=) ]}

whose console output is:

Usually we create trees for storing data together with their hierarchical relationships to other data. For example, a thesaurus easily fits into a tree. In fact, we can even define a thesaurus as a tree-like structure:

A thesaurus is either nothing or something creating a narrower thesaurus, and relating to some other thesaurus.

Thus a thesaurus is just a tree together with an additional generic relationship between its nodes.

Traversing

A tree traversal is maybe the most common operation for a tree, regarded as a collection of nodes. Each node is visited once, and there are many possible ways to do it orderly. One of the most common ways is to traverse in depth first order, i.e. starting from the root, we visit a node and each of its children. For example, in our tree (1(2(5,6),3,4(7))), a depth first traversal would visit in order: 1,2,5,6,3,4,7. It’s easy to understand why it’s a very common traversal. It’s not a coincidence that the order is the same we would get by erasing the structure.

Here is a bit of JavaScript code for traversing a tree in depth first order.

{[ .traversal-each| 1.hilite(=javascript=) ]}

The console output is:

Mapping

There are many possible things we can do while traversing a tree. One of the most common is to map nodes’ contents to some transformation so that we get another tree with same structure but different data.

Here is an updated version of TreeTraversal with a new map method.

{[ .traversal-map| 1.hilite(=javascript=) ]}

The console output is exactly the same as the one we got when building tree.

Note that both .each() and .map() of TreeTraversal share the same signature and functionality with .each() and .map() of jQuery respectively.

Here is a jsFiddle if you want to play around.

This site uses Akismet to reduce spam. Learn how your comment data is processed.