Last active
May 27, 2018 04:33
-
-
Save thmain/9a43046b0ec8d8853f2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TreeTraversals { | |
public void inorderRecursive(Node root) { | |
if (root != null) { | |
inorderRecursive(root.left); | |
System.out.print(root.data + " "); | |
inorderRecursive(root.right); | |
} | |
} | |
public void postOrderRecursive(Node root) { | |
if (root != null) { | |
postOrderRecursive(root.right); | |
postOrderRecursive(root.left); | |
System.out.print(root.data + " "); | |
} | |
} | |
public void preOrderRecursive(Node root) { | |
if (root != null) { | |
System.out.print(root.data + " "); | |
preOrderRecursive(root.left); | |
preOrderRecursive(root.right); | |
} | |
} | |
public static void main(String[] args) { | |
Node root = new Node(1); | |
root.left = new Node(2); | |
root.right = new Node(3); | |
root.left.left = new Node(4); | |
root.left.right = new Node(5); | |
root.right.left = new Node(6); | |
root.right.right = new Node(7); | |
TreeTraversals i = new TreeTraversals(); | |
System.out.println("Inorder Traversal:"); | |
i.inorderRecursive(root); | |
System.out.println("\nPreorder Traversal:"); | |
i.preOrderRecursive(root); | |
System.out.println("\nPostorder Traversal:"); | |
i.postOrderRecursive(root); | |
} | |
} | |
class Node { | |
int data; | |
Node left; | |
Node right; | |
public Node(int data) { | |
this.data = data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment