Exercises from Chapter 6 6.1 6.4 6.5 6.7 6.9 6.12 Solutions 6.1 template bool Compare(GTNode* t1, GTNode* t2) { GTNode *c1, *c2; if (((t1 == NULL) && (t2 != NULL)) || ((t2 == NULL) && (t1 != NULL))) return false; if ((t1 == NULL) && (t2 == NULL)) return true; if (t1->val() != t2->val()) return false; c1 = t1->leftmost_child(); c2 = t2->leftmost_child(); while (!((c1 == NULL) && (c2 == NULL))) { if (!Compare(c1, c2)) return false; if (c1 != NULL) c1 = c1->right_sibling(); if (c2 != NULL) c2 = c2->right_sibling(); }} This algorithm has a worst case running time of Theta(n), where n is the total size of both trees. 6.4 template int gencount(GTNode* subroot) { if (subroot == NULL) return 0 int count = 1; GTNode* temp = rt->leftmost_child(); while (temp != NULL) { count += gencount(temp); temp = temp->right_sibling(); } return count; } 6.5 Each element in the node array contains three fields: value index of parent size of its subtree Initially, since all nodes belong to sets of size one, all the size fields are 1. When two trees are merged, the size fields of the two roots are compared. The smaller root becomes a child of the larger, and the size field of the larger is updated by adding the size field of the smaller root to it. 6.7 The array will contain 4 4 4 4 -1 4 4 0 0 4 9 9 9 12 9 -1 where -1 indicates a root. 6.9 Let P be the size of a pointer, D be the size of a data value, and I be the size of an index. In the lists of children implementation (Fig. 6.9), every node except the root occurs once in the node array and once in the linked list of children of some node. Therefore, all nodes except the root have overhead fraction 2I + 2P ___________ D + 2I + 2P For large trees, this is a good approximation to the overhead fraction of the entire tree. In the left child/right sibling implementation (Fig. 6.10), every node occurs exactly once in the node array. Therefore the overhead fraction is 3I ______ D + 3I In the first linked implementation (Fig. 6.12), let S be the size of the size field. Every node except the root is pointed to by one pointer. Therefore, all nodes except the root have overhead fraction S + P _________ D + S + P In the second linked implementation (Fig. 6.13), every data node except the root is associated with a pair of pointers, as shown in the figure. Also, every node has a pointer field. So every data node except the root is associated with a total of three pointers, giving overhead fraction 3P ______ D + 3P 6.12 (a) Ignoring the root, every node has overhead fraction 4(K + 1) K + 1 ____________ = _____ 4 + 4(K + 1) K + 2 (b) 4K K _______ = _____ 4K + 16 K + 4 (c) From the next exercise, there are about K-1 leaves for every internal node. For any internal node and any set of K-1 leaves, there are a total of 2K pointers and K data fields, so the overhead fraction is about 8K 1 ___ = _ 16K 2 Or, if you prefer to do it exactly, let n = number of internal nodes. Then the overhead fraction is n(4(K+1)) + ((K-1)n + 1)4 8Kn + 4 1 ______________________________ = _________ = _ (approx.) n(8 + 4(K+1)) + ((K-1)n + 1)12 16Kn + 12 2 (d) 2K K ______ = ______ 6K - 4 3K - 2 Or, if you prefer to do it exactly, let n = number of internal nodes. Then the overhead fraction is n(2K) 2Kn K _____________________ = ___________ = ______ (approx.) n(2K) + ((K-1)n + 1)4 (6K-4)n + 4 3K - 2