Decision Tree

How an algorithm classifies through "yes/no" questions – and why too many questions make it dumb.

What is a decision tree?

AnalogyDefinition

Imagine a doctor in the ER: "Fever, yes or no?" — if yes, then "Cough?". Every question splits the patients into two groups until a diagnosis emerges. Nobody has to process all symptoms at once — the path through the questions yields the answer.

A decision tree does exactly that. It asks a series of simple yes/no questions about the features of the data points and slices them step by step into ever purer groups. The result: a tree of questions a human can actually read.

Question by question to a class

The tree is built node by node — every step is almost embarrassingly simple. Repeated, that is what you see in the demo:

  1. Split — the best cut

    For each node the algorithm tries every possible threshold in every feature and picks the cut with the highest information gain. The node remembers "feature x ≤ threshold" and divides the data into two halves.

  2. Purity — Gini or entropy

    Gini impurity measures the probability of misclassifying a randomly drawn sample: 0 for a pure node, 0.5 for a 50/50 mix. Entropy measures the same thing in bits. Both push the tree to produce groups that are as homogeneous as possible.

  3. Stop — when does the tree stop growing?

    Without a brake the tree would put every single point into its own leaf — memorizing every speck of noise. So it stops at the maximum depth, when too few samples remain in a node, or when a node is already pure.

This is the trade-off: too few splits and the tree cannot separate the pattern (underfitting); too many and it memorises noise instead of generalising (overfitting). The demo shows both directly — the overfitting gap between training and test accuracy tells you when you have gone too far.

Interactive demo

Click on the surface to set points. Tune the depth and watch the tree carve the plane into ever finer pieces – until it memorises every speck of noise.

Guided tour

New to the topic? Follow the tour step by step – or jump straight into free experimentation.

What this demo shows

Two views side by side make it visible how a tree of questions carves a surface into regions. Here is what you see on screen.

What you see
On the left, a square field of blue and red dots that breaks apart into rectangular blocks along straight horizontal and vertical lines — each block coloured for the class that dominates there. On the right, a tree diagram of little boxes that branches downward from a single box at the top into ever more limbs.
What happens
Each new question adds one more straight cut on the left and splits a block into two smaller ones; on the right the tree grows one level deeper and forks into a yes and a no branch. Block by block the regions turn more uniform in colour while the tree grows wider and deeper.
What you can do
Place points by clicking the field, pick a ready-made pattern as the dataset, drag the sliders for depth and minimum size, let the tree grow with ▶ or step through cut by cut. In test mode you send a point down through the tree from the top.
What to watch for
Watch the shape of the border between the colours: it is always made of straight steps, never slanted or curved lines. And every coloured block can be traced back from the top through the tree — as a chain of simple yes/no questions.

Classification surface

Place at least 4 points or load a dataset.
Class AClass BDecision boundaryTest point

Learned tree

Showing the complete tree. Hit ▶ to watch it grow split by split.

No data – set a few points or pick a dataset.

0
Points
0
Leaves
0
Depth
Train accuracy
Test accuracy
0%
Overfitting gap
TheoryPseudocodeStep by stepFlow diagram

A tree of yes/no questions

A binary decision tree classifies data points by a chain of simple threshold tests. At each node a feature and a threshold are chosen: everything below the threshold goes left, everything above goes right. That way the tree carves the feature space into axis-aligned rectangles — each leaf yields a prediction.

Information gain — the best cut

To stop the tree from growing arbitrarily, a greedy algorithm picks at each node the split that reduces impurity the most. Impurity measures are:

  • Gini impurity: 1 − Σ p_k² — probability of misclassifying a randomly drawn sample
  • Entropy: −Σ p_k · log₂ p_k — information content of the class distribution in bits

Information gain = impurity(parent) − (n_L/n)·impurity(left) − (n_R/n)·impurity(right). The algorithm tries every feature and every possible threshold and picks the one with the highest gain.

Stop criteria

Recursion stops when one of these holds:

  • The node is pure (all points belong to the same class)
  • The maximum depth has been reached
  • The node holds fewer samples than the minimum required for a split
  • No split has positive gain (no cut improves purity)

Bias-variance trade-off

  • Shallow (depth 1–3): high bias, the tree cannot separate the pattern → underfitting
  • Medium (depth 4–6): good balance, train and test accuracy stay close
  • Deep (depth ≥ 8): high variance, the tree memorises noise → overfitting

Play in the demo! Crank max depth up — the overfitting gap between train and test accuracy reveals the moment the tree starts memorising instead of learning.

Where decision trees show up in the real world

Decision trees aren't just a teaching example — they run wherever decisions need to be traceable:

Credit scoring

Banks decide on a loan based on income, debt and history. The tree delivers the reasoning along with the verdict — crucial, since rejections must be legally explainable.

Medical triage & diagnosis

A chain of symptom questions leads to a suspected diagnosis or urgency level — a path that clinicians can read without any AI knowledge.

Customer analytics

Which customers are likely to churn? Which segment responds to an offer? Trees segment tabular data quickly and explainably.

Random forests & gradient boosting

Hundreds of averaged trees form random forests and XGBoost — still the strongest models for structured tabular data and perennial winners of data-science competitions.

Common misconceptions

The deeper the tree, the better the model.

More depth only raises training accuracy. Past a point the tree memorises noise (overfitting) — what matters is test accuracy, not depth.

A decision tree is always easy to interpret.

Only while it stays small. A tree with hundreds of nodes is, in practice, just as opaque to humans as a neural network.

Features must be normalised before training.

Not needed: trees compare individual thresholds per feature and are therefore scale-invariant — unlike, say, a perceptron or k-means.

A tree can draw any boundary you like.

Splits are always perpendicular to an axis. Diagonal or circular boundaries (see the "Spiral" dataset) are only approximated in a staircase pattern — with many small cuts.

Test your understanding

Question 1 / 4

What does Gini impurity measure in a node?

Select one answer
Answer Key: 1) B · 2) A · 3) C · 4) B

The essentials in five points

  1. Questions, not formulasA decision tree classifies through a chain of simple yes/no threshold questions; every path from root to leaf reads as an if-then rule.
  2. Purity drives the splitsAt each node the greedy algorithm picks the cut with the highest information gain, i.e. the largest drop in impurity (Gini or entropy).
  3. Depth is a trade-offToo shallow underfits, too deep overfits. The gap between training and test accuracy (the overfitting gap) shows when you've gone too far.
  4. Axis-aligned & scale-invariantCuts are perpendicular to an axis; curved boundaries are only approximated in steps. In return, no feature normalisation is required.
  5. Strongest in a crowdRandom forests and gradient boosting average many trees and rank among the best methods for tabular data anywhere.