11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

216 Chap. 6 Non-Binary Treeschild value B, <strong>and</strong> right child value A. Make the algorithm as efficient as youcan. Analyze your algorithm’s running time. How much harder would it beto make this algorithm work on a general tree?6.3 Write a postorder traversal function for general trees, similar to the preordertraversal function named preorder given in Section 6.1.2.6.4 Write a function th<strong>at</strong> takes as input a general tree <strong>and</strong> returns the number ofnodes in th<strong>at</strong> tree. Write your function to use the GenTree <strong>and</strong> GTNodeADTs of Figure 6.2.6.5 Describe how to implement the weighted union rule efficiently. In particular,describe wh<strong>at</strong> inform<strong>at</strong>ion must be stored with each node <strong>and</strong> how this inform<strong>at</strong>ionis upd<strong>at</strong>ed when two trees are merged. Modify the implement<strong>at</strong>ion ofFigure 6.4 to support the weighted union rule.6.6 A potential altern<strong>at</strong>ive to the weighted union rule for combining two trees isthe height union rule. The height union rule requires th<strong>at</strong> the root of the treewith gre<strong>at</strong>er height become the root of the union. Explain why the heightunion rule can lead to worse average time behavior than the weighted unionrule.6.7 Using the weighted union rule <strong>and</strong> p<strong>at</strong>h compression, show the array forthe parent pointer implement<strong>at</strong>ion th<strong>at</strong> results from the following series ofequivalences on a set of objects indexed by the values 0 through 15. Initially,each element in the set should be in a separ<strong>at</strong>e equivalence class. When twotrees to be merged are the same size, make the root with gre<strong>at</strong>er index valuebe the child of the root with lesser index value.(0, 2) (1, 2) (3, 4) (3, 1) (3, 5) (9, 11) (12, 14) (3, 9) (4, 14) (6, 7) (8, 10) (8, 7)(7, 0) (10, 15) (10, 13)6.8 Using the weighted union rule <strong>and</strong> p<strong>at</strong>h compression, show the array forthe parent pointer implement<strong>at</strong>ion th<strong>at</strong> results from the following series ofequivalences on a set of objects indexed by the values 0 through 15. Initially,each element in the set should be in a separ<strong>at</strong>e equivalence class. When twotrees to be merged are the same size, make the root with gre<strong>at</strong>er index valuebe the child of the root with lesser index value.(2, 3) (4, 5) (6, 5) (3, 5) (1, 0) (7, 8) (1, 8) (3, 8) (9, 10) (11, 14) (11, 10)(12, 13) (11, 13) (14, 1)6.9 Devise a series of equivalence st<strong>at</strong>ements for a collection of sixteen itemsth<strong>at</strong> yields a tree of height 5 when both the weighted union rule <strong>and</strong> p<strong>at</strong>hcompression are used. Wh<strong>at</strong> is the total number of parent pointers followedto perform this series?6.10 One altern<strong>at</strong>ive to p<strong>at</strong>h compression th<strong>at</strong> gives similar performance gainsis called p<strong>at</strong>h halving. In p<strong>at</strong>h halving, when the p<strong>at</strong>h is traversed fromthe node to the root, we make the gr<strong>and</strong>parent of every other node i on the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!