Decision trees
Decision trees
We may represent a decision-making process diagrammatically with a decision tree. Decision trees are commonly used for species identification.1
Here’s an example:
Here, we start on the left and move toward the right, making decisions along the way. Notice that at each branching point we have two branches.
So, for example, to reach “watermelon, cantaloupe”, we make the decisions: soft inside, small seeds, thick skin, and unsegmented. To reach “peach”, we’d have to have made the decisions: soft inside, pit or stone, and fuzzy.
How do we encode these decision points? One way is to treat them as yes or no questions. So if we ask “Is the fruit soft inside?” then we have a yes or no answer. If the answer is “no”, then we know the fruit is not soft inside and thus must be hard inside (like a walnut or almond).
Here’s a snippet of Python code, demonstrating a single question:
= input('Is the fruit soft inside? y/n ')
response if response == 'y':
# we know the fruit is soft inside
# ...
else:
# we know the fruit is hard inside
# ...
We can write a program that implements this decision tree by using multiple, nested if
statements.
"""
CS 1210
Decision tree for fruit identification
"""
= input('Is the fruit soft inside? y/n ')
response if response.lower() == 'y':
# soft inside
= input('Does it have small seeds? y/n ')
response if response.lower() == 'y':
# small seeds
= input('Does it have a thin skin? y/n ')
response if response.lower() == 'y':
# thin skin
print("Tomato")
else:
# thick skin
= input('Is it segmented? y/n ')
response if response.lower() == 'y':
# segmented
print("Orange or lemon")
else:
# unsegmented
print("Watermelon or cantaloupe")
else:
# pit or stone
= input('Is it fuzzy? y/n ')
response if response.lower() == 'y':
# segmented
print("Peach")
else:
# unsegmented
print("Plum")
else:
# hard inside
print("Walnut or almond")
Comprehension check
In the decision tree above, Figure 1, which decisions lead to plum? (There are three.)
Revisit ?@sec-if_elif_else and draw decision trees for the code examples shown.
Copyright © 2023–2025 Clayton Cafiero
No generative AI was used in producing this material. This was written the old-fashioned way.
Footnotes
If you’ve had a course in biology, you may have heard of a cladogram for representing taxonomic relationships of organisms. A cladogram is a kind of decision tree. If you’re curious, see: and similar applications.↩︎