Professional Documents
Culture Documents
CHAPTER 4 TREES
CONTENT
Introduction to Trees Application of Trees Tree Traversal Trees and Sorting (Data structure) Spanning Trees Minimum Spanning Trees
A Tree
A tree is a connected undirected graph with no simple circuit. An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. THEOREM: A tree with n vertices has n 1 edges.
EXAMPLES
e
NOT A TREE
e
A TREE
e
NOT A TREE
EXERCISE 4.1
1. Which of the following graphs are trees?
a b c a b
e
FIGURE 1
d
FIGURE 2
a c d e
FIGURE 3
v2
v1
v3
v5
FIGURE 4
v4
A Rooted Tree
A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root.
f d a c
A TREE
g b
a c d b e f a
e d g
A TREE with root c
e
f
a g h
Height The maximum level of all the vertices The height of this tree is 3.
a b c g h
Internal Vertices vertices that have children The internal vertices are a, b, c, g, h and j
A subtree rooted at g
Subtree A subgraph of the tree consisting of a root and its descendent and all edges incident to these descendent.
EXERCISE 4.1
2. Answer these questions about the rooted tree illustrated.
a) Which vertex is the root? b) Which vertices are internal? c) Which vertices are leaves? d) Which vertices are children of g? a b c e f g h o n q p i d e) Find a subgraph. f) Find the level of each vertex. g) What is the height of this tree? h) Is this tree a balanced tree? e) Which vertex is the parent of o? f) Which vertices are siblings of e? g) Which vertices are ancestors of m? h) Which vertices are descendants of d?
k l
m-ary Tree
A rooted tree is called an m-ary tree if every vertex has no more than m children.
The tree is called a full m-ary tree if every internal vertex has exactly m children.
A rooted m-ary tree is balanced if all leaves are at levels h or h-1.
EXAMPLES
a b c d e a
a b c d f f g e
A full 4-ary TREE
b
g h i j k l m f
d g
A 3-ary TREE
m-ary Tree
THEOREM 1: A full m-ary tree with i internal vertices contains n = mi + 1 vertices
Send letter to four other people 4-ary tree m = 4 People send out the letter internal vertices i People do not send out the letter leaves l = 100 By theorem 2 (iii): Number of people have seen this letter n = (4100 1) / (4 1) = 133 Number of people send out the letter i = 133 100 = 33
EXERCISE 4.1
3. A chain letter starts with a person sending a letter out to 10 others. Each person is asked to send the letter out to 10 others, and each letter contains a list of previous six people in the chain. Unless there are fewer than six names in the list, each person sends one dollar to the first person from the list, moves up each of other five names one position, and insert his names at the end of this list.
If no person breaks the chain and no one receives more than one letter, how much money will a person in the chain ultimately receive?
zoology
Decision Trees
A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of decision.
a>c EXAMPLE
b<c c>b>a
The possible solutions of the problem correspond to the paths to the leaves of this rooted tree.
b:c b>c
b<c a>c>b
a>c
a<c b>c>a
a>b>c
b>a>c
Game Trees
Use trees to analyze certain types of games Vertices represent the positions can be in as it progresses Edges represent legal moves between this position Games tree is infinite if the games they represent never end
EXAMPLE
A Game tree that represents the first two level of the tic-tac-toe game.
CHAPTER 4 TREES
Tree Traversal
Ordered trees are often used to restore data/info.
Tree traversal is a procedure for systematically visiting each vertex of an ordered rooted tree to access data.
If the tree is label by Universal Address System we can totally order the vertices using lexicographic ordering Example: 0 < 1 < 1.1 < 1.2 < 1.2.1 < 1.3 < 2 < 3 < 3.1 < 3.1.1 < 3.1.2 < 3.1.2.1 < 3.1.2.2 < 4 < 4.1
Preorder Traversal
Let T be an ordered rooted tree with root r. If T consists only of r, then r is the preorder traversal of T. If T1, T2, , Tn are subtrees at r from left to right in T, then the preorder traversal begins by visiting r, continues by traversing T1 in preorder, then T2 in preorder, and so on until Tn is traversed in preorder.
r STEP 1 Visit r TIPS Preorder Traversal: Visit root, visit subtrees left to right
T1
T2
Tn
STEP n+1 Visit Tn in preorder
b c e j n k o p b e j n k o p f l c
a b
f k
h i
m
ITERATION 2
f
l
h
m
j
n a b e j
o p
k f c d g l mh i
o p
ITERATION 3
g h
m
ab e j k n opf c dgl mh i
ITERATION 4
ITERATION 1
Inorder Traversal
Let T be an ordered rooted tree with root r. If T consists only of r, then r is the inorder traversal of T. If T1, T2, , Tn are subtrees at r from left to right in T, then the inorder traversal begins by traversing T1 in inorder, then visiting r, continues by traversing T2 in inorder, and so on until Tn is traversed in inorder.
r STEP 2 Visit r TIPS Inorder Traversal: Visit leftmost subtree, Visit root, Visit other subtrees left to right.
T1
STEP 1 Visit T1 in inorder
T2
STEP 3 Visit T2 in inorder
Tn
STEP n+1 Visit Tn in inorder
b c e j n b e f k n o p l k o p a c
e b f k n j e o p
a c
d h i
m
ITERATION 2
f
l
h
m
d n
k b f a c l g md h i
ITERATION 3
g h
m
o p
j e n k opb f ac l g m d h i
ITERATION 4
ITERATION 1
Postorder Traversal
Let T be an ordered rooted tree with root r. If T consists only of r, then r is the postorder traversal of T. If T1, T2, , Tn are subtrees at r from left to right in T, then the preorder traversal begins by traversing T1 in postorder, then T2 in postorder, and so on until Tn is traversed in postorder and ends by visiting r.
r STEP n+1 Visit r TIPS Postorder Traversal: Visit subtrees left to right, Visit root.
T1
T2
Tn
b c e j n b e j n k o p f l k o p c
f k
b c
h i d a
m
ITERATION 2
f
l
h
m
j
n j
o p k e
f b c l mg h i da
ITERATION 3
g h
m
o p
j n o pk e f bc l mg hi d a
ITERATION 4
ITERATION 1
EXERCISE 4.3
1. Determine the order in which a preorder, inorder, and postorder traversal visits the vertices of the following rooted tree.
a b c e f g h m o n q r s d i d a
b
c
f
h i
g
j
k l
FIGURE 1
FIGURE 2
+-*235/234
23=8
+-*2352
2*3=6
+-*235/84
8/4 =2
+-652
6 5 =1
+-*2352
2*3=6
+12
1+2=3
723*-493/+
2*3=6
1493/+
14=1
76-493/+
7-6=1
193/+
9/3 = 3
1493/+
14=1
13+
1+3=4
EXERCISE 4.3
2. Represent the following expression using binary trees. Then write these expression in infix, prefix and postfix notations. a. ((x+2)3)*(y (3+x)) 5 b. (AB) (A(B A))
3. What is the value of these expression in prefix expression? a. + 3 2 2 3 / 6 4 2 b. * + 3 + 3 3 + 3 3 3 4. What is the value of these expression in postfix expression? a. 3 2 * 2 5 3 8 4 / * b. 9 3 / 5 + 7 2 *
Spanning Trees
Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G. A simple graph is connected if and only if it has a spanning tree. Applied in IP multitasking.
a b c d a b c d e e f g f g
Not Spanning tree
d
Spanning tree
A simple graph
EXERCISE 4.5
1. Find a spanning tree for the following graphs.
b
a
c
b e f
h
e
FIGURE 1
g
c b
FIGURE 2
b a c
FIGURE 3 FIGURE 4
e d
Prims Algorithm
1. Chose an edge with the least weight. 2. Include it in spanning tree, T. 3. Select an edge of least weight that is incident with a vertex of an edge in T. 4. If it does not create a cycle (simple circuit) with the edges in T, then include it in T; otherwise discard it. 5. Repeat STEPS 3 and 4 until T contains n-1 edges. There may be more than one minimum spanning tree for a given connected weighted simple graph. If there are two edges with similar smallest weight, chose either one.
CHOICE
d
EDGE
WEIGHT
b
7
{ a, b }
SPANNING TREE b
7
16
12 15 10 11 e 12
FIGURE 1
a 2 { a, d } 10 b
7 10 d 10 11 e 12 10 11 d d
{b, e}
11
a b
7
b
7 10 11
12
a 4 {e, c} 12 b
c
Total weight = 40
Kruskals Algorithm
1. 2. 3. 4. 5. Arrange the edges in G in increasing order. Chose an edge with the minimum weight. Include it in spanning tree, T. Add an edge of least weight to T. If it does not create a cycle (simple circuit) with the edges in T, then include it in T; otherwise discard it. 6. Repeat STEPS 4 and 5 until T contains n-1 edges. There may be more than one minimum spanning tree for a given connected weighted simple graph. If there are two edges with similar smallest weight, chose either one.
CHOICE
d
EDGE
WEIGHT
b
7
{ a, b }
SPANNING TREE b
7
16
12 15 10 11 e 12
FIGURE 1
a 2 { b, d } 10 b
7 d 10
{b, e}
11
a b
10 7
d 11
a
d
e d 12
b
7
10
{e, c}
12 b
c
10
12 e
11
Total weight = 40
11
EXERCISE 4.6
1. Construct a minimum spanning tree for each of the following connected weighted graphs using Prims and Kruskals algorithm.
c 3 2 11
a
1 e 2 1 8
3 f 5 1
b f
7
2 5
e
3
6
8 7
1
6
b
1 d
g 1 c
FIGURE 1
FIGURE 2
CHAPTER 4 TREES
Tree is a connected undirected graph with no simple circuits Trees have been employed to solve problems in a wide variety of disciplines.
SUMMARY