Decision Tree (ID3) Theory Guide

Try the Decision Tree (ID3) Solver →
Intermediate10 min readLast Updated June 26, 2026
Prerequisites:Information Gain, Entropy Basics
Decision TreeID3EntropyInformation GainClassification

Think of how you play the game '20 Questions'. You don't start by guessing the exact answer; you start with a broad question like 'Is it alive?' to instantly eliminate half the possibilities. A Decision Tree is just an algorithm playing this exact game with your dataset — it asks a sequence of mathematically chosen yes/no questions to filter raw data down to a final prediction.

  • The Root Question: Every feature in the dataset gets scanned to find the single one that separates the classes most cleanly. This 'cleanliness' is measured mathematically through Information Gain or Gini Impurity — the core exam formulas behind every split.
  • The Branches: Once data splits into smaller groups, the exact same process repeats inside each one. The algorithm interrogates the remaining features again, hunting for the next best question specific to that particular subgroup.
  • The Leaves: Questioning stops once a subgroup becomes completely pure — like 100% Spam — or a forced depth limit gets hit. Whatever class dominates that final subgroup becomes the prediction handed back to the user.

It is the only fundamental classifier that hands back a human-readable flowchart instead of a mathematical black box.

How to Build a Decision Tree by Hand

1

Calculate the Base Target Entropy: Look strictly at the final target column and ignore every feature for now. Count the total Positives and total Negatives across the entire dataset, then plug those counts into the Entropy formula. This baseline 'messiness' number is mandatory — Information Gain cannot be calculated without it.

2

Count Positives and Negatives per Value: Pick one feature column and list its unique values. For each individual value, count its specific Positives pip_i and Negatives nin_i separately. Calculate the standalone entropy for each value exactly the same way the base target entropy was calculated in Step 1.

3

Calculate the Total Feature Entropy: Branch entropies cannot simply be averaged together — a weighted average is required. Multiply each value's entropy by its weight, calculated as that branch's row count divided by the total dataset rows. Add all of these weighted results together to get one single Total Feature Entropy number.

4

Subtract to Find the Information Gain: Subtract the Total Feature Entropy from the Base Target Entropy calculated in Step 1. That subtraction result is the Information Gain for this specific feature. Repeat Steps 2 through 4 separately for every remaining feature, and select whichever one scores the highest as the splitting node.

5

Draw Branches and Pure Leaves: Draw one branch for every unique value belonging to the winning feature. Inspect the dataset rows flowing down each branch. If a branch contains exclusively Positives or exclusively Negatives, that data is pure — cap it immediately with a terminal Leaf Node and move on.

6

Filter Mixed Branches and Repeat: If a branch contains a confusing mix of Positives and Negatives, the split is not finished. Filter the scratch paper dataset down to only the rows belonging to that specific branch, cross out the feature already used, and repeat the entire process starting again from Step 1.

The Entropy & Information Gain Formulas

Entropy(S)=PP+Nlog2(PP+N)NP+Nlog2(NP+N)\text{Entropy}(S)=-\frac{P}{P+N}\log_2\left(\frac{P}{P+N}\right)-\frac{N}{P+N}\log_2\left(\frac{N}{P+N}\right)

Breaking Down the Math

  • PP and NN — The Chaos Metric: PP is the total count of Positives, NN is the total count of Negatives. Entropy is purely a measure of messiness. A perfect 50/50 split between classes pushes Entropy to its maximum value of 1.01.0. A completely pure dataset — all Positives or all Negatives — drops Entropy to exactly 0.00.0.
  • Why the Leading Negative Sign Exists: Probabilities like PP+N\frac{P}{P+N} are always fractions strictly between 00 and 11. The logarithm of any fraction in that range is mathematically negative. Without the leading minus sign, Entropy would always output a negative number. Flipping that sign converts the result into a clean, readable 00 to 11 chaos scale.
  • Why Log Base 2 Specifically: Decision Trees fundamentally ask Yes/No questions, meaning every split produces exactly two possible paths. Measuring uncertainty in binary 'bits' matches that two-path structure perfectly. Using log2\log_2 scales the math so a perfectly uncertain 50/50 coin-toss split equals exactly 11 full bit of information.
  • The Weighted Feature Entropy: Before finding the gain, you must calculate the messiness of the new branches. You calculate the subset entropy for each branch Entropy(Pi,Ni)\text{Entropy}(P_i, N_i) and multiply it by that branch's weight. Formula: Entropy(A)=[pi+niP+N]×Entropy(Pi,Ni)\text{Entropy}(A)=\sum\left[\frac{p_i + n_i}{P + N}\right]\times\text{Entropy}(P_i, N_i)
  • Information Gain — The Final Subtraction: Entropy alone only describes the starting messiness; it does not pick a feature. Information Gain does that job, calculated as Base Entropy minus the Weighted Feature Entropy. Formula: Gain(S,A)=Entropy(S)Entropy(A)\text{Gain}(S, A)=\text{Entropy}(S)-\text{Entropy}(A)

Solved Example: Calculating a Root Node by Hand

Draw a 2-column scratchpad table: Feature ('Unknown Sender') and Target ('Label'). The 4 rows are: Row 1 (Yes → Spam), Row 2 (Yes → Legit), Row 3 (No → Legit), and Row 4 (Blocked → Spam). Write this exact dataset down before starting the math.

Step 1: Calculate the Base Target Entropy

Look only at the target column for now: P=2P=2 Spam and N=2N=2 Legit. This is a perfect 50/50 split, so plugging these counts into the Entropy formula produces exactly 1.01.0. That number is the starting messiness of the entire dataset — 1 full bit of chaos — before any feature gets tested.

Step 2: Count Positives and Negatives per Value

Break down the 'Unknown Sender' feature value by value. The 'Yes' branch holds pi=1,ni=1p_i=1,n_i=1 — a 50/50 split, giving it a standalone entropy of 1.01.0. The 'No' branch holds pi=0,ni=1p_i=0,n_i=1 — 100% pure, entropy 0.00.0. The 'Blocked' branch holds pi=1,ni=0p_i=1,n_i=0 — also 100% pure, entropy 0.00.0.

Step 3: Calculate Total Feature Entropy

Combine the three branch entropies using a weighted average based on row count. 'Yes' holds 2 of 4 rows, 'No' holds 1 of 4, 'Blocked' holds 1 of 4. The math becomes (24×1.0)+(14×0.0)+(14×0.0)=0.5(\frac{2}{4}\times1.0)+(\frac{1}{4}\times0.0)+(\frac{1}{4}\times0.0)=0.5. The Total Feature Entropy for 'Unknown Sender' is 0.50.5.

Step 4: Subtract to Find Information Gain

Subtract the weighted feature entropy from the base target entropy: 1.00.5=0.51.0-0.5=0.5. This 0.50.5 is the Information Gain for splitting on 'Unknown Sender'. If no other feature scores higher, 'Unknown Sender' wins the round and becomes the root node of the tree.

Step 5: Draw Leaves and Identify Next Steps

Because 'No' and 'Blocked' both landed at 0.00.0 entropy, they are completely pure — draw them immediately as terminal leaf nodes. The 'Yes' branch still sits at 1.01.0 entropy, meaning it stays mixed. Filter the dataset down to just those two rows and repeat the entire process to split them further.

See the Interactive Solver in Action

Calculating weighted entropy by hand across multiple features is brutal. Use the interactive solver to instantly calculate Information Gain and watch the tree carve out rules step-by-step.

Rules & Common Mistakes

  • Exam Trap: Never Reuse Categorical Features on the Same Path
    In basic ID3 trees, splitting on a feature like 'Weather' permanently crosses it off for that specific downward path only. A completely separate, parallel branch elsewhere in the tree can still use 'Weather' freely. Students constantly fail traces either by reusing a feature locally on the same path, or by mistakenly crossing it out globally for the entire tree.
  • Exam Trick: Pure Nodes = Zero Math
    If a branch receives rows that are 100% 'Spam' or 100% any single class, do not waste exam time writing out the full log formula just to prove the answer is zero. A pure node has an entropy of exactly 0.00.0 by definition. Simply state 'Pure Node = 0.00.0 Entropy' on the paper, draw the leaf node, and move straight to the next branch.
  • Exam Trap: The Unweighted Average Disaster
    The fastest way to lose marks on an Information Gain calculation is finding the left branch entropy, the right branch entropy, adding them together, and dividing by two. This is mathematically invalid. Each branch's entropy must be multiplied by its proportional weight — that branch's row count divided by the parent's total row count — before summing them into the final weighted average.
  • Lab Trap: How to Handle Continuous Numbers
    Standard Information Gain cannot run directly on raw numbers like 'Salary' — every row would form its own unique branch, producing a useless, overfit tree. In a real lab environment, the algorithm sorts the values, scans for points where the class label changes, and tests those midpoints as binary threshold questions, such as 'Is Salary greater than 50k?'

Strengths, Weaknesses & When To Use It

When to use it:Decision Trees are the go-to algorithm whenever interpretability is non-negotiable. If a bank denies a loan or a hospital flags a patient as high-risk, the exact reasoning must be explainable. A Decision Tree hands back the precise sequence of rules that led to that decision. That said, on exams and in real-world competitions, bare Decision Trees are rarely deployed alone — they overfit too easily and are usually upgraded into Random Forests to fix stability issues.

Advantages

  • The Ultimate Glass Box — 100% Interpretable: Unlike black box models like Neural Networks, a Decision Tree produces a literal flowchart. Any prediction can be traced by hand, node by node, and explained in plain language to a non-technical stakeholder — no matrix algebra, no probability theory, no hidden weights. The logic is fully visible and fully auditable from root to leaf.
  • Zero Data Prep Required — Immune to Scaling: Distance-based algorithms like KNN require normalization, since a raw salary of 100,000 would mathematically overwhelm an age of 25. Decision Trees evaluate one feature at a time and never calculate distance or geometry. That means scaling, normalization, and standardization can all be skipped entirely, saving significant preprocessing time before training even begins.

Disadvantages

  • The Memorization Trap — Extreme Overfitting: Without an explicit stopping rule like maximum depth or minimum leaf size, a Decision Tree will keep splitting until every single leaf is perfectly pure. That means it has effectively memorized the training data row by row. The result is a model that scores beautifully on training data but performs terribly and unpredictably on new, unseen test data.
  • High Instability — The Butterfly Effect: Decision Trees are a greedy algorithm, meaning each split locks in permanently without reconsidering earlier choices. Changing just one or two training rows can cause a completely different feature to win the root node. Since every subsequent split depends entirely on that root, one tiny data change can cascade into a completely unrecognizable tree structure.

Decision Tree vs. Random Forest

This is a classic exam question precisely because the two algorithms do not compete — one is built directly from the other. A Random Forest is simply an army of hundreds of Decision Trees forced to vote on a final answer collectively. Understanding exactly why data scientists abandoned single trees in favor of forests is the key to understanding modern ensemble learning as a whole.

  • Memorization vs. Generalization: A single tree is a notorious overfitter — it perfectly memorizes the training data, including every noisy quirk and outlier, which causes it to fail badly on new, unseen data. A Random Forest builds each individual tree on a random subset of rows and features, then averages all the results together, mathematically canceling out the noise that any single tree latched onto.
  • The Glass Box vs. The Black Box: A single Decision Tree is fully interpretable — the entire flowchart can be printed and traced to explain exactly why a specific prediction was made, step by step. A Random Forest sacrifices that transparency completely. It becomes an unreadable black box where hundreds of trees vote anonymously and no single tree's logic is authoritative on its own.
  • The Butterfly Effect vs. Robustness: A single tree is highly unstable. Changing just a handful of training rows can flip which feature wins the root node, rewriting every single branch beneath it. A Random Forest easily absorbs those same small data changes, since any given fluctuation only affects a fraction of the hundreds of trees voting in the ensemble.
  • Speed vs. Accuracy: A single Decision Tree trains almost instantly and demands very little memory, making it ideal for quick prototypes. A Random Forest requires significantly more RAM and CPU power to train hundreds of trees simultaneously across random data subsets. The trade-off is clear: sacrifice raw training speed in exchange for a substantial upgrade in real-world predictive accuracy.

Implementation Pseudocode

// DECISION TREE (ID3) — Recursive Partitioning by Information Gain
// Goal: recursively split the dataset by always choosing the feature
// that destroys the most uncertainty, measured by Information Gain.
// Stop splitting once a branch is pure or no features remain.

FUNCTION buildTree(dataset, availableFeatures):

    // ============================================================
    // BASE CASE 1: All rows share the same label
    // ============================================================
    IF all rows in dataset have the same label:
        RETURN LeafNode(label = that shared label)
        // Pure node — entropy is 0.0, no further math needed.
    END IF

    // ============================================================
    // BASE CASE 2: No features left to split on
    // ============================================================
    IF availableFeatures is empty:
        RETURN LeafNode(label = MAJORITY_VOTE(dataset))
        // Out of questions to ask — predict whichever class is most common.

    // ============================================================
    // STEP 1: Calculate the Base Target Entropy
    // ============================================================
    P = COUNT rows in dataset WHERE label == Positive
    N = COUNT rows in dataset WHERE label == Negative
    baseEntropy = entropy(P, N)
    // This is the starting 'messiness' before testing any feature.

    // ============================================================
    // STEP 2: Test Every Feature, Every Value
    // ============================================================
    bestFeature = NULL
    bestGain    = -INFINITY

    FOR EACH feature IN availableFeatures:

        weightedEntropy = 0

        FOR EACH uniqueValue IN feature.uniqueValues:
            branchRows = FILTER dataset WHERE feature == uniqueValue
            p_i = COUNT branchRows WHERE label == Positive
            n_i = COUNT branchRows WHERE label == Negative

            branchEntropy = entropy(p_i, n_i)
            branchWeight  = COUNT(branchRows) / COUNT(dataset)

            weightedEntropy = weightedEntropy + (branchWeight * branchEntropy)
            // Exam Trap: Do NOT just add branch entropies and divide by
            // the number of branches. Each branch entropy must be scaled
            // by its row-count weight first, or the gain calculation is invalid.
        END FOR

        // --- STEP 3: Calculate Information Gain for this feature ---
        gain = baseEntropy - weightedEntropy

        IF gain > bestGain:
            bestGain    = gain
            bestFeature = feature
        END IF

    END FOR

    // ============================================================
    // STEP 4: Build the Split Node and Recurse
    // ============================================================
    node = SplitNode(feature = bestFeature)

    FOR EACH uniqueValue IN bestFeature.uniqueValues:
        subset = FILTER dataset WHERE bestFeature == uniqueValue

        remainingFeatures = COPY(availableFeatures)
        REMOVE bestFeature FROM remainingFeatures
        // Exam Trap: Always pass a COPY of the remaining features down
        // this specific branch. Removing bestFeature from the original
        // shared list would cross it out globally, blocking parallel
        // branches elsewhere in the tree from ever using it again.

        node.addBranch(uniqueValue, buildTree(subset, remainingFeatures))
    END FOR

    RETURN node

END FUNCTION

Time & Space Complexity

ScenarioTime ComplexitySpace ComplexityNotes
Training Phase (Building the Tree)O(n×d×h)O(n\times d\times h)O(n)O(n)Here nn is rows, dd is features, and hh is the maximum tree height. At every single level, every remaining feature must be scanned across every row to calculate Information Gain. Space stays at O(n)O(n) since the final node count caps out if every row ends up in its own leaf.
Prediction Phase (Classifying One Item)O(h)O(h)O(1)O(1)Classifying a new item is just answering a sequence of yes/no questions from root to leaf, checking exactly one feature per level. Memory usage is essentially nothing — the algorithm simply follows a single pointer path straight down the tree to its prediction.
Exam Theory: Balanced vs. Overfit Depthh=log2(n)h=\log_2(n) vs h=nh=nN/ATree height hh controls the entire algorithm's speed. A perfectly balanced tree achieves h=log2(n)h=\log_2(n), making predictions lightning fast. An extremely overfit tree degenerates into one long chain where h=nh=n, destroying performance and turning every prediction into a slow, linear walk.

Summary

Decision Trees abandon abstract math in favor of a rigid sequence of yes/no questions, using Information Gain to find the splits that destroy the most chaos at every level. The superpower is total transparency — a 100% human-readable flowchart instead of a mathematical black box, traceable line by line. The reality check: greedy splitting makes it a notorious overfitter. Left unchecked, it will perfectly memorize the training data row by row. Strictly limit its depth, or upgrade it into a Random Forest ensemble for real-world stability.

Decision Tree Exam Traps & FAQs

  • What happens if two features have the exact same Information Gain?

    Mathematically, they are equally good — either choice produces a valid tree, even if it looks different from a classmate's. On an exam, pick either feature confidently. To stay safe and consistent, default to whichever one comes first alphabetically, and simply note the tie on the paper.

  • I ran out of features to split on, but my leaf is still mixed. What do I do?

    This happens when the dataset has contradictory rows — identical feature values but different target labels. Splitting further is impossible at that point. Stop and apply a Majority Vote instead. If the leaf holds 2 'Spam' rows and 1 'Legit' row, simply label the leaf 'Spam' and move on.

  • My exam asks for Gini Impurity instead of Entropy. Does the whole process change?

    The process stays exactly the same — only the starting formula changes. Instead of logarithms, Gini uses simple probabilities: 1pi21-\sum p_i^2. Base impurity, weighted feature impurity, and the final subtraction for gain all work identically. Gini is simply faster to calculate by hand on a written exam.

  • How do I handle a row that has a missing value (?) for the feature I am testing?

    Written exams usually avoid this scenario entirely. If it does appear, the standard ID3 approach assigns that row the most common value for that feature within the current subset. Some lab variations instead simply ignore that specific row for the calculation and move forward.

  • How do I calculate Entropy for a column with continuous numbers like Age or Salary?

    Raw numbers are never used directly in standard ID3. Sort the values, find every point where the target class changes, and treat those midpoints as binary threshold questions like 'Is Age greater than 25?' Calculate Information Gain for that binary split exactly like any categorical feature.

Core University Curriculum

This algorithm and its manual calculation methods are foundational requirements in leading Computer Science and Software Engineering programs worldwide. You will find this topic heavily featured in the syllabi of these standard AI courses:

Explore Related Algorithms