{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Data Structures &amp; Alogs HW_Part_11

# Data Structures &amp; Alogs HW_Part_11 - 41...

This preview shows pages 1–2. Sign up to view the full content.

41 if (t1->val() != t2->val()) return false; if (Compare2(t1->leftchild(), t2->leftchild()) if (Compare2(t1->rightchild(), t2->rightchild()) return true; if (Compare2(t1->leftchild(), t2->rightchild()) if (Compare2(t1->rightchild(), t2->leftchild)) return true; return false; } 6.3 template <class Elem> // Print, postorder traversal void postprint(GTNode<Elem>* subroot) { for (GTNode<Elem>* temp = subroot->leftmost_child(); temp != NULL; temp = temp->right_sibling()) postprint(temp); if (subroot->isLeaf()) cout << "Leaf: "; else cout << "Internal: "; cout << subroot->value() << "\n"; } 6.4 template <class Elem> // Count the number of nodes int gencount(GTNode<Elem>* subroot) { if (subroot == NULL) return 0 int count = 1; GTNode<Elem>* temp = rt->leftmost_child(); while (temp != NULL) { count += gencount(temp); temp = temp->right_sibling(); } return count; } 6.5 The Weighted Union Rule requires that when two parent-pointer trees are merged, the smaller one’s root becomes a child of the larger one’s root. Thus, we need to keep track of the number of nodes in a tree. To do so, modify the node array to store an integer value with each node. Initially, each node is in its own tree, so the weights for each node begin as 1. Whenever we wish to merge two trees, check the weights of the roots to determine which has more nodes. Then, add to the weight of the fi nal root the weight of the new subtree.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern