Successfully reported this slideshow.
Upcoming SlideShare
×

# Decision tree handson

334 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Decision tree handson

1. 1. Decision Tree Implementation 1. INTRODUCTION Decision trees are an important structure used in many branches of Computer Science (e.g. Classification, Artificial Intelligence etc.). The tree comprises a set of nodes commencing with a single root node and terminating at a set of leaf nodes, in between are located body nodes. The root and each body node have connections (called arcs or edges) to at least two other body or leaf nodes. A tree where the root and body nodes have uniformly two arcs each is called a binary tree. At its simplest the leaf nodes in a decision tree represent a set of terminating "answers", the root and body nodes then represent "questions". The user arrives at an answer by providing responses to the questions. The nature of the response to a particular question then dictates which arc should be followed to the next question (or answer if a leaf node is arrived at). In the case of a binary decision tree the responses are typically Yes or No, each corresponding to one of the two available branches at each question node. From an implementation view point a binary tree is like a linked list but with an extra "link". 2. A GENERIC BINARY DECISION TREE GENERATOR AND QUERY SYSTEM Decision trees are so common that it would seem to be a useful expedient to write a Java program that builds and queries such trees. The class presented in Table 1 does this with respect to binary decision trees. The class has, as one of its fields, another class (an inner class) which defines a node in a generic decision tree. This node class (BinTree) has four field: 1. nodeID: An identification number which serves no other purpose than to aid the tree construction process. 2. questOrAns: the "question" in the case of a root or body node, or "answer" in the case of a leaf node. 3. yesBranch: Another instance of the class BinTree describing the "Yes" branch (this would be set to null in the case of a leaf node). 4. noBranch: Same as yesBranch field but describing the "No" branch. The code has three groups of methods:
2. 2. 1. Tree generation methods: A method to create the root node and then further methods to add body and leaf nodes. We add a body or leaf node by providing the nodeID number for the node we wish to add to. The existing tree is then searched until this node is found (in which case we add the new node), or there are no more nodes in the tree (in which case we report an error). Note that: (1) the tree is built commencing from the root node, and (2) it is up to the user to ensure that the tree is properly "balanced". 2. Output methods: A set of methods to output the tree once it has been built. This is really for diagnostic purposes only. Note that nodes are number according to there level in the tree and whether they are a "Yes" (1) or "No" (2) nodes. 3. Query methods: A set of methods to facilitate querying of the tree. // DECISION TREE import java.io.*; class DecisionTree { /* ------------------------------- */ /* */ /* FIELDS */ /* */ /* ------------------------------- */ /* NESTED CLASS */ private class BinTree { /* FIELDS */ private int nodeID; private String questOrAns = null; private BinTree yesBranch = null; private BinTree noBranch = null; /* CONSTRUCTOR */ public BinTree(int newNodeID, String newQuestAns) { nodeID = newNodeID; questOrAns = newQuestAns; } } /* OTHER FIELDS */ static BufferedReader keyboardInput = new BufferedReader(new InputStreamReader(System.in)); BinTree rootNode = null; /* ------------------------------------ */ /* */ /* CONSTRUCTORS */ /* */ /* ------------------------------------ */
3. 3. /* Default Constructor */ public DecisionTree() { } /* ----------------------------------------------- */ /* */ /* TREE BUILDING METHODS */ /* */ /* ----------------------------------------------- */ /* CREATE ROOT NODE */ public void createRoot(int newNodeID, String newQuestAns) { rootNode = new BinTree(newNodeID,newQuestAns); System.out.println("Created root node " + newNodeID); } /* ADD YES NODE */ public void addYesNode(int existingNodeID, int newNodeID, String newQuestAns) { // If no root node do nothing if (rootNode == null) { System.out.println("ERROR: No root node!"); return; } // Search tree if (searchTreeAndAddYesNode(rootNode,existingNodeID,newNodeID,newQuestAns)) { System.out.println("Added node " + newNodeID + " onto "yes" branch of node " + existingNodeID); } else System.out.println("Node " + existingNodeID + " not found"); } /* SEARCH TREE AND ADD YES NODE */ private boolean searchTreeAndAddYesNode(BinTree currentNode, int existingNodeID, int newNodeID, String newQuestAns) { if (currentNode.nodeID == existingNodeID) { // Found node if (currentNode.yesBranch == null) currentNode.yesBranch = new BinTree(newNodeID,newQuestAns); else { System.out.println("WARNING: Overwriting previous node " + "(id = " + currentNode.yesBranch.nodeID + ") linked to yes branch of node " + existingNodeID); currentNode.yesBranch = new BinTree(newNodeID,newQuestAns); } return(true);