01 / 21

Binary Tree
Representation in Java

Learn how to model binary trees using node classes in Java: from defining the structure to linking nodes into hierarchical data.

Java Data Structures

What Is a Binary Tree?

A binary tree is a hierarchical data structure where each element, called a node, has at most two children: a left child and a right child. The topmost node is called the root.

A B C D E root left right

Key Terminology

Root
The topmost node with no parent. Every tree has exactly one root.
Leaf
A node with no children: both left and right are null.
Parent
A node that has one or two children connected below it.
Depth
Number of edges from the root to a given node. The root has depth \(0\).
Height
Longest path (edge count) from a node down to a leaf.

Why Binary Trees Matter

Binary trees power some of the most important structures in computer science. Understanding them unlocks efficient search, sorting, and hierarchical modeling.

1
Binary Search Trees (BSTs) enable \(O(\log n)\) average-case search, insert, and delete.
2
Heaps (often implemented as arrays) use a binary tree structure for priority queues and efficient sorting.
3
Expression trees represent mathematical formulas in compilers and calculators.
4
Huffman trees drive data compression algorithms used in file formats like ZIP and JPEG.

The Node Class: Core Building Block

In Java, a binary tree node is modeled as a class with three fields: a value, a reference to the left child, and a reference to the right child.

class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; this.left = null; this.right = null; } }

Anatomy of a TreeNode

val: 42 left: • right: • val: 17 left: null right: null val: 63 left: null right: null

Node Class vs. Tree Class

A common design pattern separates the node definition from a tree wrapper that manages the root and provides methods.

TreeNode (Node) BinaryTree (Wrapper)
Purpose Stores one value + child links Manages root reference + tree methods
Fields val, left, right root
Methods Constructor only (typically) insert, search, traverse, etc.
Instances One per element in the tree One per entire tree

How to Build a Binary Tree

1
Define the TreeNode class with val, left, and right fields, plus a constructor.
2
Create individual nodes by calling new TreeNode(value) for each element.
3
Connect nodes by assigning root.left = childNode and root.right = childNode.
4
Keep a root reference: this single variable gives you access to the entire tree.

Example 1: Three-Node Tree

// Step 1: Create nodes TreeNode root = new TreeNode(10); TreeNode left = new TreeNode(5); TreeNode right = new TreeNode(15); // Step 2: Link children to root root.left = left; root.right = right;
10 5 15

Example 2: Five-Node Tree

TreeNode root = new TreeNode(20); root.left = new TreeNode(10); root.right = new TreeNode(30); root.left.left = new TreeNode(5); root.left.right = new TreeNode(15);
20 10 30 5 15 leaf (no children)

Example 3: Using a Wrapper Class

class BinaryTree { TreeNode root; BinaryTree() { this.root = null; } void buildSample() { 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); } }

Understanding null References

In Java, null means "this reference points to nothing." In binary trees, null marks the absence of a child. It is the base case for recursive algorithms and the signal that you've reached the edge of the tree.

7 3 null null null

Trees Are Recursive by Nature

Every subtree of a binary tree is itself a binary tree. This recursive structure is why tree algorithms are naturally expressed as recursive methods.

Full tree Left subtree Right subtree A B C D E

Going Generic: Any Data Type

So far our node stores int values. Using Java generics, we can make the node store any type: String, Double, or custom objects.

class TreeNode<T> { T val; TreeNode<T> left; TreeNode<T> right; TreeNode(T val) { this.val = val; this.left = null; this.right = null; } } // Usage with Strings: TreeNode<String> root = new TreeNode<>("hello");

Common Mistakes

✕ Forgetting null Checks
Calling node.left.val when node.left is null throws a NullPointerException. Always guard with an if-check first.
✕ Losing the Root
Reassigning root = root.left without saving the original root means you permanently lose access to the right subtree.
✕ Circular References
Setting node.left = node (a node pointing to itself) creates an infinite loop. Children must always be different nodes.

Summary: Key Concepts at a Glance

Concept Definition Java Code
Node One element in the tree class TreeNode { ... }
Value Data stored in a node int val;
Left child Reference to left subtree TreeNode left;
Right child Reference to right subtree TreeNode right;
Root Top-level node TreeNode root = new TreeNode(x);
Leaf Node with no children left == null && right == null

Question 1 of 5

What are the three fields in a basic TreeNode class for a binary tree?

Question 2 of 5

Given the following code, what does root.left.right evaluate to?

Question 3 of 5

Which statement correctly makes node B the left child of node A?

Question 4 of 5

A leaf node in a binary tree is defined as a node where:

Question 5 of 5

Why is the TreeNode class called a self-referential (recursive) data structure?