8 - Trees
Published online by Cambridge University Press: 05 September 2016
Summary
Trees are one of the most pervasive data structures in Computer Science. They are also one of the most striking success stories of compact data structures: We can represent a tree of n nodes using basically 2n bits instead of pointers (or machine words). Moreover, the compact representations have much richer functionality than most classical representations.
Let us distinguish two types of trees: ordinal and cardinal. Ordinal trees allow for an arbitrary number of children per node but distinguish only their order. For example, the parse tree of a program or the structure of an XML document are ordinal trees. Cardinal trees, instead, have a fixed set [1, σ] of types of children, and each node might have one or no child of each type. The most common example of a cardinal tree is the binary tree, where each node may have a left and may have a right child. Having only a left child is not the same as having only a right child; an ordinal tree cannot make this distinction. Other examples are digital trees or tries (see Section 8.5.3), which have an alphabet [1, σ] and there can be at most one child for each alphabet symbol.
In addition, trees can be labeled, meaning that edges have an associated label from an alphabet [1, σ]. For example, an XML tree can be regarded as a labeled tree if we associate the tag names with their corresponding edges. Unlike a trie, a labeled tree node may have several children with the same label, and they can appear in any order. Thus, a cardinal tree can be regarded as a special type of labeled tree. An alternative model labels the nodes, not the edges. The two models are not very different, as we can assign the label of the node to the edge that leads to it (making an exception for the root). Thus, we will consider only labels at the edges.
We will mainly consider ordinal trees in this chapter and show how the solutions are extended to cardinal and labeled trees. In Chapter 2 we showed that bits are necessary to represent the topology of every possible ordinal tree of n nodes.
- Type
- Chapter
- Information
- Compact Data StructuresA Practical Approach, pp. 211 - 278Publisher: Cambridge University PressPrint publication year: 2016