Binary Tree in Java: Representation, Traversal, and Common Operations
This article introduces binary trees, explains their Java representation, demonstrates pre-order, in-order, post-order, and level-order traversals, and provides implementations for creating full binary trees, counting nodes, computing height, and counting leaf nodes, complete with sample code and usage examples.
Binary trees are a special type of tree data structure where each node has at most two children, commonly referred to as the left and right child. They are fundamental in computer science and frequently appear in technical interviews.
The basic Java representation of a binary tree node is shown below:
class TreeNode {
// node data (using int for simplicity)
int data;
// left child
TreeNode left;
// right child
TreeNode right;
// constructor
public TreeNode(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}Four classic traversal methods are demonstrated with recursive implementations:
public void preOrder(TreeNode root) {
if (root == null) {
return;
}
System.out.println(root.data);
// traverse left subtree
preOrder(root.left);
// traverse right subtree
preOrder(root.right);
} public void inOrder(TreeNode root) {
if (root == null) {
return;
}
// traverse left subtree
inOrder(root.left);
System.out.println(root.data);
// traverse right subtree
inOrder(root.right);
} public void postOrder(TreeNode root) {
if (root == null) {
return;
}
// traverse left subtree
postOrder(root.left);
// traverse right subtree
postOrder(root.right);
System.out.println(root.data);
} public void levelOrder(TreeNode root) {
if (root == null) {
return;
}
Queue
queue = new LinkedList<>();
// enqueue root
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
System.out.println(node.data);
// enqueue left and right children if they exist
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}A method for constructing a full binary tree from an array is provided:
public TreeNode createFullTree(int[] arr, int start, int end) {
if (start > end) {
return null;
}
int mid = (start + end) / 2;
TreeNode root = new TreeNode(mid + 1);
// recursively build left subtree
root.left = createFullTree(arr, start, mid - 1);
// recursively build right subtree
root.right = createFullTree(arr, mid + 1, end);
return root;
}Additional utility methods include counting the total number of nodes, computing the tree height, and counting leaf nodes:
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
return countNodes(root.left) + countNodes(root.right) + 1;
} public int getHeight(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
} public int getLeafCount(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
return getLeafCount(root.left) + getLeafCount(root.right);
}The complete example program ties everything together: it creates a full binary tree from a sample array, performs all four traversals, and prints the results along with node count, tree height, and leaf count.
package com.sample.algo.tree;
import java.util.LinkedList;
import java.util.Queue;
class TreeNode {
int data;
TreeNode left;
TreeNode right;
public TreeNode(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTreeExample {
public static void main(String[] args) {
BinaryTreeExample binaryTreeExample = new BinaryTreeExample();
int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7};
TreeNode root = binaryTreeExample.createFullTree(arr, 0, arr.length - 1);
System.out.println("Pre-order traversal result:");
binaryTreeExample.preOrder(root);
System.out.println("In-order traversal result:");
binaryTreeExample.inOrder(root);
System.out.println("Post-order traversal result:");
binaryTreeExample.postOrder(root);
System.out.println("Level-order traversal result:");
binaryTreeExample.levelOrder(root);
System.out.println("Node count: " + binaryTreeExample.countNodes(root));
System.out.println("Tree height: " + binaryTreeExample.getHeight(root));
System.out.println("Leaf count: " + binaryTreeExample.getLeafCount(root));
}
// traversal and utility methods (preOrder, inOrder, postOrder, levelOrder, createFullTree, countNodes, getHeight, getLeafCount) are defined here as shown above
}In conclusion, the article covers essential binary‑tree operations and provides ready‑to‑run Java code, serving as a practical reference for anyone learning or reviewing fundamental data‑structure algorithms.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.