Decision Tree

Decision trees are a very easy concept to understand, even for an 8th-grade student. For example, if it rains today, I will get a jacket; otherwise, I will get a t-shirt. This is the basic definition. On the other hand, we also have nested decisions, where one decision depends on another decision. For example, in a restaurant, if you order a meal, you will get your meal, but the waiter will also ask if you want anything else. Based on your previous decision, you will make another decision, such as choosing a drink to go along with the food you ordered.

In the computer world, there are only two decisions: true and false, or 0 and 1. We can use this concept to create our own decision tree algorithm.

const isEven = number => number % 2 == 0

isEven(2) // True
isEven(3) // False

As you can see, this code will return true if the given number is even, otherwise false. Let's make it a little more attractive by combining it with other code snippets.

function decision(conditionFunction, trueOutcome, falseOutcome) {
  return function(context) {
    if (conditionFunction(context)) {
      return trueOutcome;
    } else {
      return falseOutcome;
    }
  };
}

evenOddDecision = decision(isEven, "The number is even", "The number is odd")

evenOddDecision(2) // The number is even
evenOddDecision(5) // The number is odd

We have updated the code to provide a more user-friendly response. However, we haven't handled all possibilities, such as invalid user input or the number being zero. Let's add those conditions as well.

// Decision API
const decision = (conditionFunction, trueOutcome, falseOutcome) =>
(context) => conditionFunction(context) ? trueOutcome : falseOutcome;

const isEven = number => number % 2 == 0
const isValidNumber = (number) => !isNaN(number) && isFinite(number);

In the code snippet above, we have refactored the decision function and added the isValidNumber function. The algorithm now checks if the number is valid and then decides if it is even or odd.

const evenOddDecision = decision(isValidNumber,   
  decision(isEven, "The number is even", "The number is odd"), 
  "The given number is not valid")

In this example, the evenOddDecision uses the decision function with the condition of isValidNumber. If the number is valid, it further checks if it's even or odd and returns the appropriate outcome. If the number is not valid, it simply returns the message "The given number is not valid".

Decision trees can be more complex and nested, allowing for more sophisticated decision-making. This concept is used in various applications, such as movie recommendation systems. For example, a decision tree for movie recommendations may include conditions like:

  • Is the movie released between 2010 and 2023? If not, reject the movie.
  • Is the genre of the movie romance or comedy? If not, reject the movie.
  • Is the movie's duration between 90 and 120 minutes? If not, reject the movie.
  • Is the movie's rating greater than 7? If not, reject the movie.

By applying these filters, the decision tree can select movies that satisfy all the conditions and provide personalized recommendations.