How to Build a Decision Tree in Java Open Source

Since Sun Microsystems releases Java as an open source platform, Sun versions of the development kit (JDK) can be considered open source. Because of this, open source programmers can utilize standard Java libraries for any program. This means that the basics of Java programming can go towards open or closed source applications. For example, you can develop a very basic Java decision tree using free Java tools such as the JDK.

Things You'll Need

  • Java development environment
Show More

Instructions

    • 1

      Create a Decision Tree class:

      import java.io.*;

      class DTree{

      }

    • 2

      Inside the DTree class, create an internal Node class to represent decision nodes:

      class Node{

      String Question = null;
      String answer = null;
      Node yes = null;
      Node no = null
      }

    • 3

      Set up a question answer schematic. For this example, use four animals: horse, zebra, dog and cat. The decision tree will ask a series of questions to lead a user to a particular animal. The first question asks "Do people keep this animal in the house?" The answer will split into two nodes based on a yes or no answer. The next nodes ask one of two questions: "Does this animal have stripes?" and "Does this animal bark?"

    • 4

      Construct the tree based on the questions. The first question goes in the "question" string of the head node, which connects to the second two questions based on "yes" or "no" questions. The following nodes have their own questions, and their own set of answers:

      Node head = new Node();
      head.question = "Do people keep this animal in the house?";
      Node temp = head.no = new Node();

      temp.question = "Does this animal have stripes?";
      temp.yes = new Node();
      temp.yes.answer = "Zebra";
      temp.no.answer = "Horse";

      temp = head.yes = new Node;
      temp.question = "Does this animal bark?";
      temp.yes.answer = "Dog"
      temp.no.answer = "Cat"

Related Searches:

References

Comments

Related Ads

Featured