Data Str & Algorithm HW Solutions 41

Data Str & Algorithm HW Solutions 41 - temp =

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
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);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: temp = temp-&gt;right_sibling(); } return count; } 6.5 The Weighted Union Rule requires that when two parent-pointer trees are merged, the smaller ones root becomes a child of the larger ones 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 f nal root the weight of the new subtree. 6.6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15-1 6 9 12 6.7 The resulting tree should have the following structure:...
View Full Document

Ask a homework question - tutors are online