Data Str &amp; Algorithm HW Solutions 35

# Data Str & Algorithm HW Solutions 35 - void

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

35 x leaf nodes: 4 x 4 x +4 x =4 / 8 = 50% . 5.10 If equal valued nodes were allowed to appear in either subtree, then during a search for all nodes of a given value, whenever we encounter a node of that value the search would be required to search in both directions. 5.11 This tree is identical to the tree of Figure 5.20(a), except that a node with value 5 will be added as the right child of the node with value 2 . 5.12 This tree is identical to the tree of Figure 5.20(b), except that the value 24 replaces the value 7, and the leaf node that originally contained 24 is removed from the tree. 5.13 template <class Key, class Elem, class KEComp> int smallcount(BinNode<Elem>* root, Key K); if (root == NULL) return 0; if (KEComp.gt(root->value(), K)) return smallcount(root->leftchild(), K); else return smallcount(root->leftchild(), K) + smallcount(root->rightchild(), K) + 1; 5.14 template <class Key, class Elem, class KEComp>
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: void printRange(BinNode<Elem>* root, int low, int high) { if (root == NULL) return; if (KEComp.lt(high, root->val()) // all to left printRange(root->left(), low, high); else if (KEComp.gt(low, root->val())) // all to right printRange(root->right(), low, high); else { // Must process both children printRange(root->left(), low, high); PRINT(root->value()); printRange(root->right(), low, high); } } 5.15 The minimum number of elements is contained in the heap with a single node at depth h − 1 , for a total of 2 h − 1 nodes. The maximum number of elements is contained in the heap that has com-pletely f lled up level h − 1 , for a total of 2 h − 1 nodes. 5.16 The largest element could be at any leaf node. 5.17 The corresponding array will be in the following order (equivalent to level order for the heap): 12 9 10 5 4 1 8 7 3 2...
View Full Document

## This note was uploaded on 12/27/2011 for the course MAP 2302 taught by Professor Bell,d during the Fall '08 term at UNF.

Ask a homework question - tutors are online