代码拉取完成,页面将自动刷新
package week6;
import week6.jsjf.LinkedBinaryTree;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* The DecisionTree1 class uses the LinkedBinaryTree class to implement
* a binary decision tree. Tree elements are read from a given file and
* then the decision tree can be evaluated based on user input using the
* evaluate method.
*
* @author Lewis and Chase
* @version 4.0
*/
public class DecisionTree
{
private LinkedBinaryTree<String> tree;
/**
* Builds the decision tree based on the contents of the given file
* @param filename the name of the input file
* @throws FileNotFoundException if the input file is not found
*/
public DecisionTree(String filename) throws FileNotFoundException
{
File inputFile = new File(filename);
Scanner scan = new Scanner(inputFile);
int numberNodes = scan.nextInt();
scan.nextLine();
int root = 0, left, right;
List<LinkedBinaryTree<String>> nodes = new ArrayList<LinkedBinaryTree<String>>();
for (int i = 0; i < numberNodes; i++)
nodes.add(i,new LinkedBinaryTree<>(scan.nextLine()));
while (scan.hasNext())
{
root = scan.nextInt();
left = scan.nextInt();
right = scan.nextInt();
scan.nextLine();
nodes.set(root, new LinkedBinaryTree<>((nodes.get(root)).getRootElement(), nodes.get(left), nodes.get(right)));
}
tree = nodes.get(root);
}
/**
* Follows the decision tree based on user responses.
*/
public void evaluate()
{
LinkedBinaryTree<String> current = tree;
Scanner scan = new Scanner(System.in);
while (current.size() > 1)
{
System.out.println (current.getRootElement());
if (scan.nextLine().equalsIgnoreCase("N"))
current = current.getLeft();
else
current = current.getRight();
}
System.out.println ( current.getRootElement());
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。