Exercises on Trees
Total Page:16
File Type:pdf, Size:1020Kb
EXERCISES ON TREES
1. How many vertices does a full 5-ary tree with 100 internal vertices have?
2. How many edges does a full binary tree with 1000 internal vertices have?
3. How many leaves does a full 3-ary tree with 100 vertices have?
4. Build a binary search tree for the words banana, peach, apple, pear, coconut, mango, and papaya using alphabetical order.
5. Build a binary search tree for the words oenology, phrenology, campanology, ornithology, ichthyology, limnology, alchemy, and astrology using alphabetical order.
6. How many comparisons are needed to locate or to add each of these words in the search tree for Question 4, starting fresh each time? a) pear b) banana c) kumquat d) orange
7. How many comparisons are needed to locate or to add each of the words in the search tree for Question 5, starting fresh each time? a) palmistry b) etymology c) paleontology d) glaciology
8. Using alphabetical order, construct a binary search tree for the words in the sentence “The quick brown fox jumps over the lazy dog.”
9. Given the coding scheme a: 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.
10. Use Huffman coding to encode these symbols with given frequencies: a: 0.20, b: 0.10, c: 0.15, d: 0.25, e: 0.30. What is the average number of bits required to encode a character?
11. Use Huffman coding to encode these symbols with given frequencies: A: 0.10, B: 0.25, C: 0.05, D: 0.15, E: 0.30, F: 0.07, G: 0.08. What is the average number of bits required to encode a symbol? 12. Construct the universal address system for the given ordered rooted tree below. Then use this to order its vertices using the lexicographic order of their labels.
13. Determine the order in which a: a. preorder b. inorder c. postorder traversal visits the vertices of the given ordered rooted trees below. a. b.
14. a) Represent the expressions (x + xy) + (x/y) and x + ((xy + x)/y) using binary trees. Write these expressions in b) prefix notation. c) postfix notation. d) infix notation.
15. What is the value of each of these prefix expressions? a) − * 2 / 8 4 3 b) ↑ − * 3 3 * 4 2 5 c) + − ↑ 3 2 ↑ 2 3 / 6 − 4 2 d) * + 3 + 3 ↑ 3 + 3 3 3
16. What is the value of each of these postfix expressions? a) 5 2 1−−3 1 4 ++ * b) 9 3 / 5 + 7 2 − * c) 3 2 * 2 ↑ 5 3 − 8 4 / * −
17. How many edges must be removed from a connected graph with n vertices and m edges to produce a spanning tree? 18. Draw all the spanning trees of the given simple graphs below.
19. Use a. Depth-first search b. Breadth first search to produce a spanning tree for the simple graph below. Choose a as the root of this spanning tree and assume that the vertices are ordered alphabetically.
20. Use backtracking to find a subset, if it exists, of the set {27, 24, 19, 14, 11, 8} with sum a) 20. b) 41. c) 60.
21. Use a. Prim’s algorithm b. Kruskal’s algorithm to find a minimum spanning tree for the given weighted graph below.