A decision tree classifies data by testing one feature at a time and following the corresponding branches until it reaches a leaf node. That leaf node provides the predicted class.
A decision tree is a supervised learning model, meaning it is trained using data containing input features and known class labels. During training, the algorithm repeatedly selects feature tests that divide the training examples into increasingly pure groups.
| Tree component | Function |
|---|---|
| Root node | Applies the first feature test to every input. |
| Internal node | Applies another test after an earlier decision. |
| Branch | Represents an outcome of a test. |
| Leaf node | Stores the final predicted class. |
For example, a tree classifying emails as spam or not spam might first test whether the sender is known. If the sender is unknown, it might then test whether the email contains more than two links. An unknown sender combined with more than two links could lead to a leaf labelled “spam.”
During training, a splitting criterion such as information gain or Gini impurity may be used to select each test. The aim is to produce child nodes containing examples that mainly belong to one class. For a new email, the tree does not retrain; it simply applies the learned tests from the root to a leaf. This stage is called inference or prediction.
A common misconception is that a decision tree compares a new item with every training example. It does not. It follows only one path through the trained tree. A tree that becomes excessively deep may also show overfitting, performing well on training data but poorly on unseen data; pruning or depth limits can reduce this.
In an IB Computer Science HL response, clearly distinguish training from classification. For an “explain” question, describe the complete sequence: test a feature, follow a branch, repeat at internal nodes, and output the class stored at the leaf.