Subsections

6.1 BinaryTree: A Basic Binary Tree

The simplest way to represent a node, $ \ensuremath{\ensuremath{\mathit{u}}}$, in a binary tree is to explicitly store the (at most three) neighbours of $ \ensuremath{\ensuremath{\mathit{u}}}$. When one of these three neighbours is not present, we set it to $ \ensuremath{\ensuremath{\mathit{nil}}}$. In this way, both external nodes of the tree and the parent of the root correspond to the value $ \ensuremath{\ensuremath{\mathit{nil}}}$.

The binary tree itself can then be represented by a reference to its root node, $ \ensuremath{\ensuremath{\mathit{r}}}$:
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{initialize}(...
...{\ensuremath{\mathit{r}} \gets \ensuremath{nil}}\\
\end{flushleft}\end{leftbar}

We can compute the depth of a node, $ \ensuremath{\ensuremath{\mathit{u}}}$, in a binary tree by counting the number of steps on the path from $ \ensuremath{\ensuremath{\mathit{u}}}$ to the root:
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{depth}(\ensu...
...bf{return}} \ensuremath{\ensuremath{\mathit{d}}}\\
\end{flushleft}\end{leftbar}

6.1.1 Recursive Algorithms

Using recursive algorithms makes it very easy to compute facts about binary trees. For example, to compute the size of (number of nodes in) a binary tree rooted at node $ \ensuremath{\ensuremath{\mathit{u}}}$, we recursively compute the sizes of the two subtrees rooted at the children of $ \ensuremath{\ensuremath{\mathit{u}}}$, sum up these sizes, and add one:


\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{size}(\ensur...
...remath{\mathit{u}}.\ensuremath{\mathit{right}})}\\
\end{flushleft}\end{leftbar}

To compute the height of a node $ \ensuremath{\ensuremath{\mathit{u}}}$, we can compute the height of $ \ensuremath{\ensuremath{\mathit{u}}}$'s two subtrees, take the maximum, and add one:


\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{height}(\ens...
...emath{\mathit{u}}.\ensuremath{\mathit{right}})})\\
\end{flushleft}\end{leftbar}


6.1.2 Traversing Binary Trees

The two algorithms from the previous section both use recursion to visit all the nodes in a binary tree. Each of them visits the nodes of the binary tree in the same order as the following code:
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{traverse}(\e...
...remath{\mathit{u}}.\ensuremath{\mathit{right}})}\\
\end{flushleft}\end{leftbar}

Using recursion this way produces very short, simple code, but it can also be problematic. The maximum depth of the recursion is given by the maximum depth of a node in the binary tree, i.e., the tree's height. If the height of the tree is very large, then this recursion could very well use more stack space than is available, causing a crash.

To traverse a binary tree without recursion, you can use an algorithm that relies on where it came from to determine where it will go next. See Figure 6.3. If we arrive at a node $ \ensuremath{\ensuremath{\mathit{u}}}$ from $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{parent}}}$, then the next thing to do is to visit $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{left}}}$. If we arrive at $ \ensuremath{\ensuremath{\mathit{u}}}$ from $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{left}}}$, then the next thing to do is to visit $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{right}}}$. If we arrive at $ \ensuremath{\ensuremath{\mathit{u}}}$ from $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{right}}}$, then we are done visiting $ \ensuremath{\ensuremath{\mathit{u}}}$'s subtree, and so we return to $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{parent}}}$. The following code implements this idea, with code included for handling the cases where any of $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{left}}}$, $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{right}}}$, or $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{parent}}}$ is $ \ensuremath{\ensuremath{\mathit{nil}}}$:
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{traverse2}()...
...{\ensuremath{\mathit{u}} \gets \ensuremath{nxt}}\\
\end{flushleft}\end{leftbar}

Figure 6.3: The three cases that occur at node $ \ensuremath{\ensuremath{\mathit{u}}}$ when traversing a binary tree non-recursively, and the resultant traversal of the tree.
\includegraphics[scale=0.90909]{figs-python/bintree-traverse-2} \includegraphics[scale=0.90909]{figs-python/bintree-3}  

The same facts that can be computed with recursive algorithms can also be computed in this way, without recursion. For example, to compute the size of the tree we keep a counter, $ \ensuremath{\ensuremath{\mathit{n}}}$, and increment $ \ensuremath{\ensuremath{\mathit{n}}}$ whenever visiting a node for the first time:
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{size2}()}\\ ...
... {\color{black} \textbf{return}} \ensuremath{n} \\
\end{flushleft}\end{leftbar}

In some implementations of binary trees, the $ \ensuremath{\ensuremath{\mathit{parent}}}$ field is not used. When this is the case, a non-recursive implementation is still possible, but the implementation has to use a List (or Stack) to keep track of the path from the current node to the root.

A special kind of traversal that does not fit the pattern of the above functions is the breadth-first traversal. In a breadth-first traversal, the nodes are visited level-by-level starting at the root and moving down, visiting the nodes at each level from left to right (see Figure 6.4). This is similar to the way that we would read a page of English text. Breadth-first traversal is implemented using a queue, $ \ensuremath{\ensuremath{\mathit{q}}}$, that initially contains only the root, $ \ensuremath{\ensuremath{\mathit{r}}}$. At each step, we extract the next node, $ \ensuremath{\ensuremath{\mathit{u}}}$, from $ \ensuremath{\ensuremath{\mathit{q}}}$, process $ \ensuremath{\ensuremath{\mathit{u}}}$ and add $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{left}}}$ and $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{right}}}$ (if they are non- $ \ensuremath{\ensuremath{\mathit{nil}}}$) to $ \ensuremath{\ensuremath{\mathit{q}}}$:
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{bf\_traverse...
...remath{\mathit{u}}.\ensuremath{\mathit{right}})}\\
\end{flushleft}\end{leftbar}

Figure 6.4: During a breadth-first traversal, the nodes of a binary tree are visited level-by-level, and left-to-right within each level.
\includegraphics[scale=0.90909]{figs-python/bintree-4}

opendatastructures.org