package jhpro.dtree;
/**
*
* Construct a decision tree.
* 
*
**/

import java.io.*;
import java.io.Serializable;


public class DecisionTree implements Serializable  {

	
	static BufferedReader    keyboardInput = new
	                BufferedReader(new InputStreamReader(System.in));
	public static BinTree rootNode = null;

	public String description="";

	/* ------------------------------------ */
	/*                                      */
	/*              CONSTRUCTORS            */
	/*                                      */
	/* ------------------------------------ */

	
	
	/**
	* Default constructor of the tree.
	*
	**/
	public DecisionTree() {
	}

	
	
	/**
	* Create ROOT of the tree.
	*
	**/
	public void createRoot(int newNodeID, String newQuestAns) {
		rootNode = new BinTree(newNodeID,newQuestAns);
		System.out.println("Created root node " + newNodeID);
	}

	
	
	/**
	* Add YES to this 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 the tree.
	**/
	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);
		}
		else {
			// Try yes branch if it exists
			if (currentNode.yesBranch != null) {
				if (searchTreeAndAddYesNode(currentNode.yesBranch,
				                            existingNodeID,newNodeID,newQuestAns)) {
					return(true);
				}
				else {
					// Try no branch if it exists
					if (currentNode.noBranch != null) {
						return(searchTreeAndAddYesNode(currentNode.noBranch,
						                               existingNodeID,newNodeID,newQuestAns));
					}
					else return(false);	// Not found here
				}
			}
			return(false);		// Not found here
		}
	}

	
	
	/**
	* Add NO mode to the tree.
	**/
	public void addNoNode(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 (searchTreeAndAddNoNode(rootNode,existingNodeID,newNodeID,newQuestAns)) {
			System.out.println("Added node " + newNodeID +
			                   " onto \"no\" branch of node " + existingNodeID);
		}
		else System.out.println("Node " + existingNodeID + " not found");
	}


	
	 /**
	* Search the tree.
	**/

	private boolean searchTreeAndAddNoNode(BinTree currentNode,
	                                       int existingNodeID, int newNodeID, String newQuestAns) {
		if (currentNode.nodeID == existingNodeID) {
			// Found node
			if (currentNode.noBranch == null) currentNode.noBranch = new
				                BinTree(newNodeID,newQuestAns);
			else {
				System.out.println("WARNING: Overwriting previous node " +
				                   "(id = " + currentNode.noBranch.nodeID +
				                   ") linked to yes branch of node " +
				                   existingNodeID);
				currentNode.noBranch = new BinTree(newNodeID,newQuestAns);
			}
			return(true);
		}
		else {
			// Try yes branch if it exists
			if (currentNode.yesBranch != null) {
				if (searchTreeAndAddNoNode(currentNode.yesBranch,
				                           existingNodeID,newNodeID,newQuestAns)) {
					return(true);
				}
				else {
					// Try no branch if it exists
					if (currentNode.noBranch != null) {
						return(searchTreeAndAddNoNode(currentNode.noBranch,
						                              existingNodeID,newNodeID,newQuestAns));
					}
					else return(false);	// Not found here
				}
			}
			else return(false);	// Not found here
		}
	}

	
	
	
	/**
	* Query the tree for a given node
	* @param currentNode current node.
	**/

	public BinTree  queryBinTree(BinTree currentNode) throws IOException {

		// Test for leaf node (answer) and missing branches

		if (currentNode.yesBranch==null) {
			if (currentNode.noBranch==null) return currentNode; // System.out.println("Answer="+currentNode.questOrAns);

			else System.out.println("Error: Missing \"Yes\" branch at \"" +
				                        currentNode.questOrAns + "\" question");
			// System.out.println(currentNode.toString());
			return currentNode;
		}
		if (currentNode.noBranch==null) {
			System.out.println("Error: Missing \"No\" branch at \"" +
			                   currentNode.questOrAns + "\" question");
			// System.out.println(currentNode.toString());
			return currentNode;
		}

		// Question
		// askQuestion(currentNode);

		return currentNode;
	}

	private void askQuestion(BinTree currentNode) throws IOException {
		System.out.println(currentNode.questOrAns + " (enter \"Yes\" or \"No\")");
		String answer = keyboardInput.readLine();
		if (answer.equals("Yes")) queryBinTree(currentNode.yesBranch);
		else {
			if (answer.equals("No")) queryBinTree(currentNode.noBranch);
			else {
				System.out.println("ERROR: Must answer \"Yes\" or \"No\"");
				askQuestion(currentNode);
			}
		}
	}

	
	
	/**
	* Show the tree as a string
	* 
	*/
	public String outputBinTree() {

		return outputBinTree("1",rootNode);
	}

	private String outputBinTree(String tag, BinTree currentNode) {

		// Check for empty node
		if (currentNode == null) return description;

		// Output
		description=description+"[" + tag + "] nodeID = " + Integer.toString(currentNode.nodeID) +
		            ", question/answer = " + currentNode.questOrAns+"\n";

		// Go down yes branch
		outputBinTree(tag + ".1",currentNode.yesBranch);

		// Go down no branch
		outputBinTree(tag + ".2",currentNode.noBranch);

		return description;
	}
}



 

Ads help maintain this website.