Tutorials Week 11
Tutorials Week 11
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.
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