A binary tree is a structure where every node can have at most two children. It is one of the most important topics in data structures because many advanced data structures are built using this concept. In this binary tree chapter, we will learn the basic terms, types of trees, and different ways to traverse a tree. Ultimately, we will create a simple binary tree in Java and print it using preorder, inorder, postorder, and level-order traversals.
A Binary Tree in data structures is a special tree structure used in data structures where each node can have at most two children. These children are known as:
- Left Child
- Right Child
Structure of a Binary Tree
The word binary means two; no node can have more than two children. In a binary tree, each node can connect to a maximum of two children
10
/ \
5 20
/ \ \
3 7 30
- 10 is the root
- 5 and 20 are children of 10
- 3, 7, 30 are leaf nodes
Types of Binary Trees
- Full Binary Tree: Every node has either 0 children or 2 children. No node has only one child.
- Complete Binary Tree: All levels are filled from left to right. It’s used in the Heap data structure.
- Perfect Binary Tree: All nodes have two children, and all leaf nodes are at the same level.
- Skewed Binary Tree: All nodes are on one side (left or right). Looks like a linked list.
- Binary Search Tree (BST): The Left child has a smaller value, the Right child has a larger value.
Binary Tree Traversal
Traversal means visiting each node one by one.
- Inorder Traversal: Left → Root → Right
- Preorder Traversal: Root → Left → Right
- Postorder Traversal: Left → Right → Root
Example:
10
/ \
5 20
- Inorder → 5 10 20
- Preorder → 10 5 20
- Postorder → 5 20 10
Binary Tree Implementation in Java
// Java code for Binary Tree
class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
// Binary Tree implementation in java
public class BinaryTree {
Node root;
// Preorder Traversal (Root → Left → Right)
public void preorder(Node node) {
if (node == null) return;
System.out.print(node.data + " ");
preorder(node.left);
preorder(node.right);
}
// Inorder Traversal (Left → Root → Right)
public void inorder(Node node) {
if (node == null) return;
inorder(node.left);
System.out.print(node.data + " ");
inorder(node.right);
}
// Postorder Traversal (Left → Right → Root)
public void postorder(Node node) {
if (node == null) return;
postorder(node.left);
postorder(node.right);
System.out.print(node.data + " ");
}
// Testing the Binary Tree
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
// Creating nodes manually
tree.root = new Node(10);
tree.root.left = new Node(5);
tree.root.right = new Node(20);
tree.root.left.left = new Node(3);
tree.root.left.right = new Node(7);
System.out.print("Inorder: ");
tree.inorder(tree.root); // 3 5 7 10 20
System.out.print("\nPreorder: ");
tree.preorder(tree.root); // 10 5 3 7 20
System.out.print("\nPostorder: ");
tree.postorder(tree.root); // 3 7 5 20 10
}
}
In this program, we create a simple binary tree in Java. We insert elements and then display the tree using inorder, preorder, and postorder traversals. This example helps beginners understand how a binary tree grows and how different traversal methods work.