Chapter 10
|
Modified: |
Resources
http://highered.mcgraw-hill.com/sites/0072880082/student_view0/index.html - Index to the Rosen text Web site learning resources.
http://highered.mcgraw-hill.com/sites/0072880082/student_view0/chapter10/extra_examples.html - Extra examples with solutions.
10.1 Introduction to Trees
Definition 1
Tree is a connected graph with no simple circuits. Example
G1, G2 are trees
G3 has circuit d,a,b,c,d
G4 is not connected
Definition
Forest is unconnected trees.
Theorem 1
Tree as undirected graph iff simple path between any two vertices. Definition 2
Rooted tree has one vertex designated as root and every edge is directed away from the root.
T is undirected graph as a tree, any vertex could be root.
Question
Draw tree T with root b.
Definitions by Example
Parent a of g, f, b.
Child g, f, b of a.
Leaf if no children, d,e,f,i,k,l,m
Ancestors a, b,c of d.
Siblings g, f, b.
Descendants b, c, d of a.
All are descendants of the root
Internal vertices have children.
a,b,c,g,h,j
Subtree g.
Definition 3
m-ary tree if every internal Vertex has no more than m children. Full m-ary tree if every internal Vertex has exactly m children.
Binary tree is m-ary tree with m=2.
T1 is full binary
T2 is full 3-ary
T3 is full 5-ary
T4 is 3-ary
Definition
Left child in (a) of parent a is b in (c) of parent i is k.
Trees as Models
Seven connected processors
Recurrence relations
Edges are labeled with the values of each recurrence term.
a0 = 3 a1 = 4
an = an-1 + an-2
Properties of Trees
Theorem 2
n vertices tree has n-1 edges. Example
Below tree has 7 vertices and 6 edges.
Theorem 3
Full m-ary tree with i internal vertices contains n=mi+1 vertices. Example
Above 2-ary tree
3 internal vertices
n=2*3+1 = 7 total vertices.
Theorem 4
Full m-ary tree with: (i) n vertices has i=(n-1)/m internal vertices and l=[(m-1)n+1]/m leafs.
(ii) i internal vertices has n=mi+1 vertices and l=(m-1)i+1 leafs.
(iii) l leafs has n=(ml-1)/(m-1) vertices and i=(l-1)/(m-1) internal vertices.
Example
Full 3-ary tree: (iii) l leafs has n=(ml-1)/(m-1) vertices and i=(l-1)/(m-1) internal vertices.
9 leafs
n = (3*9-1)/(3-1) = 13 total vertices
i = (9-1)/(3-1) = 4 internal vertices
Example 9
Chain letter sent to 4 others represented by 4-ary tree with leaf Vertexs those not sending letter.
How many people have seen letter, including first person, if no duplicates and letter ends after 100 people received but did not send (leaf Vertex)?
How many people sent the letter?
Internal Vertexs those who sent letter.
(iii) l leafs has n=(ml-1)/(m-1) vertices and i=(l-1)/(m-1) internal vertices.
l=100 Leaf Vertexs.
By (iii) n=(4*100-1)/(4-1) = 399/3 = 133 vertices, the number who have seen the letter.
By (iii) i=(100-1)/(4-1) = 99/3 = 33 internal vertices, the number who sent out the letter.
Definition
Balanced m-ary tree if all leaf Vertexs are at levels h or h-1. Example
T1 is balanced, leafs at level 3 and 4.
T2 is not balanced, leafs at levels 2, 3, 4.
T3 is balanced, all leafs at level 3.
Definition
Height is maximum number of edges from tree root to lowest leaf.
Depth is number of edges a vertex is from the root.
Theorem 5
mh maximum leafs in m-ary tree of height h. Example
Following 2-ary tree of height 2 has 4 leafs.
mh = 22 = 4
Example
Full 4-ary tree of height 5 has 1024 leafs.
mh = 45 = 1024
Example
3-ary
HeightMaximum
leafs mh0 30 = 1 1 31 = 3 2 32 = 9 3 33 = 27
Corollary 1
h ³ élogm lù for m-ary tree of height h and l leafs. h = élogm lù for full and balanced m-ary tree of height h and l leafs.
Example
Below full, balanced 2-ary tree of height 2 has 4 leafs.
h = élog2 4ù = élog2 22ù = é2ù = 2
Example
4-ary tree of height 5 has 1024 leafs maximum.
h ³ élog4 1024ù = élog4 45ù = é5ù = 5
10.2 Applications of Trees
Binary Search Trees
Definition
Binary search tree totally orders vertices. Example
The following binary tree is totally ordered where all:
- left children are alphabetically less than the parent,
- right children are alphabetically greater than the parent.
Definition
Binary search tree constructed by adding vertices to maintain a total ordering. Example
Child vertices:
- > parent, add to right
- < parent, add to left
- = parent, don't add
Example
Add computer science to the right-bottom figure.
Example
Add oceanography to the figure using Algorithm 1.
Algorithm 1 Locating and Adding Items to a Binary Search Tree procedure insertion( T : binary search tree, x : item)
v := root of T
while v ¹ null and label(v) ¹ null
if x < label( v ) then
if left child of v ¹ null
then v := left child of v
else add new vertex as left child of v
v := null
else
if right child of v ¹ null
then v := right child of v
else add new vertex as right child of v
v := null
if root of T = null then add a vertex v to tree and label it with x
else
if v is null or label(v) ¹ x thenlabel new vertex with x and let v be the new vertex
Decision Trees
Example
8 coins, 1 counterfeit of different weight than other 7 coins.
Same number of coins on balance scale weight either:
left < right, left = right, left > right
3-ary decision.
Best case possible is 2 weightings below:
Weight 1,2,3 and 4,5,6: left = right
Weight 7 and 8: either left < right or left > right
8 possible outcomes, 8 leafs
coin in leaf 1,2, 4, 5, 7, 8 is counterfeit.
By Corollary 1:
h ³ élog3 8ù @ é1.89ù = 2
so the decision tree height is at least 2, agreeing with the above figure.
Corollary 1
h ³ élogm lù for m-ary tree of height h and l leafs. h = élogm lù for full and balanced m-ary tree of height h and l leafs.
Example
Sorting a, b, c.
P(3,3) = 3! = 6 ways to arrange 3 values
a<b<c a<c<b b<a<c b<c<a c<a<b c<b<a 6 leaf Vertexs
Decision tree for sorting compares 2 values at a time (binary-comparison sort).
2-ary tree.
left > right left < right Sorting n has n! outcomes (leafs).
Number of comparisons (tree height), by Corollary 1: h ³ élog2 n!ù
élog2 3!ù = élog2 6ù @ é2.33ù = 3
so the height is at least 3, agreeing with the below figure.
élog2 n!ù is Q(n log2 n)
Graph illustrates that log n! is O(n log n)
Means that a binary-comparison sort is Q(n log2 n)
Example - Sorting
Universal Address Systems
Traverse tree in lexicographic (dictionary or alphabetical) order:
0<1<1.1<1.2<1.3<2<3<3.1<3.1.1<3.1.2.1<3.1.2.2<3.1.1.3<3.1.2.4<3.1.2<3.1.3<3.2<4<4.1<5<5.1<5.1.1<5.2<5.3
Traversal Algorithms
Example
Trees are naturally recursive, each subtree is a tree.
Tree algorithms are recursive, when visiting subtrees.
The following lists first, then visits only left subtrees.
Order listed is: a, b, e, j
Algorithm 0.1 Preorder Left Subtree Traversal procedure preLeft( T: ordered rooted tree )
if T = null then return
r := root of T
list r
T( c ) := left subtree with c as root
preLeft( T( c ) )
The following first visits only left subtrees, then lists.
Order listed is: j,e,b,a
Algorithm 0.2 Postorder Left Subtree Traversal procedure postLeft( T: ordered rooted tree )
if T = null then return
r := root of T
T( c ) := left subtree with c as root
postLeft( T( c ) )
list r
Definition 1
Preorder traversal of tree T rooted at r with left-to-right subtrees T1, T1,..., Tn.
- Visit r.
- Visit T1 in preorder.
- Visit T2 in preorder.
n. Visit Tn in preorder.
Example
Preorder traversal of T visits vertices in the order of:
a,b,e,j,k,n,o,p,f,c,d,g,l,m,h,i
|
![]() |
Definition 1
Preorder traversal of tree T rooted at r with left-to-right subtrees T1, T1,..., Tn.
- Visit r.
- Visit T1 in preorder.
- Visit T2 in preorder.
n+1. Visit Tn in preorder.
Definition 2
Inorder traversal of tree T rooted at r with left-to-right subtrees T1, T1,..., Tn.
- Visit T1 inorder.
- Visit r.
- Visit T2 inorder.
n+1. Visit Tn inorder.
Inorder traversal of T visits vertices in the order of:
j,e,n,k,o,p,b,f,a,c,l,g,m,h,d,h,i
|
![]() |
Definition 3
Postorder traversal of tree T rooted at r with left-to-right subtrees T1, T1,..., Tn.
- Visit T1 postorder.
- Visit T2 postorder.
n. Visit Tn postorder.
n+1. Visit r.
|
![]() |
Postorder traversal of T visits vertices in the order of:
j,n,o,p,k,e,f,b,c,l,m,g,h,i,d,a
Example
Lexiographic ordering of "newyork" in a binary-search tree.
n
/ \
e w
\ / \
k o y
\
r
void Preorder() {
System.out.print( key ); // nekwory
if( left != null) left.Preorder( );
if( right != null) right.Preorder( );
}void Inorder() {
if( left != null) left.Inorder( );
System.out.print( key ); // eknorwy
if( right != null) right.Inorder( );
}void Postorder() {
if( left != null) left.Postorder( );
if( right != null) right.Postorder( );
System.out.print( key ); // keroywn
}Java Preorder/Inorder/Postorder construction and traversal of binary tree
class Vertex {
Vertex p, left, right;
char key;
Vertex (char key, Vertex p, Vertex left, Vertex right) {
this.key=key; this.p = p;
this.left = left; this.right = right;
}
void Preorder() { // Traverse preorder
System.out.println( key );
if( left != null) left.Preorder( );
if( right != null) right.Preorder( );
}
void Inorder() { // Traverse inorder
if( left != null) left.Inorder( );
System.out.println( key );
if( right != null) right.Inorder( );
}void Postorder() { // Traverse postorder
if( left != null) left.Postorder( );
if( right != null) right.Postorder( );
System.out.println( key );
}void Insert(char key) { // Insert new Vertex
if( key <= this.key)
if( left != null ) left.Insert( key );
else left = new Vertex(key, this, null, null);
else
if( right != null ) right.Insert( key );
else right = new Vertex(key, this, null, null);
}
}
public class VertexFun {
public static void main(String a[]) {
Vertex root = new Vertex( 'n', null, null, null);
root.Insert( 'e' );
root.Insert( 'w' );
root.Insert( 'y' );
root.Insert( 'o' );
root.Insert( 'r' );
root.Insert( 'k' );
root.Inorder();
}
}
Infix, Prefix, Postfix Notation
Binary tree representing ((x+y) ^2) + ((x-4)/3)
Preorder: + ^ + x y 2 / - x 4 3
Inorder: x + y ^ 2 + x - 4 / 3
Postorder: x y + 2 ^ x 4 - 3 / +
RPN - Reverse Polish Notation
RPN can be evaluated by a simple stack machine.
The following stack algorithm evaluates an expression in RPN.
Evaluate the RPN: 1 2 3 * - 4 +
Try the Java applet to enter expressions in the form specified in the expression grammar, parse, generate RPN, then evaluate the RPN. Source code for the applet is available.
10.4 Spanning Trees
Definition 1
Spanning tree of simple graph G is subgraph of G containing every vertex of G. Example 1
Simple graph not necessarily a tree, below has circuits.
Spanning tree construction from the simple graph
Theorem 1
Connected simple graph iff it has a spanning tree. Proof
Show G connected
- Assume simple graph G has spanning tree T.
- T contains every vertex of G.
- T is a subgraph of G, therefore is a path between every two vertices of T.
- Since T is a spanning tree of G and a subgraph of G, G is connected.
Show spanning tree
- Assume G is connected.
- If G is not a tree, contains simple circuit.
- Remove an edge from circuit.
- Result still contains all vertices of G.
- Still connected.
- Go to 2.
- G is a connected tree, no simple circuits remain.
Example
Several spanning trees
Example 2
IP Multicasting
Depth-First Search
|
Example
|
Breadth-First Search
|
Example
|