12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 6.7 Exercises 2276.2 Write an algorithm <strong>to</strong> determine if two binary trees are identical when theordering of the subtrees for a node is ignored. For example, if a tree has rootnode with value R, left child with value A <strong>and</strong> right child with value B, thiswould be considered identical <strong>to</strong> another tree with root node value R, leftchild 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 be<strong>to</strong> make this algorithm work on a general tree?6.3 Write a pos<strong>to</strong>rder traversal function for general trees, similar <strong>to</strong> the preordertraversal function named print given in Section 6.1.2.6.4 Write a function that takes as input a general tree <strong>and</strong> returns the number ofnodes in that tree. Write your function <strong>to</strong> use the GenTree <strong>and</strong> GTNodeADTs of Figure 6.2.6.5 Describe how <strong>to</strong> implement the weighted union rule efficiently. In particular,describe what information must be s<strong>to</strong>red with each node <strong>and</strong> how this informationis updated when two trees are merged. Modify the implementation ofFigure 6.4 <strong>to</strong> support the weighted union rule.6.6 A potential alternative <strong>to</strong> the weighted union rule for combining two trees isthe height union rule. The height union rule requires that the root of the treewith greater height become the root of the union. Explain why the heightunion rule can lead <strong>to</strong> worse average time behavior than the weighted unionrule.6.7 Using the weighted union rule <strong>and</strong> path compression, show the array forthe parent pointer implementation that 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 separate equivalence class. When twotrees <strong>to</strong> be merged are the same size, make the root with greater 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> path compression, show the array forthe parent pointer implementation that 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 separate equivalence class. When twotrees <strong>to</strong> be merged are the same size, make the root with greater 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)

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

Saved successfully!

Ooh no, something went wrong!