← Back to Streams
Question 644
Indented Tree Printer
Indented Tree Printer
Build a classNoderepresenting one node of a tree: 1.Node(const string& name)— a node with no children 2.void addChild(const Node& child)— stores a COPY ofchildin this node's list of children, keeping insertion order 3.void print(ostream& out, int depth = 0) const— writesdepthtimes two spaces, then the node's name, then a newline; then callsdepth + 1Write toout, notcout. For a rootrootwith childrenaandb, whereahas one childa1,root.print(cout)prints the output shown in Expected Output.solo.print(cout, 2)on a single node namedsoloprints 4 spaces and thensolo. Do NOT write a main function.
Expected Output:
root a a1 b solo solo p solo q
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Streamsostream
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.