CJCoding With Joseph
15free / daySign inCreate account

Indented Tree Printer

Build a class Node representing one node of a tree:
1. Node(const string& name) — a node with no children
2. void addChild(const Node& child) — stores a COPY of child in this node's list of children, keeping insertion order
3. void print(ostream& out, int depth = 0) const — writes depth times two spaces, then the node's name, then a newline; then calls print on each child with depth + 1

Write to out, not cout.

For a root root with children a and b, where a has one child a1, root.print(cout) prints the output shown in Expected Output.

solo.print(cout, 2) on a single node named solo prints 4 spaces and then solo.

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
Topics:
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.