LEARNING
TREE CONCEPT
IN JAVA
When I first learned Tree data structures in Java, I felt lost. Arrays were easy. Lists were predictable. Everything was linear.
Then Trees showed up.
Suddenly, data was no longer in a straight line. It branched, split, and went deeper. I kept seeing words like root, child, left, right, and inOrder, but no one explained them in a way that felt natural.
The First Mental Model: A Family Tree
- check_circle Node - One element in the tree
- check_circle Root - The top node
- check_circle Parent - A node that has children
- check_circle Child - A node connection below another node
- check_circle Leaf - A node with no children
- check_circle Edge - Connection between nodes
- check_circle Height - Number of levels in the tree
- check_circle Each child can have its own children
In a Binary Tree, each node can have at most two children:
- check_circle Left
- check_circle Right
The `TreeNode` Everyone Starts With
In Java, a Tree is usually built from a simple class like this:
class TreeNode {
int value;
TreeNode left;
TreeNode right;
TreeNode(int value) {
this.value = value;
}
}Important realization:
- check_circle TreeNode = a single piece of the tree
- check_circle A Tree is just a bunch of TreeNodes connected together
- check_circle You don’t even need a Tree class, a root node is enough
Seeing It in Real Code
We start with this structure:
1
/ \
2 3
/ \ / \
4 5 6 7InOrder traversal:
- check_circle Go left → 4
- check_circle Visit root → 2
- check_circle Go right → 5
Result:
4 → 2 → 5 → 1 → 6 → 3 → 7
Now, more complex tree
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \ / \ / \
8 9 10 11 12 13 14 15Building tree in Java
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(7);
root.left.left.left = new TreeNode(8);
root.left.left.right = new TreeNode(9);
root.left.right.left = new TreeNode(10);
root.left.right.right = new TreeNode(11);
root.right.left.left = new TreeNode(12);
root.right.left.right = new TreeNode(13);
root.right.right.left = new TreeNode(14);
root.right.right.right = new TreeNode(15);Implementation inOrder
void inOrder(TreeNode node) {
if (node == null) return;
inOrder(node.left);
System.out.print(node.value + " ");
inOrder(node.right);
}Result:
8 → 4 → 9 → 2 → 10 → 5 → 11 → 1 → 12 → 6 → 13 → 3 → 14 → 7 → 15
See the level and count the nodes
Here’s a very simple way to count how many nodes exist at each level using Java.
import java.util.*;
class TreeNode {
int value;
TreeNode left;
TreeNode right;
TreeNode(int value) {
this.value = value;
}
}
public class CountNodesPerLevel {
static void countLevels(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int level = 0;
while (!queue.isEmpty()) {
int size = queue.size(); // nodes in this level
System.out.println("Level " + level + ": " + size + " nodes");
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
level++;
}
}
}When you run this on the tree above, you get:
Level 0: 1 nodes
Level 1: 2 nodes
Level 2: 4 nodes
Level 3: 8 nodes