Transcript dmi18.ppt
5.5.3 Rooted tree and binary tree
Definition 25: A directed graph is a directed
tree if the graph is a tree in the underlying
undirected graph.
Definition 26: A rooted tree is a directed tree if
there are exactly a vertex that is 0 in-degree,
and other vertices that are 1 in-degree. The
vertex of 0 in-degree is called root. And the
vertices of 0 out-degree are called leaves. The
vertices that are not 0 out-degree are called
internal vertices.
There is a unique path from the root to each
vertex of the rooted tree by the definition 26
Definition 27: Let u be an internal vertex.
If there is a directed edge (u,w) from u to
w, then w is called child of u, and u is
called the parent of w. If the vertices w1
and w2 are child of u, then w1 and w2 are
called brothers. If there a directed path
from u to z, then z is called descendant of
u. and u is called ancestors of w. The level
of a vertex v is the length of the unique
path from the root to this vertex. The
level of the root is defined to be zero. The
height of a rooted tree is the maximum of
the levels of all vertices.
Note: The parent of w is unique.
Root:1
Leaf: 6, 8, 9, 10, 11, 12
internal vertices: 1, 2, 3, 4, 5, 7
the levels of 2, 3 are 1, the levels of 4, 5, 6, 7, 8 are 2 ,
the levels of 9, 10, 11, 12 are 3 。
The height of a rooted tree is 3
Definition 28: If v be a vertex of a rooted
tree, then the subtree with v as its root is
the subgraph of the tree consisting of v and
its descendants and all edges incident to
these descendants.
Definition 29: An ordered rooted tree is a
rooted tree where the children of each
internal vertex are ordered. Ordered rooted
trees are drawn so that the children of each
internal vertex and edges are shown in order
from left to right, and these edges are
marked on 1,2,,i,
Definition 30: An ordered 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 vertex has
exactly m children. An m-ary tree with
m=2 is called a binary tree.
Left subtree, right subtree
a-(b+(c/d)+(e/f))
Theorem 5.19: A full binary tree with t
leaves contains i=t-1 internal vertices.
Proof: Let the number vertices be n.
The sum of children of all internal
vertices equals n-1,
internal vertices and leaves
the number vertices n=i+t,
i.e. 2i=i+t-1,
Thus i=t-1。
A full m-ary tree with t leaves contains
i=(t-1)/(m-1) internal vertices.
Theorem 5.20: Let T be a full binary tree. We
denote the sum of length of simple paths from root
to all internal vertices by I, and the sum of length
of simple paths from root to all leaves by E. Then
E=I+2i, where i is the number of internal vertices.
Proof: Let us apply induction on the number i of
internal vertices.
E=2 and I=0 when i=1
Suppose that result holds for i=k-1
For i=k, we chose internal vertex v so that its
children are leaves. We have a new tree which is
obtained by omitting edges of incident v and its
children.
We denote the length of path from root to v by l.
E'=E- l-2, I'=I-l.
By the inductive hypothesis, E'=I'+2(k-1)
E= E'+l+2=I'+2(k-1)+l+2=I-l+2(k-1)+l+2=I+2k。
Let T be a full m-ary tree. Then E=(m-1)I+mi, where
i is the number of internal vertices.
5.6 Prefix codes and optimal tree
a b c d e
00 110 010 10 01
The set {00,110,010,10,01} is called code
010010
ead or cc?
The string of e is prefix of string of c
c: 111
The set {00,110,111,10,01} is called prefix code
Definition 31: Codes with this property which
the bit string for a letter never occurs as the
first part of the bit string for another letter are
called prefix codes.
Theorem 5.21: We can construct a prefix code
from any binary tree, and we can construct a
binary tree from the prefix codes.
Proof: (1) We can construct a prefix code from
any binary tree where the left edge at each
internal vertex is labeled by 0 and the right
edge by a 1 and where the leaves are labeled by
characters
(2)We can construct a binary tree from the
prefix codes
字母
频率
字母
频率
a
b
c
d
e
f
g
h
i
j
k
l
m
0.0356
0.0139
0.0279
0.0378
0.1304
0.0289
0.0199
0.0528
0.0627
0.0013
0.042
0.0339
0.0249
n
o
p
q
r
s
t
u
V
w
x
y
z
0.0707
0.0797
0.0199
0.0012
0.0677
0.0607
0.1045
0.0249
0.0092
0.0149
0.0017
0.0199
0.0008
Definition 32: Let T be a tree with weigths w1w2...wn
at its leaf nodes. The weighted leaf path length w(T) of
n
T is W(T)= wi li , where li is the path length from the
i 1
root to vertex i. Say that a binary tree T is optimal if
w(T) has its minimum value over all possible trees with
the same set of leaf nodes.
Example: Let T be a binary tree with weigths 3, 5, 7, 9
a) wi li 48,
b) wi li 47,
Huffman algorithm:
Let a1,a2,,an be n vertex with weight w1,w2,,wn and
w1w2wn。
F:=forest of n rooted tree each consisting of the single vertex
ai with weight wi for i=1,2,...n.
While F is not a tree
Begin
Replace the rooted trees T and T’ of least weights from F
with w(T)≥ w(T’) with a tree having a new root that has T as
its left subtree and T’ as its right subtree. Assign w(T)+w(T’)
as the weight of the new tree.
end
Example: Find a optimal tree with weight
2,4,7,8,10,12
w(T)=2*4+4*4+7*3+
12*2+8*2+10*2=105
Theorem 5.22: Let T be built according to Huffman
algorithm and leaves of T with weight w1,w2,,wn.
Then T is an optimal tree.
Proof: Let us apply induction on the number n of
vertices.
n=2,
The results holds
Suppose that result holds for n=k-1
For n=k,
By the inductive hypothesis,
Suppose that nodes in an optimal tree T have weights
w1+w2,w3,,wk. Then This is an optimal tree with weight
w1,w2,w3,,wk if T’s leaf with weight w1+w2 is replaced
by subtree
Quiz:
1.Let G be a tree with two or more vertices.
Then G is a bipartite graph.
2.Let G be a simple graph with n vertices.
Show that ifδ(G) >[n/2]-1, then G is a tree or
contains three spanning trees at least.
Theorem 5.22,
Transport Networks, 8.4 P307
Exercise: P259 23
1. Let T be a full m-ary tree. We denote the sum of length
of simple paths from root to all internal vertices by I, and
the sum of length of simple paths from root to all leaves
by E. Then E=(m-1)I+2i, (m-1)i=t-1, where i is the
number of internal vertices and t is the number of leaves
2.(1)Find a optimal binary tree with weight 1,3,8,9,12,
15,16
(2)Construct a algorithms for optimal 3-ary tree
(3)Find a optimal 3-ary tree with weight 1,2,3,4,5,6,7,8,9
(4)Find a optimal 3-ary tree with weight 1,2,3,4,5,6,7,8,