Steven J. Zeil

Old Dominion University, Dept. of Computer Science

Table of Contents

1. Tree Terminology
1.1. All in the Family
1.2. Binary Trees
1.3. Paths
1.4. Paths [cont.]
1.5. Depth & Height
1.6. Depth & Height [cont.]
2. Tree Traversal
2.1. Kinds of Traversals
2.2. Recursive Traversals
2.3. Level-Order Traversal
3. Example: Processing XML
3.1. The Traversal

Most of the data structures we have looked at so far have been devoted to keeping a collection of elements in some linear order.

Trees are the most common non-linear data structure in computer science. Trees are useful in representing things that naturally occur in hierarchies (e.g., many company organization charts are trees) and for things that are related in a is-composed-of or contains manner (e.g., this country is composed of states, each state is composed of counties, each county contains cities, each city contains streets, etc.)

Trees also turn out to be exceedingly useful in implementing associative containers like std::set. Properly implemented, a tree can lead to an implementation that can be both searched and inserted into in O(log N) time. Compare this to the data structures we've seen so far, which may allow us to search in O(log N) time but insert in O(N), or insert in O(1) but search in O(N).

In the Forum:

(no threads at this time)