I implemented a recursive depth-first traversal, with all three traversal modes in this function. Is this the most succinct way to write the code?
/** * Node */ public class Node { private int value; Node left; Node right; public Node(int value) { this.setValue(value); left = null; right = null; } /** * @return the value */ public int getValue() { return value; } /** * @param value the value to set */ public void setValue(int value) { this.value = value; }
/** * Depth-First Traversal (Recursion) * @param root root of node-tree to be traversed */ public void dftRecursion(Node root) { if (root == null) { return; } switch (tMode) { // traversal mode case IN_ORDER: dftRecursion(root.left); System.out.print(root.getValue() + " "); dftRecursion(root.right); break; case PRE_ORDER: System.out.print(root.getValue() + " "); dftRecursion(root.left); dftRecursion(root.right); break; case POST_ORDER: dftRecursion(root.left); dftRecursion(root.right); System.out.print(root.getValue() + " "); break; default: throw new IllegalArgumentException("Mode value:" + tMode); } }
tMode
? Please ensure that your code is posted with the proper context.\$\endgroup\$