10TreesSupplement - CS 240 Chapter 10 – Trees CS 240...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 240 Chapter 10 – Trees CS 240 Chapter 10 – Trees 1Page 210.01 Additional Tree Traversal Example #1 // Public function to “spell out” the values from the root to the tree’s // // leaf nodes, with the results output on a separate line for each leaf. // template < class E> void BinaryTree<E>::spellFromTop(ofstream &os) { continueSpelling(root, os, ""); } // Protected function to concatenate the char value of the current subtree’s // // root to the str param. until a leaf is reached, whereupon str is output. // template < class E> void BinaryTree<E>::continueSpelling(nodePtr treeRoot, ofstream &os, string str) { if (treeRoot == NULL) return ; else { str += char (treeRoot->data); if ((treeRoot->left == NULL) && (treeRoot->right == NULL)) os << str << endl; else { continueSpelling(treeRoot->left, os, str); continueSpelling(treeRoot->right, os, str); } } } CS 240 Chapter 10 – Trees CS 240 Chapter 10 – Trees 2Page 210.02 Applying spellFromTop to a Sample Tree M I C E L K L O M P S str: “M” str: “MI” str: “MIL” str: “MICE” output “MICE” str: “MIC” str: “MILL” output “MILL” str: “MILK” output “MILK” str: “MO” str: “MOP” output “MOM” str: “MOM” str: “MOPS” output “MOPS” M I C E L K L O M P S CS 240...
View Full Document

This note was uploaded on 08/26/2009 for the course CS 240 taught by Professor Klein,s during the Spring '08 term at Southern Illinois University Edwardsville.

Page1 / 8

10TreesSupplement - CS 240 Chapter 10 – Trees CS 240...

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