there are n! possible orderings of these
elements, because each of the n!
permutations of these elements can be
the correct order. The sorting
algorithms studied in this book, and
most commonly used sorting
algorithms, are based on binary
comparisons, tha
follows that U has n internal vertices.
We can now use part (ii) of Theorem 4
in Section 11.1 to conclude that U has n
+ 1 leaves. Using Corollary 1 of Section
11.1, we see that the height of U is
greater than or equal to h = log(n + 1).
Consequently, it
log n!is (n log n), one of the commonly
used reference functions for the
computational complexity of
algorithms. Corollary 1 is a
consequence of this estimate. P1: 1
CH11-7T Rosen-2311T MHIA017Rosen-v5.cls May 13, 2011 10:27 762
11 / Trees COROLLARY 1 The
as its label. For instance, the tree in
Figure 5 represents the encoding of e
by 0, a by 10, t by 110, n by 1110, and s
by 1111. 1 1 1 0 1 0 0 0 t s a e n FIGURE
5 A Binary Tree with a Prefix Code. The
tree representing a code can be used to
decode a bit
starting position. The usual convention
is to represent vertices at even levels
by boxes and vertices at odd levels by
circles. When the game is in a position
represented by a vertex at an even
level, it is the first players move; when
the game is in a po
10:27 11.2 Applications of Trees 759
ALGORITHM 1 Locating an Item in or
Adding an Item to a Binary Search
Tree. procedure insertion(T : binary
search tree, x: item) v := root of T cfw_a
vertex not present in T has the value
null while v = null and label(v
coding to encode these letters with the
given frequencies: A: 0.25, E: 0.30, N:
0.10, R: 0.05, T: 0.12, Z: 0.18. 30.
Consider the three symbols A, B, and C
with frequencies A: 0.80, B: 0.19, C:
0.01. a) Construct a Huffman code for
these three symbols. b)
= physics > oceanography, so we
examine the left child of v. This left
child exists, so we set v, the vertex
under examination, to this left child. At
this step, we also have v = null and
label(v) = metereology <
oceanography, so we try to examine the
rig
the leaf that is labeled with e.
Therefore, the original word is sane.
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
n! and note that a function that is (log
n!) is also (n log n) because log n! is (n
log n). THEOREM 2 The average
number of comparisons used by a
sorting algorithm to sort n elements
based on binary comparisons is (n log
n). Prefix Codes Consider the prob
labeling is called the universal address
system of the ordered rooted tree. We
can totally order the vertices using the
lexicographic ordering of their labels
in the universal address system. The
vertex labeled x1.x2. . . . .xn is less than
the vertex lab
the payoffs to the first player, assuming
that play starts at each of the positions
represented by these vertices. We need
to consider two cases, when it is the
first players turn and when it is the
second players turn. When it is the
first players turn,
instance, if e were encoded with 0, a
with 1, and t with 01, then the bit
string 0101 could correspond to eat,
tea, eaea, or tt. One way to ensure that
no bit string corresponds to more than
one sequence of letters is to encode
letters so that the bit str
of each vertex (see Exercises 26 and
27). In particular, both a preorder
traversal and a postorder traversal
encode the structure of a full ordered
m-ary tree. However, when the number
of children of vertices is not specified,
neither a preorder traversal
Visit r Postorder traversal FIGURE 7
Postorder Traversal. EXAMPLE 4 In
which order does a postorder traversal
visit the vertices of the ordered rooted
tree T shown in Figure 3? Solution: The
steps of the postorder traversal of the
ordered rooted tree T ar
001, b: 0001, e: 1, r: 0000, s: 0100, t:
011, x: 01010, find the word
represented by a) 01110100011. b)
0001110000. c) 0100101010. d)
01100101010. 23. Use Huffman coding
to encode these symbols with given
frequencies: a: 0.20, b: 0.10, c: 0.15, d:
0.25, e
payoff when play begins at the position
represented by this vertex. Remark: By
extending the proof of Theorem 3, it
can be shown that the minmax strategy
is the optimal strategy for both players.
Example 8 illustrates how the minmax
procedure works. It di
grandchildren does it have? 42. How
many children does the root of the
game tree for nim have and how many
grandchildren does it have if the
starting position is a) piles with four
and five stones, respectively. b) piles
with two, three, and four stones,
the steps used to encode these
symbols. The encoding produced
encodes A by 111, B by 110, C by 011, D
by 010, E by 10, and F by 00. The
average number of bits used to encode
a symbol using this encoding is 3 0.08
+ 3 0.10 + 3 0.12 + 3 0.15 + 2 0.20 +
2 0.
zoology, because zoology comes after
mathematics and after physics.
Similarly, add a left child of the vertex
with key physics and assign this new
vertex the key meteorology. Add a right
child of the vertex with key geography
and assign this new vertex th
tournament sort using pseudocode. 16.
Assuming that n, the number of
elements to be sorted, equals 2k for
some positive integer k, determine the
number of comparisons used by the
tournament sort to find the largest
element of the list using the
tournament
studied game. P1: 1 CH11-7T Rosen2311T MHIA017-Rosen-v5.cls May 13,
2011 10:27 766 11 / Trees 1 1 +1 +1
+1 +1 +1 +1 1 1 Terminal vertices
are labeled with +1 if the first player
wins 1 and 1 if the second player
wins 1 2 2 1 2 2 2 1 1 1 1 2 1 1 2 1 1 11
1
preorder, inorder, and postorder. To do
this, first draw a curve around the
ordered rooted tree starting at the
root, moving along the edges, as shown
in the example in Figure 9. We can list
the vertices in preorder by listing each
vertex the first time t
values of all vertices in a game tree in a
way that enables us to determine the
outcome of this game when both
players follow optimal strategies. By a
strategy we mean a set of rules that
tells a player how to select moves to
win the game. An optimal stra
at least one stone remains. Draw the
game tree for this variation of nim if
the starting position consists of three
piles containing two, two, and one
stone, respectively. Find the values of
each vertex in the game tree and
determine the winner if both pl
each code. Which tie-breaking
procedure produced the smaller
variance in the number of bits
required to encode a symbol? P1: 1
CH11-7T Rosen-2311T MHIA017Rosen-v5.cls May 13, 2011 10:27 11.2
Applications of Trees 771 27. Construct
a Huffman code for the l
dfhinopkjebfacgidlm
Postorder traversal: Visit subtrees left
to right; visit root b c h T a i a m
FIGURE 8 The Postorder Traversal of
T . a b d hi j k e f g c FIGURE 9 A
Shortcut for Traversing an Ordered
Rooted Tree in Preorder, Inorder, and
Postorder. P
when it is not feasible to compute
these values exactly. For example, in
the game of tic-tac-toe, as an
evaluation function for a position, we
may use the number of files (rows,
columns, and diagonals) containing no
Os (used to indicate moves of the
secon
7T Rosen-2311T MHIA017-Rosenv5.cls May 13, 2011 10:27 11.3 Tree
Traversal 775 r T1 T2 Tn Step 1: Visit
T1 in inorder Step 3: Visit T2 in
inorder Step n + 1: Visit Tn in inorder
Step 2: Visit r Inorder traversal
FIGURE 5 Inorder Traversal. The
preorder l
tree for Exercise 1, starting fresh each
time? a) pear b) banana c) kumquat d)
orange 4. How many comparisons are
needed to locate or to add each of the
words in the search tree for Exercise 2,
starting fresh each time? a) palmistry
b) etymology c) paleon