1 Star 0 Fork 0

20172304段志轩 / 段志轩20172304java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
DecisionTree.java 2.12 KB
一键复制 编辑 原始数据 按行查看 历史
20172304段志轩 提交于 2018-11-08 19:29 . 决策树方法类
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());
}
}
1
https://gitee.com/duanzhixuan/duan_zhixuan_20172304java.git
git@gitee.com:duanzhixuan/duan_zhixuan_20172304java.git
duanzhixuan
duan_zhixuan_20172304java
段志轩20172304java
master

搜索帮助