#ifndef TREE_DEBUG_H_ #define TREE_DEBUG_H_ 1 // // Massingill, Berna L. // 2000 Apr 5 // CS 1321 // // Function to print contents of tree, in hierarchically indented // form -- if preprocessor symbol DEBUG is defined. If flag is not set, // a "dummy" version of the function, which does nothing, is compiled // in instead. // // To use this code: // Include a call to printTree() in your program, at the point at // which you want to print the contents of the tree for debugging // purposes. During program development/debugging, compile with the // option "-DDEBUG". When finished debugging, compile without this // option to suppress printing the contents of the tree. // #include #include "tree.h" template void printTree(ostream & out, const Tree &t); #ifdef DEBUG // Prototypes for helper functions. void printTabs(ostream & out, const int n); template void printTree_(ostream & out, const Tree &t, const int indents = 0); // Function definitions. // Function to print contents of tree -- root, then left subtree, // then right subtree, indented to show nesting. // Post: tree "t" printed to output stream "out". template void printTree(ostream & out, const Tree &t) { printTree_(out, t); } // Helper function to print contents of tree -- root, then left // subtree, then right subtree, indented to show nesting. // Pre: indents is the number of tabs to print at the start of each // line. // Post: tree "t" printed to output stream "out". template void printTree_(ostream & out, const Tree &t, const int indents) { if (t.empty()) { printTabs(out, indents); out << "empty tree\n"; } else { printTabs(out, indents); out << "root = " << t.item() << endl; if (!t.left().empty()) { printTabs(out, indents); out << "left subtree = \n"; printTree_(out, t.left(), indents+1); } if (!t.right().empty()) { printTabs(out, indents); out << "right subtree = \n"; printTree_(out, t.right(), indents+1); } } return; } void printTabs(ostream & out, const int n) { for (int i = 0; i < n; i++) out << '\t'; return; } #else // not DEBUG template void printTree(ostream & out, const Tree &t) { return; } #endif // DEBUG #endif // TREE_DEBUG_H_