What are Decision Trees in C Programming ? See Examples

In programming, especially when working with C, we often deal with complex decision-making situations. These decisions involve choosing an execution path based on multiple conditions. To make this process simpler and error-free, programmers use Decision Trees and Decision Tables before writing the actual code.

These tools help visualize logic, handle multiple conditions clearly, and reduce mistakes in conditional branching.


1. What Are Decision Trees?

A Decision Tree is a graphical representation of decisions and their possible outcomes. It uses a tree-like structure where each node represents a condition or decision, and each branch represents the result of that decision.

Why Use Decision Trees?

  • To handle complex nested conditions
  • To visualize multi-level decision-making
  • To avoid confusion in if-else chains
  • To check all possible paths easily

Decision trees are especially useful when many conditions depend on each other.


2. Structure of a Decision Tree

A decision tree contains:

  • Root Node: The first main decision
  • Branches: Yes/No or True/False paths
  • Internal Nodes: Sub-decisions
  • Leaf Nodes: Final outcome or action

3. Example of a Decision Tree in C Programming

Problem:

Write a program that decides if a person is eligible to vote and if they need a voter ID.

Logic:

  • Must be 18 or older to vote.
  • If age ≥ 18, check if they have a voter ID.

Decision Tree Diagram (Text Form)

                     [Start]
                        |
                 Is age >= 18?
                 /            \
              Yes              No
              |                |
   Do you have voter ID?     [Not Eligible]
         /       \
      Yes         No
      |           |
 [Eligible]   [Apply for ID]

Equivalent C Code

#include <stdio.h>

int main() {
    int age, id;

    printf("Enter age: ");
    scanf("%d", &age);

    if (age >= 18) {
        printf("Do you have a voter ID? (1 for Yes, 0 for No): ");
        scanf("%d", &id);

        if (id == 1)
            printf("Eligible to vote.");
        else
            printf("Eligible but must apply for Voter ID.");
    } else {
        printf("Not eligible to vote.");
    }
    return 0;
}

Decision trees help plan these nested decisions clearly.


4. What Are Decision Tables?

A Decision Table is a structured way to represent complex logic using rows and columns.
It lists all possible conditions and the resulting actions in a tabular format.

Why Use Decision Tables?

  • Ensures no combination of conditions is missed
  • Best for business logic or rule-based programs
  • Avoids logical duplication
  • Easy to read and maintain

Decision tables are especially useful when multiple conditions can occur together.


5. Structure of a Decision Table

A decision table consists of:

  • Condition Stub: List of conditions
  • Condition Entries: True/False or values
  • Action Stub: List of possible actions
  • Action Entries: What action happens when conditions are met

6. Example of a Decision Table in C

Problem:

Determine electricity bill discount based on two conditions:

  1. Customer Type (Domestic / Commercial)
  2. Units Consumed (>500 or ≤500)

Decision Table

Condition / RuleRule 1Rule 2Rule 3Rule 4
Customer Type = DomesticTTFF
Units > 500TFTF
Action
Discount = 20%YesNoNoNo
Discount = 10%NoYesYesNo
No DiscountNoNoNoYes

Interpretation of Rules

  • Rule 1: Domestic + Units > 500 → 20% Discount
  • Rule 2: Domestic + Units ≤ 500 → 10% Discount
  • Rule 3: Commercial + Units > 500 → 10% Discount
  • Rule 4: Commercial + Units ≤ 500 → No Discount

C Code Implementation

#include <stdio.h>

int main() {
    char type;
    int units;
    float discount = 0;

    printf("Enter Customer Type (D/C): ");
    scanf(" %c", &type);

    printf("Enter Units Consumed: ");
    scanf("%d", &units);

    if (type == 'D' || type == 'd') {            // Domestic
        if (units > 500)
            discount = 20;
        else
            discount = 10;
    }
    else if (type == 'C' || type == 'c') {       // Commercial
        if (units > 500)
            discount = 10;
        else
            discount = 0;
    }

    printf("Applicable Discount: %.2f%%", discount);
    return 0;
}

7. Decision Trees vs Decision Tables

FeatureDecision TreeDecision Table
Visual FormDiagram (tree structure)Table (rows & columns)
Best ForStep-by-step decisionsMultiple combined conditions
Detection OfFlow errorsMissing condition combinations
Easy ForBeginnersSystem analysts

8. When to Use What?

Use a Decision Tree When:

  • Decisions occur one after another
  • There are hierarchical “if-else” conditions
  • You want a clear visual of the flow

Use a Decision Table When:

  • Multiple conditions interact simultaneously
  • You need to test all combinations
  • You want to avoid missing edge cases

Conclusion

Decision Trees and Decision Tables are powerful tools that make programming logic clear, structured, and error-free. They help visualize conditions, organize logic, and avoid mistakes before writing code in C. Whether you’re a student or a professional, using these methods improves problem-solving and builds better programs.


🔗 View other articles about PHP:

https://savanka.com/category/learn/c-programming

🔗 External PHP Documentation:

https://www.w3schools.com/c

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *