Introduction
A Binary Tree is a data structure that consists of nodes, where each node has at most two children referred to as the left child and the right child. Traversing a binary tree means visiting all the nodes in a specific order. There are three common types of traversal algorithms for binary trees: In-order, Pre-order, and Post-order. These algorithms help in visiting nodes of the tree in a systematic manner and are widely used in tree-based applications like search trees, expression trees, and more.
Objective
The objective of this tutorial is to implement the three common binary tree traversal algorithms in Java: In-order, Pre-order, and Post-order traversal. Each traversal method will visit all nodes of the binary tree in different sequences. After implementing the code, you will be able to understand the structure and logic behind these traversal techniques.
Java Code for Binary Tree Traversal
public class BinaryTree {
// Define a node structure
static class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
left = right = null;
}
}
// Root node of the binary tree
Node root;
// Pre-order traversal (Root -> Left -> Right)
public void preOrderTraversal(Node node) {
if (node != null) {
System.out.print(node.data + " ");
preOrderTraversal(node.left);
preOrderTraversal(node.right);
}
}
// In-order traversal (Left -> Root -> Right)
public void inOrderTraversal(Node node) {
if (node != null) {
inOrderTraversal(node.left);
System.out.print(node.data + " ");
inOrderTraversal(node.right);
}
}
// Post-order traversal (Left -> Right -> Root)
public void postOrderTraversal(Node node) {
if (node != null) {
postOrderTraversal(node.left);
postOrderTraversal(node.right);
System.out.print(node.data + " ");
}
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
// Manually creating a binary tree
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.right.left = new Node(6);
tree.root.right.right = new Node(7);
// Print traversal results
System.out.print("Pre-order Traversal: ");
tree.preOrderTraversal(tree.root);
System.out.print("\nIn-order Traversal: ");
tree.inOrderTraversal(tree.root);
System.out.print("\nPost-order Traversal: ");
tree.postOrderTraversal(tree.root);
}
}
Explanation of the Program Structure
The program consists of a simple BinaryTree class with a nested static Node class that represents each node of the tree. The BinaryTree class contains methods for the three traversal algorithms:
- preOrderTraversal: This method prints the data of the root node first, then recursively traverses the left subtree, followed by the right subtree (Root -> Left -> Right).
- inOrderTraversal: This method recursively traverses the left subtree, prints the data of the root node, and then traverses the right subtree (Left -> Root -> Right).
- postOrderTraversal: This method recursively traverses the left and right subtrees first, then prints the data of the root node (Left -> Right -> Root).
The binary tree is manually created in the main
method, where the root node and its children are initialized. After creating the tree, the three traversal methods are called to print the result of each traversal.
How to Run the Program
- Copy the entire Java code into a file named
BinaryTree.java
. - Open your terminal or command prompt.
- Navigate to the directory where you saved the file.
- Compile the Java file using the command:
javac BinaryTree.java
- Run the compiled Java program using the command:
java BinaryTree
- You will see the output of the pre-order, in-order, and post-order traversal of the binary tree.