I have the following method to recursively perform a preorder traversal of a ternary tree but having difficulty printing it in a certain manner.
public void preOrder(Node node) {
if (node == null) {
return;
}
System.out.print(" " + node.data);
preOrder(node.left);
preOrder(node.middle);
preOrder(node.right);
}
Output: Root LeftChild LeftChildA LeftChildB LeftChildC MiddleChild RightChild
Desired Output:
Root
Left
LeftChildA //Left child of left
LeftChildB //Middle child of left
LeftChildC //Right child of left
Middle
Right
I want to indent each level of the tree to make it more easier to visualize the tree's structure. Please help.