Example A:
5 / \ 3 7 / \ / \ 2 4 6 8 / \ 1 9
Example B:
2 4 9 | | | 1----3----5----8 | | 6 7
Example C (Linked List):
1 - 2 - 3 - 4
All of these trees are very different. Example A is a binary search tree, which is a type of rooted tree. Example B, in contrast, is not a rooted tree, and it is not ordered. Example C is a doubly-linked list, a subset of tree. Notice how all of these are acyclic, simple graphs.
Rooted Trees
Rooted trees are most commonly studied in computer science, with applications in search trees like binary search trees, B-Trees. Certain data structures, like heaps, are also rooted trees. A rooted tree is defined such that there is a parent node with zero or more child nodes. A node that has no children is called a leaf. So by definition, a rooted tree is a directed graph.
- Binary Search Tree: A Binary Search Tree is based on the sorting algorithm Binary Search. It organizes data so there is an ideal log(n) lookup time for any element. Balanced binary search trees actually rotate themselves to enforce a worst case O(log(n)) lookup, insertion, and deletion time for elements. Just like with binary search, the median element is the first one examined, so that is the root Node. Let's take a look at how a binary search tree would be formed given the following elements: [5, 2, 3, 9, 7, 4.
5 / \ 2 9 \ / 3 7 \ 4
Since 5 is the first element, it is the root. The next element 2, is less than 5, so it goes to the left of the root. Since 3 is less than 5, it goes to the left of 5. It is also greater than 2, so it goes to 2's right. This process continues for the remaining elements until the above binary search tree remains.
Spanning Trees
Given a graph, and a tree on the graph, the tree is a spanning tree on the graph if and only if it encompasses all the vertices on the graph. As with much of graph theory and computer science, finding the optimal (or minimal) spanning tree is desirable.
A simple spanning tree can be constructed from a Graph with either a breadth or depth first traversal of the tree. A breadth first traversal examines each of the sibling nodes before examining the siblings' children. In contrast, a depth first traversal examines the next generation before examining the sibling nodes.
Consider the Graph:
A -- B -- E | \ | \ | \ | \ C----D -- F
Now let's use both algorithms to construct a spanning tree, starting at A.
Using a breadth-first traversal, A's children (B, C, D) are all visited. Next, B's children are evaluated, leaving E and F to be evaluated.
A -- B -- E | \ \ | \ \ C D F
Now using a depth-first, traversal, A's first child B is evaluated. Next, B's children (E and F) are added to the tree. Since B has no more children, A's next child © is evaluated. C has only one child, D. Thus, the spanning tree looks like:
A -- B -- E | \ | \ C -- D F
On weighted graphs, minimum spanning trees can be found. A minimum spanning tree on Graph g is a spanning tree with the smallest possible sum of weights. Minimum spanning trees are found using one of two algorithms: Prim's Algorithm or Kruskal's Algorithm. Both of these algorithms would require tutorials in and of themselves. I do have a tutorial on Kruskal's algorithm for those that are interested.
Tree Traversals
The final section of this tutorial will discuss the three primary types of depth-first traversals: pre-order, in-order, and post-order. A pre-order traversal visits the root node first, then the left subtree, followed by the right subtree. An in-order traversal visits the left subtree, the root, then the right subtree. Lastly, the post-order traversal visits the left subtree, the right subtree, and finally the root. Each of these algorithms are evaluated recursively.
Consider the following tree:
5 / \ 3 7 / \ / \ 2 4 6 8 / \ 1 9
When evaluated with a pre-order traversal, the Nodes are visited in the following order: 5, 3, 2, 1, 4, 7, 6, 8, 9. The 5 node is the root, so it is evaluated first. As these algorithms are evaluated recursively, the 3 subtree is evaluated next. So holding to form, 3 is evaluated, followed by the 2 subtree. So the ordering here has to be 3, 2, 1, then visiting 3's right child 4. The same process is then applied for 5's right child.
With an in-order traversal, the Nodes are evaluated in the following order: 1, 2, 3, 4, 5, 6, 7, 8, 9. Because an in-order traversal evaluates the left subtree first, applied recursively, it recurses all the way down to the far left leaf: 1. Then it evaluates 1's parent, the root of the subtree: 2. From there, 3 is evaluated as 2's parent, then 3's right subtree (4) is evaluated next. This process is applied for the remainder of the tree.
With a post-order traversal, the Nodes are evaluated in the following order: 1, 2, 4, 3, 6, 9, 8, 7, 5. Here, 1 is evaluated first as the left leaf Node. Since it has no right sibling, its parent 2 is evaluated next. The 2 Node has a right sibling though, 4, which is evaluated before the parent 3. This process is applied throughout the rest of the tree.