2 Star 0 Fork 0

CS-IMIS-23/GK20172301_JavaProgramming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
DecisionTree.java 2.20 KB
一键复制 编辑 原始数据 按行查看 历史
package week6;
import java.io.*;
import java.util.*;
public class DecisionTree
{
private LinkedBinaryTree<String> tree;
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 java.util.ArrayList<LinkedBinaryTree<String>>();
for (int i = 0; i < numberNodes; i++)
nodes.add(i,new LinkedBinaryTree<String>(scan.nextLine()));
while (scan.hasNext())
{
root = scan.nextInt();
left = scan.nextInt();
right = scan.nextInt();
scan.nextLine();
nodes.set(root, new LinkedBinaryTree<String>((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());
}
private static int count = 0;
public int CountLeaf ()
{
count = 0;
CountLeaf(tree);
return count;
}
private void CountLeaf(LinkedBinaryTree<String> tree)
{
if (!(tree == null))
{
if ((tree.getLeft() == null) && (tree.getRight() == null))
{
count++;
}
CountLeaf(tree.getLeft());
CountLeaf(tree.getRight());
}
}
public int hight()
{
int a = tree.getHeight();
return a;
}
public void leveltoString()
{
tree.LevelOrderToString();
}
public void leveltoString2()
{
tree.LevelOrderToString2();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/GK20172301_JavaProgramming.git
git@gitee.com:CS-IMIS-23/GK20172301_JavaProgramming.git
CS-IMIS-23
GK20172301_JavaProgramming
GK20172301_JavaProgramming
master

搜索帮助