Binary Tree
Table of Contents + β
A normal tree lets a node have any number of children. That sounds flexible. But it makes the code messy. You never know how many children to look at. So people picked one simple shape and used it everywhere. That shape is the binary tree.
A binary tree is a tree where each node has at most two children. We call them the left child and the right child. βAt most twoβ means a node can have zero, one, or two children. It can never have three.
π³ A Real-World Picture
Think about a knockout tournament on TV. One final match sits at the top. That final came from two semi-final matches below it. Each semi-final came from two quarter-finals below that. So every match feeds up from exactly two matches under it. There is a left side and a right side. That is a binary tree.
That fixed βtwo below each oneβ shape is what makes binary trees so handy.
π― Why Only Two Children?
Two is a sweet spot. With two children you can always ask one clean question. Do I go left, or do I go right? Here is why that helps.
- Your code stays simple. You only ever check two links, not a whole list of them.
- It fits decisions naturally. A yes/no question sends you to one of two sides.
- It is enough to build big ideas. Search trees, heaps, and expression trees all sit on top of this one shape.
Note
βBinaryβ just means βtwoβ. A binary tree is the two-children version of a tree. It does not mean the data has to be the numbers 0 and 1.
π§± What One Node Looks Like
Every node in a binary tree holds three things.
- data β the value stored in the node, like a number or a name.
- left β a link to the left child node, or empty if there is none.
- right β a link to the right child node, or empty if there is none.
When a link points to nothing, we call it null. In C and C++ it is NULL. In Java it is null. In Python it is None. A node with both links empty sits at the bottom. We call that a leaf node.
πΊοΈ The Tree We Will Build
Let us build this small tree. The value 10 sits at the top. That top node is called the root. The root has 20 as its left child and 30 as its right child.
Here 10 is the root. Both 20 and 30 are leaf nodes. Neither of them has children yet.
πͺ Steps to Build a Binary Tree
Before we write code, let us list the plan in plain words. Every language below follows these same steps.
- Define a node type that holds
data,left, andright. - Create the root node with the value
10. Set its left and right links to empty for now. - Create a node for
20. Link it as the rootβs left child. - Create a node for
30. Link it as the rootβs right child. - Print the rootβs value, then its left childβs value, then its right childβs value.
π» Building the Tree in Code
Now the same five steps turned into real code. Read the comments to follow along.
# One node holds data and two linksclass Node: def __init__(self, value): self.data = value self.left = None self.right = None
root = Node(10) # step 2: rootroot.left = Node(20) # step 3: left childroot.right = Node(30) # step 4: right child
# step 5: print root and its two childrenprint("Root:", root.data)print("Left child:", root.left.data)print("Right child:", root.right.data)The output of the above code will be:
Root: 10Left child: 20Right child: 30Tip
Building the tree took the same time in every language. We made three nodes and set a few links. That is O(1) work because the tree size is fixed and small. Reaching the root or a direct child is also O(1). We just follow one link.
π§© Common Types of Binary Trees
As trees grow, people gave names to the shapes they take. You will hear these words a lot. So let us meet them quickly.
- A full binary tree is one where every node has either zero children or exactly two children. No node is allowed to have just one child.
- A complete binary tree is filled level by level from the top. Each level fills from left to right. No gaps are left behind.
- A balanced binary tree keeps the left and right sides close to the same height. So the tree stays short and fast to search.
Note
Do not memorise these names yet. Just know they exist. Each type has its own tutorial later. The shape will make more sense once you start traversing trees.
Here is a quick side-by-side so the difference is easy to see.
| Type | Simple Rule |
|---|---|
| Full | Each node has 0 or 2 children, never 1 |
| Complete | Filled top to bottom, left to right, no gaps |
| Balanced | Left and right heights stay close |
β οΈ Common Mistakes
A few small slips trip up almost everyone at the start. Watch for these.
- Forgetting to set
leftandrightto empty when you make a node. Then they hold garbage and your code may crash. - Reading
root.left.datawhenleftis empty. Always check the link is not null before you follow it. - Mixing up left and right when you link children. The tree still builds, but the shape is wrong.
- Thinking every node must have two children. It can have zero, one, or two.
π§© What Youβve Learned
- β A binary tree is a tree where each node has at most two children, a left and a right.
- β Two children keeps the code simple and fits yes/no style decisions.
- β
A node stores
data, aleftlink, and arightlink, with empty links shown as null. - β
You built a small tree with
10at the root,20on the left, and30on the right, in five languages. - β Full, complete, and balanced are common named shapes you will study later.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
How many children can a node in a binary tree have?
Why: A binary tree node can have zero, one, or two children, so at most two.
- 2
What three things does a binary tree node usually store?
Why: Each node holds its data plus a left link and a right link to its children.
- 3
What does a null left or right link mean?
Why: A null link simply means the node has no child on that side.
- 4
In a full binary tree, how many children can a node have?
Why: A full binary tree allows only zero or two children per node, never just one.