{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CSC375 Homework7 George Corser 2009-03-09

# CSC375 Homework7 George Corser 2009-03-09 - CSC 375...

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

CSC 375 Homework 7 George Corser 2009 March 9 5.5 Recursive function to search for a value K within a binary tree. template <class Key, class Elem, class KEComp> // given in the problem bool search(BinNode<Elem>* subroot, Key K); bool retval FALSE; // initialize return value if (subroot == NULL) // return FALSE if empty return retval; if (subroot->value() == K) { // return TRUE if found retval = TRUE; // no need to search further return retval; } retval = search(subroot->left()); // recurse left if retval == TRUE return retval; // return TRUE if found retval = search(subroot->right()); // recurse right if retval == TRUE return retval; // return TRUE if found retval = FALSE; // return FALSE if not found return retval; } 5.7 Recursive function to return the height of a binary tree. template <class Elem> int heightof(BinNode<Elem>* subroot) { int leftheight = 0; int rightheight = 0; if (subroot == NULL) // return 0 if empty tree return 0; leftheight = heightof(subroot->left()); // recurse left rightheight = heightof(subroot->right()); // recurse right if (leftheight >= rightheight) // return larger height return leftheight + 1; // (+ 1 for this node) else return rightheight + 1; } 5.8 Recursive function to return the number of leaf nodes in a binary tree. template <class Elem> int leafsof(BinNode<Elem>* subroot) { int leftleafs = 0; int rightleafs = 0; if (subroot == NULL) // return 0 if empty tree return 0; if (subroot->isLeaf()) // return 1 if this is a leaf return 1; leftleafs = leafsof(subroot->left()); // recurse left rightleafs = leafsof(subroot->right()); // recurse right return leftleafs + rightleafs }

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

View Full Document
5.21 Huffman coding tree for the following: A B C D E F G H I J K L 2 3 5 7 11 13 17 19 23 31 37 41 A B C D E F G H I J K L Total
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}