February 2, 2009
Introduction to Trees
Trees are very useful conceptual tools for representing relationships among certain classes
For example, trees can be used to represent familial relationships, such as the
following stylized family tree:
Here, “gp” is grandparent, “mf” is mother/father, “ua” is uncle/aunt, “k1”, “k2” and “k3”
are kids, and “c1” and “c2” are cousins of the kids.
As you may recall from our initial discussion of graphs (in the context of discussing
representations for relations), the diagram above is a
And, as you may have
guessed by now,
a tree is simply a particular type of graph
More formally stated:
is a connected graph with no cycles.
(A cycle is a path of length one
or more that starts and ends at the same vertex and does not contain repeated edges.)
The relationship of the elements in a tree is
In other words, as we descend
down the tree, each node in the tree can have many nodes “underneath” it.
in computer science, a compiler uses trees for representing algebraic expressions.
that the tree specifies an unambiguous order for evaluating an expression, as seen below.
Trees used to represent expressions, as the tree above, are sometimes called
In order to evaluate the expression represented above, we would need to evaluate
the tree bottom-up.
In other words, before we can evaluate the „/‟ operator at the top of
the tree, we must evaluate the sub-expressions (a – b) and ((c * d) + e) beneath it.
more accurately describe this process, we first present some terminology with respect to