Trees
Trees
49
And v3e4v4e5v~ is a path from 1'3 to v~. We call 1'3e41'4e5v~ a directed path since
the edges e4 and es have the forward direction. (But 1'je~V3e3v~ is not a directed
path as e~ is in the forward direction and e3 is in the backward direction.)
2.2.2
TREES
Fig. 2.6
Fig. 2.7
50
;!
Property 1
Property 2
vertices.
In a tree there is one and only one path between every pair of
Property 3 If in a graph there is a unique (i.e. one and only one) path
between every pair of vertices, then the graph is a tree.
Property 4
Property 5
a tree.
Property 6
it is a tree.
11 -
1 edges.
11 -
1 edges, then it is
11 -
1 edges,tben
T 1: There is one vertex called the root of the tree which is distinguished
from all the other vertices and the root has no predecessors.
T:: There is a directed path from the root to every other vertex.
T3 : Every ve11ex except the root has exactly one predecessor.
T4 : The successors of each vertex are ordered 'from the left'.
Note:
Figure 2.7 is an ordered tree with VI as the root. Figure 2.8 also gives an
ordered directed tree with V1 as the root. In this figure the successors of 1'1 are
ordered as 1':1'3' The successors of 1'3 are ordered as 1'51'6'
!\!\
v,
v4
Fig. 2.8
J;1
51
By adopting the following convention, we can simplify Fig. 2.8. The root
is at the top. The directed edges are represented by arrows pointing downwards.
As all the arrows point downwards, the directed edges can be simply
represented by lines sloping downwards, as illustrated in Fig. 2.9.
Fig. 2.9
Note: An ordered directed tree is connected (which follows from T2). It has
no circuits (because of T3 ). Hence an ordered directed tree is a tree (see
Definition 2.17).
As we use only the ordered directed trees in applications to grammars, we
refer to ordered directed trees as simply trees.
Defmition 2.19 A binary tree is a tree in which the degree of the root is 2 and
the remaining vertices are of degree 1 or 3-
Note: In a binary tree any vertex has at most two successors. For example, the
trees given by Figs. 2.11 and 2.12 are binary trees. The tree given by Fig. 2.9
is not a binary tree.
Theorem 2.5
Proof Let n be the number of vertices. The root is of degree 2 and the
remaining n - 1 vertices are of odd degree (by Definition 2.19). By
Theorem 2.4, n - 1 is even and hence 11 is odd. I
52
Figure 2.10. for example. gives a tree where the levels of vertices are
indicated.
Root
r--,
Level 0
\L""'~
Level :2
-0
cf-
Level 2 . : 0
Level 3
Fig. 2.10
EXAMPLE 2.15
For a binary tree T with n vertices. shO\v that the minimum possible height
is rlog=(n + 1) - n where r k 1 is the smallest integer 2 k. and the maximum
possible height is (n - 1)12.
Solution
In a binacy tree the root is at level O. As every vertex can have at most t\vo
successors. vve have at most two vertices at level 1. at most 4 vertices at level
2. etc. So the maximum number of vertices in a binary tree of height k is
1 + 2 + 2= + ... + i'. As T has n vertices. 1 + 2 + 2= + ... + 2k 2 11, i.e.
(2 k+1 - 1)/(2 -1) 2': 11. so k 2': log=(n + 1) - 1. As k is an integer, the smallest
possible value for k is log=(n + 1) - n Thus the minimum possible height
is r log=(n + 1) To get the maximum possible height. we proceed in a similar way. In
a binary tree we have the root at zero level and at least two vertices at level
1. 2, .... When T is of height k. we have at least 1 + 2 + ... + 2 (2 repeated
k times) vertices. So. 1 + 2k ~ n, i.e. k ~ (n - 1)/2. But, n is odd by
Theorem 2.4. So (n - 1)/2 is an integer. Hence the maximum possible value
for k is (11 - 1)/2.
EXAMPLE 2.16
When 11 = 9. the trees \vith minimum and maximum height are shown
in Figs. 2.11 and 2.12 respectively. The height of the tree in Fig. 2.11 is
!log.:'(9 + 1) - 11 = 3. For the tree in Fig. 2.12. the height = (9 - 1)/2 = 4.
);l,
53
+ 1)/2, where
/1 IS
Fig. 2.11
Fig. 2.12
EXAMPLE 2.1 7
Prove that the number of leaves in a binary tree Tis
the number of vertices.
(/1
Solution
Let in be the number of leaves in a tree with /1 vertices. The root is of degree
2 and the remaining /1 - in - 1 vertices are of degree 3. As T has /1 vertices,
it has /1 - 1 edges (by Property 4). As each edge is counted twice while
calculating the degrees of its end vertices. 2(/1 - 1) = the sum of degrees of all
vertices = 2 + m + 3(11 - In - 1). Solving for in. we get in = (/1 + 1)12.
EXAMPLE 2.18
For the tree shown in Fig. 2.13, answer the following questions:
(a) Which vertices are leaves and \vhich internal vertices?
S4
g,
(b)
(c)
(d)
(e)
10
Fig. 2.13
Solutions
(a) 10, 4, 9, 8, 6 are leaves. 1, 2. 3, 5, 7 are internal vertices.
(bi 7 and 8 are the sons of 5.
2.3
2:* denotes the set of all strings (including A, the empty string)
over the alphabet set 2:. That is, 2:+ = 2:* - {A}.
NOTATION:
2.3.1
OPERATIONS ON STRINGS