0% found this document useful (0 votes)
4 views2 pages

Tutorials Week 11

The document discusses various tree traversal methods including preorder, inorder, and postorder traversals, along with algorithms for each. It also addresses the representation of expressions in prefix, infix, and postfix forms, and includes exercises for practice. Additionally, it covers topics related to binary trees, Huffman coding, and decision trees for identifying coins.

Uploaded by

jayden19012004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Tutorials Week 11

The document discusses various tree traversal methods including preorder, inorder, and postorder traversals, along with algorithms for each. It also addresses the representation of expressions in prefix, infix, and postfix forms, and includes exercises for practice. Additionally, it covers topics related to binary trees, Huffman coding, and decision trees for identifying coins.

Uploaded by

jayden19012004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

2. Give an algorithm to execute a preorder traversal.

3. What is inorder traversal?


4. Give an algorithm to execute an inorder traversal.
5. What is postorder traversal?
6. Give an algorithm to execute a postorder traversal.
7. What is the prefix form of an expression?
MATH2081 | MATH2328: Math for Computing

1 name of the prefix form of
8. What is an alternative
Lecturers: Dr Nguyen Hieu Thao & Dr Jeff Nijsse expression?

Email: [email protected]
9.6 Exercises
In Exercises 1–5, list the order in which the vertices are process
476 Chapter 9 ◆ Trees using preorder, inorder, and postorder traversal.

Tutorial Exercises: Trees


1. 2.
9.6 Review Exercises A A

B C
1. What is preorder traversal? 9. What is the infix form of an expression?
2.
(2024C, Week 11)1
Give an algorithm to execute a preorder traversal.
B C
10. What is the postfix form of an expression?
D E
3. What is inorder traversal? H D
11. What is an alternative name of the postfix form of an

4. Give an algorithm to execute an inorder traversal.
expression?
1. Place the words 5. What is postorder traversal?
- FOUR, SCORE, AND, SEVEN, YEARS, AGO, OUR, FOREFATHERS, I J
12. What advantages do prefix and postfix forms of expressions
E
6. Give an algorithm to execute a postorder traversal.
BROUGHT, FORTH 7. What-isin
thethe order
prefix inanwhich
form of they appear, in a binaryhave
expression? search tree.
over the infix form?
K F
8. What is an alternative name of the prefix form of an 13. Explain how a tree can be used to represent an expression.
2. Draw a full binaryexpression?
tree with four internal vertices and five terminal vertices.
L M
3. 4.
9.6 Exercises
3. List the order in which the vertices are processed using A A
In Exercises 1–5, list the order in which the vertices are processed In Exercises 6–10, represent the expression as a binary tree and
preorder, inorder and
using postorder
preorder, inorder, traversal.
and postorder traversal. write the prefix and
B postfix forms of the expression. B
1. 2. 6. (A + B) ∗ (C − D)
A A
7. (A ∗ B +CC ∗ D) −D(A/B − (D + E)) C

B C 8. ((A − C) ∗ D) / (A + (B + D))
B C
9. (((A + B) ∗ C E
+ D) ∗ E) − ((A + B) ∗ C − D)D
D E 10. (A ∗ B − C/D + E) + (A − B − C − D ∗ D)/(A + B + C)
(a) H D F E
(b)
In Exercises 11–15, represent the postfix expression as a binary
❦ tree and
5. write the prefix form, the usual infix form, and the fully
I J E parenthesized infix form ofAthe expression.
4. Represent the expression as a binary tree and write the prefix and postfix forms of the
11. AB+C− 12. ABCD+∗/E−
K F G B E
expression. 13. ABC+− 14. ABC∗∗CDE+/−
15. AB+CD∗EF/−−A∗
L M C F
(a) (A + B) ∗ (C − D) (b) ((A − C) ∗ D) / (AIn+Exercises
(B + D))16–21, find the value of the postfix expression if A = 1,
3. 4. B = 2, C = 3, and D = 4.
A A D G
5. Represent the postfix expression as a binary tree and write the prefix form, the usual infix 17. AB+CD∗AA/−−B∗
16. ABC+−
18. AB+C− 19. ABC∗∗ABC++−
B
form, and the fully parenthesized
B infix form of the expression. 20. ADBCD∗−+∗ 21. ABAB∗+∗D∗
22. Show, by example, that distinct binary trees with vertices A,
C D C
(a) AB + C− (b) ABCD + ∗/E− B, and C can have the same preorder listing ABC.
23. Show that there is a unique binary tree with six vertices whose
E D preorder vertex listing is ABCEFD and whose inorder vertex
6. Find the value of the postfix expression if A = 1, B = 2, and C = 3. listing is ACFEBD.
F E ⋆24. Write an algorithm that reconstructs the binary tree given its
(a) ABC + − (b) ABC ∗ ∗ABC + +− preorder and inorder vertex orderings.
5. 25. Give examples of distinct binary trees, B1 and B2 , each with
A
two vertices, with the preorder vertex listing of B1 equal to the
7. Show, by example, that distinct binary trees with vertices A, B, and Cpreorder
can have
listing the
of B2same
and the postorder vertex listing of B1
B E equal to the postorder listing of B2 .
preorder listing ABC. 26. Let P1 and P2 be permutations of ABCDEF. Is there a binary
C F tree with vertices A, B, C, D, E, and F whose preorder listing
1 Most of the content of this document is taken from the book [1]. is P1 and whose inorder listing is P2 ? Explain.
D G 27. Write a recursive algorithm that prints the contents of the ter-
minal vertices of a binary tree from left to right.


5. Give an example of a hierarchical definition tree.

9.1 Exercises
Which of the graphs in Exercises 1–4 are trees? Explain. 9. Find the height of the tree of Exercise 8.
10. Draw the tree T of Figure 9.1.5 as a rooted tree with a as root.
1. 2.
2 What is the height of the resulting tree?
11. Draw the tree T of Figure 9.1.5 as a rooted tree with b as root.

❦ towith
What is the height of the resulting tree?
8. Give examples of distinct binary trees, B1 and B2 , each with two vertices,
12. Give an example similar
the preorder
Example 9.1.5 of a tree that is used
vertex
3. listing of B1 equal 4.to the preorder listing of B2to and
specify hierarchical relationships.
the postorder vertex listing of
13. Give an example different from Example 9.1.7 of a hierarchical
B1 equal to the postorder listing of B2 . definition tree.
Decode each bit string using the Huffman code given.

5. For which values of n is the complete graph on n vertices a tree? 9.2 ◆ Terminology and Characteriza
1 0
9. 6.
Consider
For whichthe Huffman
values of m and ncode beside.bipartite graph on
is the complete
m and n vertices a tree? each word using the preceding Huffman code.
Encode 29. Professor
1 0 1 0 Ter A. Byte needs to
7. For which values of n is the n-cube a tree?
(a) Encode 18. the DEN
text string N19. ADAL.
LEADEN 1
characters A, B, C, D, E, which
0
8. Find the level of each vertex in the tree shown. S A E
frequencies:
20. NEED 21. PENNED
1 0
(b) Decode 22. the What
bit string
factors
i
1110011101001111.
in addition to the amount of memory used should
N
b 1 0
be considered when choosing a code, such as ASCII or a P
Root d Character Frequency C
a Huffman j
code, to represent characters in a computer? D L
k e A 6
23.c What techniques in haddition to the use 14. of Huffman codes
011000010 15. 01111001001110
B 2
might befused to save memory when storing text?
16. 01110100110 17. 1110011101001111
10. Construct an optimal Huffman gcode for the set of characters in the following table.
C Is it 3
24. Construct an optimal Huffman code for the set of letters in the
the unique optimal Huffman code? Justify your answer.
table.
Professor Byte suggests using the

Letter Frequency Letter Frequency


❦ Character
α 5 δ 11
β 6 ε 20 A
γ 6 B
C
D
25. Construct an optimal Huffman code for the set of letters in the E
11. Four coins are identical
table.
in appearance, but one coin is either heavier or lighter than the
others, which all weigh the same. Draw a decision tree that gives an algorithm that identifies
which, he argues, store the text in l
an optimal Huffman code. Is the p
in at most two weighings the bad coin (but not necessarily determines whether it is heavier
Letter Frequency Letter Frequency 30. Show that a tree is a planar graph.
or lighter than the others) using only a pan balance. 31. Show that any tree with two or mo
I 7.5 C 5.0

❦12. Eight coins are identical


U 20.0 H 10.0 degree 1.
B in appearance,
2.5 butMone coin is 2.5 either heavier 32.
or Show
lighterthatthan
a tree is a bipartite grap
S 27.5 P 25.0 33.
the others, which all weigh the same. Draw a decision tree that gives an algorithm thatvertices of a tree can
Show that the
so that each edge is incident on ve
identifies in at26.most
Use three weighings
the code developedthe bad coin
in Exercise 25 and determines
to encode whether it is heavier or
the follow-
The eccentricity of a vertex v in a tree T
ing words (which have frequencies consistent with the table
lighter than the others using only
of Exercise 25): a pan balance. a simple path that begins at v.

BUS, CUPS, MUSH, PUSS, SIP, PUSH,


34. Find the eccentricity of each vertex
13. Show that there is a unique binary tree with five vertices A, B, C, D, E that has preorder
CUSS, HIP, PUP, PUPS, HIPS. A vertex v in a tree T is a center for T
traversal ABCDE and inorder traversal ADECB. minimal.
27. Construct two optimal Huffman coding trees for the table of
Exercise 24 of different heights. 35. Find the center(s) of the tree of Fig
References ⋆36. Show that a tree has either one or
28. Construct an optimal Huffman code for the set of letters in the
table. ⋆37. Show that if a tree has two centers
1. Johnsonbaugh, R.: Discrete Mathematics - Eighth Edition. Pearson Education, New York
38. Define the radius r of a tree using t
(2018).
Letter Frequency Letter Frequency and center. The diameter d of any
Exercise 71, Section 8.2. Is it alw
a 2 d 8 definition of radius, that 2r = d? E
b 3 e 13
c 5 f 21 39. Give an example of a tree T that d
ing property: If v and w are vertices
from v to w.

9.2 Terminology and Characterizatio


A portion of the family tree of the ancient Greek gods is shown
children are listed.) As shown, we can regard a family tree as a

You might also like