BIT253 ยท 2081 ยท 5 marks
How do you model logic using decision tree? Explain with an example.
How do you model logic using decision tree? Explain with an example. [5]
Quick answer
A decision tree is a tree-structured model used to represent decisions and their possible consequences. It models logical conditions (if-then-else logic) in a hierarchical, visual form that is easy to understand and implement.
Modeling Logic Using Decision Trees
What is a Decision Tree?
Structure of a Decision Tree
| Component | Description |
|---|---|
| Root Node | The topmost node representing the first/main decision or condition |
| Internal Node | Represents a test on an attribute or condition |
| Branch | Represents the outcome (Yes/No, True/False) of a test |
| Leaf Node | Represents the final decision, class label, or result |
How Logic is Modeled
Each internal node corresponds to a logical condition (predicate), and each branch corresponds to the truth value of that condition. The path from root to a leaf encodes a conjunction of conditions (AND logic), while multiple paths to the same result encode disjunction (OR logic).
A decision tree is essentially a graphical representation of nested if-then-else statements.
Example: Deciding Whether to Play Cricket
Conditions (Attributes):
- Weather: Sunny / Rainy
- Wind: Strong / Weak
Decision Table:
| Weather | Wind | Play Cricket? |
|---|---|---|
| Sunny | Weak | Yes |
| Sunny | Strong | No |
| Rainy | Weak | Yes |
| Rainy | Strong | No |
Decision Tree:
[Weather?]
/ \
Sunny Rainy
| |
[Wind?] [Wind?]
/ \ / \
Weak Strong Weak Strong
| | | |
YES NO YES NO
Equivalent Logical Expression
Reading the YES paths from the tree:
Play Cricket = (Weather = Sunny AND Wind = Weak)
OR
(Weather = Rainy AND Wind = Weak)
This can be simplified as:
Play Cricket = Wind = Weak
This shows how a decision tree discovers and models logical rules from data.
Equivalent If-Then-Else Code
if Weather == "Sunny":
if Wind == "Weak":
return "Yes"
else:
return "No"
else: # Rainy
if Wind == "Weak":
return "Yes"
else:
return "No"
Key Points
- Each path in the tree represents a logical rule
- Decision trees can model AND, OR, and NOT logic
- They are used in machine learning, expert systems, and database query optimization
- They convert complex logical conditions into a simple, readable tree structure
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.