{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

10TreesSupplement - Additional Tree Traversal Example#1...

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

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

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

View Full Document Right Arrow Icon
 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
Background image of page 2
 CS 240
Background image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}