Another common nonlinear data structure is the tree. We have already seen an example of a tree when we looked at the employee hierarchy from the XYZ company. Let's take another look at this diagram with some of the important features of trees highlighted.
In this diagram, we can see that the starting point, or the root node, is circled in blue. A node is a simple structure that holds data and links to other nodes. In this case, our root node contains the data string "John" and three links to other nodes. Notice that the group of nodes circled in red do not have any links. These nodes are at the ends of the branches and they are appropriately called leaves or leaf nodes. In our diagram, the nodes are connected with solid black lines called arcs or edges. These edges show the relationships between nodes in the tree. One important relationship is the parent/child relationship. Parent nodes have at least one edge to a node lower in the tree. This node is called the child node. Nodes can have more than one child, but children can only have a single parent. Notice that the root node has no parent, and the leaf nodes have no children. The final feature to note in our diagram is the subtree. At each level of the tree, we can see that the tree structure is repeated. For example, the two nodes representing "Charles" and "Rick" compose a very simple tree with "Charles" as the root node and and "Rick" as a single leaf node.
Now let's examine one way that trees are implemented in the computer's memory. We will begin by introducing a simple tree structure called a binary tree. Binary trees have the restriction that nodes can have no more than two children. With this restriction, we can easily determine how to represent a single binary node in memory. Our node will need to reserve memory for data and two pointers.
Using our binary node, we can construct a binary tree. In the data cell of each node, we will store a letter. The physical representation of our tree might look something like this:
Although the diagram above represents a tree, it doesn't look much like the tree we examined from the XYZ company. Because our tree uses pointers, the physical representation is much different than the logical representation. Starting with the root node of the binary tree (the node that contains 'H'), see if you can draw a sketch of the logical representation of this tree. Once you are finished, you may view the answer.