There are three traversal methods in Java:
Pre order- The root node is visited first.
In Order-The root node is visited after one of the sides(left or right) of a sub tree has been visited.
Post Order-The root node is visited last.
For the example below we shall assume there exists a tree node with the getName() method returning a String.
We can use the below methods to find the exact node based on the requirements specified:
public void postOrder(TreeNode t){ if(t!=null){ postOrder(t.LeftChild()); postOrderHuffTraversal(t.RightChild()); System.out.println(t.getName()); } }
public void inOrder(TreeNode t){ if(t!=null){ inOrder(t.LeftChild()); System.out.println(t.getName()); inOrder(t.RightChild()); } }
public void preOrder(TreeNode t){ if(t!=null){ System.out.println(tn.getName()); preOrder(t.LeftChild()); preOrder(t.RightChild()); } }