CSC375 Homework7 George Corser 2009-03-09

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

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

View Full Document Right Arrow Icon
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 }
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/19/2010 for the course CSC 375 taught by Professor Turner during the Winter '09 term at University of Michigan-Dearborn.

Page1 / 4

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online