0% found this document useful (0 votes)
5 views9 pages

Java Decision Tree Implementation

This document defines classes for creating and manipulating a binary decision tree. The DecisionTree class contains methods for creating nodes, adding child nodes to existing nodes based on yes/no answers, and outputting the tree. The fdtree class generates a sample tree by calling the DecisionTree methods and passing a Vector of sample question/answer strings.

Uploaded by

gopal5388123
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Java Decision Tree Implementation

This document defines classes for creating and manipulating a binary decision tree. The DecisionTree class contains methods for creating nodes, adding child nodes to existing nodes based on yes/no answers, and outputting the tree. The fdtree class generates a sample tree by calling the DecisionTree methods and passing a Vector of sample question/answer strings.

Uploaded by

gopal5388123
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

*;

import [Link].*;

class DecisionTree

private class BinTree

private int nodeID;

private String questOrAns=null;

private String eq = null;

private String mark = null;

private String arrears = null;

private BinTree yesBranch = null;

private BinTree noBranch = null;

/* CONSTRUCTOR */

public BinTree(int newNodeID, String newQuestAns)

nodeID = newNodeID;

questOrAns = newQuestAns;

}//end of class Bin Tree

/* OTHER FIELDS */

//static BufferedReader keyboardInput = new

//BufferedReader(new InputStreamReader([Link]));
BinTree rootNode = null;

public DecisionTree() { }

/* CREATE ROOT NODE */

public void createRoot(int newNodeID, String newQuestAns)

rootNode = new BinTree(newNodeID,newQuestAns);

//[Link]("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)

//[Link]("ERROR: No root node!");

return;

// Search tree

if (searchTreeAndAddYesNode(rootNode,existingNodeID,newNodeID,newQuestAns))

{
//[Link]("Added node " + newNodeID +

//" onto \"yes\" branch of node " + existingNodeID);

//else //[Link]("Node " + existingNodeID + " not found");

/* SEARCH TREE AND ADD YES NODE */

private boolean searchTreeAndAddYesNode(BinTree currentNode,

int existingNodeID, int newNodeID, String newQuestAns)

if ([Link] == existingNodeID)

// Found node

if ([Link] == null) [Link] = new

BinTree(newNodeID,newQuestAns);

else

//[Link]("WARNING: Overwriting previous node " +

//"(id = " + [Link] +

//") linked to yes branch of node " +

//existingNodeID);

[Link] = new BinTree(newNodeID,newQuestAns);

return(true);

else

// Try yes branch if it exists


if ([Link] != null) {

if (searchTreeAndAddYesNode([Link],

existingNodeID,newNodeID,newQuestAns))

return(true);

else

// Try no branch if it exists

if ([Link] != null)

return(searchTreeAndAddYesNode([Link],

existingNodeID,newNodeID,newQuestAns));

else return(false); // Not found here

return(false); // Not found here

/* ADD NO NODE */

public void addNoNode(int existingNodeID, int newNodeID, String newQuestAns)

// If no root node do nothing

if (rootNode == null)

{
//[Link]("ERROR: No root node!");

return;

// Search tree

if (searchTreeAndAddNoNode(rootNode,existingNodeID,newNodeID,newQuestAns))

//[Link]("Added node " + newNodeID +

// " onto \"no\" branch of node " + existingNodeID);

// else //[Link]("Node " + existingNodeID + " not found");

/* SEARCH TREE AND ADD NO NODE */

private boolean searchTreeAndAddNoNode(BinTree currentNode,

int existingNodeID, int newNodeID, String newQuestAns)

if ([Link] == existingNodeID)

// Found node

if ([Link] == null)

[Link] = new BinTree(newNodeID,newQuestAns);

else {

//[Link]("WARNING: Overwriting previous node " +

//"(id = " + [Link] +

//") linked to yes branch of node " +

//existingNodeID);
[Link] = new BinTree(newNodeID,newQuestAns);

return(true);

else

// Try yes branch if it exists

if ([Link] != null)

if (searchTreeAndAddNoNode([Link],

existingNodeID,newNodeID,newQuestAns))

return(true);

else

// Try no branch if it exists

if ([Link] != null)

return(searchTreeAndAddNoNode([Link],

existingNodeID,newNodeID,newQuestAns));

else return(false); // Not found here

else return(false); // Not found here

/* OUTPUT BIN TREE */


public void outputBinTree()

outputBinTree("1",rootNode);

private void outputBinTree(String tag, BinTree currentNode)

// Check for empty node

if (currentNode == null) return;

// Output

[Link]("[" + tag + "] nodeID = " + [Link] +

", question/answer = " + [Link]);

// Go down yes branch

outputBinTree(tag + ".1",[Link]);

// Go down no branch

outputBinTree(tag + ".2",[Link]);

}//end of class Decesion tree

// DECISION TREE APPLICATION


//import [Link].*;

class fdtree

//static BufferedReader keyboardInput = new

//BufferedReader(new InputStreamReader([Link]));

static DecisionTree newTree;

/* MAIN */

public fdtree(Vector a) throws IOException

// Create instance of class DecisionTree

newTree = new DecisionTree();

// Generate tree

// nodebean obj=new nodebean();

// Vector a=[Link]();

generateTree(a);

[Link]("decisition tree formed");

// Output tree
[Link]("\nOUTPUT DECISION TREE");

[Link]("====================");

[Link]();

// Query tree

// queryTree();

/* GENERATE TREE */

static void generateTree(Vector a)

[Link]("\nGENERATE DECISION TREE");

[Link]("======================");

[Link](1,(String)[Link](0));

[Link](1,2,(String)[Link](1));

[Link](1,3,(String)[Link](2));

[Link](2,4,(String)[Link](3));

[Link](2,5,(String)[Link](4));

[Link](3,6,(String)[Link](5));

[Link](3,7,(String)[Link](6));

You might also like