Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save meghakrishnamurthy/6845ace3ac62dc8cedcf9ff3950a702f to your computer and use it in GitHub Desktop.
Save meghakrishnamurthy/6845ace3ac62dc8cedcf9ff3950a702f to your computer and use it in GitHub Desktop.
Preorder, Inorder and Postorder traversal of Binary tree implementation in Java
package megha.codingproblem.trees;
/**
* Class to perform the different DFS traversals on a binary tree:
* 1. Pre order traversal
* 2. In order traversal
* 3. Post order traversal
*
* Time complexity of these operations - O(n)
* Space complexity of these operations:
* O(h) where h is the height of the binary tree
* Best/average case - O(logn)
* Worst case - O(n)
*
* @author megha krishnamurthy
*
*/
public class PreorderInorderPostorder {
/**
* Method to perform pre order traversal of a binary tree
* @param root
*/
public void preOrder(Node root) {
if(root == null) {
return;
}
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
/**
* Method to perform in order traversal of a binary tree
* @param root
*/
public void inOrder(Node root) {
if(root == null) {
return;
}
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
/**
* Method to perform post order traversal of a binary tree
* @param root
*/
public void postOrder(Node root) {
if(root == null) {
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(6);
root.right = new Node(21);
root.left.left = new Node(1);
root.left.right = new Node(8);
root.right.left = new Node(13);
root.right.right = new Node(25);
root.right.left.left = new Node (12);
root.right.left.right = new Node(18);
PreorderInorderPostorder treeOrder = new PreorderInorderPostorder();
//Pre-order traversal
treeOrder.preOrder(root);
System.out.println();
//In order traversal
treeOrder.inOrder(root);
System.out.println();
//Post-order traversal
treeOrder.postOrder(root);
System.out.println();
}
}
private class Node {
int data;
Node left;
Node right;
private Node(int value) {
data = value;
left = right = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment