Chapter 8 Trees Discrete Structures Notes

Table of Contents
Adjacency list MST list
Scanning Trees Sort Comparasions


Adjacency list for a directed graph
ADJLIST node weight ptr to node weight ptr
to
1 2 25 null      
2 3 10 4 4 14 null
3 1 5 6 6 16 null
4 3 18 null      
5            
6 4 32 5 5 42 null
7 6 11 null      

Below is the directed graph represented by the structure above. Each node has a substructure the consists of a node name, weight to node and a pointer to the next adjacent node from the root. Using this structure, it is possible to keep track of the nodes, the weight to the node and a list of adjacent nodes. This data structure allows us to scan a graph. If we add information on nodes visited (see below) we can keep track of the nodes visited, the adjacent nodes and the nodes that are yet to be examined. These notes are from Baase, ch 3. These are the type of structures that will be covered in Data Structures and in Analysis of Algorithms. In C++, these structures can be defined using typed or as objects with properties. C++ works well since you can define pointers as part of your node's data structure.

tree1.gif (3616 bytes)


VSET=1 if the vertex is already include, 2 if it is adjacent to an included vertex, and tree otherwise.

This structure allows us to keep track of the final weights at each vertex as well as the vertex included, adjacent vertices and those yet to be examined.

  Minimal Spanning Tree data structure
  V2LINK weight parent VSET ADJLIST
a   2 a 1 Adjacency list for a directed graph
b null 4 b 1  
c f     2  
d       3  
e       3  
f null 7 a 2  
g f 3 a 1  
h c 3 g 2  
i h 1 g 2  

mst.gif (4826 bytes)

 


Scanning Trees

Another structure that is useful in tracking binary trees is a node that has a right link, left link and a parent link. The pointers for the graph are listed below. With the various search methods, the pointers can be set up so that each node has a pointer to the node that comes before and after it in the given ordering. This effectively makes the tree a linked list.   

binary_tree.gif (10382 bytes) This is a representation of a binary tree. Below is a dense list that represents the same tree. There are a number of ways to scan a tree. One way is known a a left-node-right(LNR) which visits the left side, the node and the the right side, recursively. In this graph, we would have f,e,g,i,b,c,h,j,d,a. This is also known as ignored, symmetrical or lexicographical (alphabetical) scans. This is a recursive scan since as we go on, each node the we reach is in tern searched in LNR or ignored.(Note: if the left node is missing, we assume it is null and then go to the root and rigth nodes.)

Another approach is a depth-first scan also known as (preorder). In this scan, you visit the node first, then scan the left subtree as far as you can go, backup and scan the right tree, then backup up a node and repeat the process. In this case, a,b,e,f,g,i,c,h,j,d. You are in effect going down on path, backing up and going all the way down the next path. This is a recursive procedure since we repeat the dept-first search as we progress down. For example, when we reach b, we use it as the root and do a depth first search from it. When we get to e, we do a depth first search from it. When we can't go any further, we backtrack to the nearest parent (e) in the case) and go down the right side in depth first order.

Another approach is to scan in breath-first order. In this method, you start with the root, then scan level 1 from left to right, then level 2 from left to right and the the next levels the same way. In this graph, it would be a,b,e,c,f,g,h,d,i,j. You are, in effect, taking all the nodes that are 1 away from the root, 2 away, 3 away etc. in that order.

Scans like these make up an important class  problems and are studied in depth in Data Structures along with their algorithms. For this course, you should know how to scan ignored, preorder, postorder(not shown) and breath-first and depth-first scans..

 

Basic Representation of the Binary Graph
Index Node Left Link Right Link
1 A 2 -
2 B 5 3
3 C 9 4
4 D - -
5 E 6 7
6 F - -
7 G - 8
8 I - -
9 H - 10
10 J - -

 

Sort Storage Comparasions
Bubble n n*(n-1)/4
Selection n n*(n-1)/2
Merge 2n n*(log2n)
Quicksort n+stack 2n*(ln n)

You are the visitor to this site.

Return to Main Page