Table of Contents
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).