Data Structures: Graph Theory- Introduction to Trees

Posted 09 September 2012 - 12:23 PM

In this tutorial, trees will be introduced in the context of graph theory. Trees are defined as simple, acyclic graph. This means there exists exactly one path between any two vertices in a tree. Let's look at some examples of trees:

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.

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.

Re: Data Structures: Graph Theory- Introduction to Trees

Re: Data Structures: Graph Theory- Introduction to Trees

Posted 13 September 2012 - 06:47 PM

Very good. What I find interesting is that, if you think of each piece of code/information being a 'node' per-say, does this mean we have essentially 'trees within trees', that are, more or less, binary fractals? Kudos on the article.

Re: Data Structures: Graph Theory- Introduction to Trees

Posted 14 September 2012 - 06:54 AM

Fractals are certainly recursive and can be modeled using Trees. Remember that Trees don't necessarily have to be binary though. I wrote a tutorial a while back on Recursion, Stacks, and Trees that you may want to check out.