Ai
2 Star 0 Fork 0

CS-IMIS-23/20172302侯泽洋

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
DecisionTree.java 3.48 KB
一键复制 编辑 原始数据 按行查看 历史
20172302 提交于 2018-10-27 21:07 +08:00 . 计算叶子结点个数及树的高度
package chapter10;
import chapter10.jsjf.BinaryTreeNode;
import chapter10.jsjf.LinkedBinaryTree;
import chapter6.jsjf.ArrayUnorderedList;
import java.io.*;
import java.util.*;
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<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);
}
public LinkedBinaryTree<String> getTree()
{
return tree;
}
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());
}
public void levelOrder(LinkedBinaryTree<String> linkedBinaryTree) {
if (linkedBinaryTree == null) {
return;
}
int depth = linkedBinaryTree.getHeight();
for (int i = 1; i <= depth; i++) {
levelOrder(linkedBinaryTree, i);
System.out.println("\n\n");
}
}
private void levelOrder(LinkedBinaryTree<String> linkedBinaryTree, int level) {
if (linkedBinaryTree == null || level < 1) {
return;
}
if (level == 1) {
System.out.print(linkedBinaryTree.getRootElement() + " ");
return;
}
// 左子树
levelOrder(linkedBinaryTree.getLeft(), level - 1);
// 右子树
levelOrder(linkedBinaryTree.getRight(), level - 1);
}
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 getHeight()
{
return height(tree.getRootNode());
}
private int height(BinaryTreeNode<String> node)
{
if(node==null){
return 0;
}
else {
int leftTreeHeight = height(node.getLeft());
int rightTreeHeight= height(node.getRight());
return leftTreeHeight>rightTreeHeight ? (leftTreeHeight+1):(rightTreeHeight+1);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/Java-pro.git
git@gitee.com:CS-IMIS-23/Java-pro.git
CS-IMIS-23
Java-pro
20172302侯泽洋
master

搜索帮助