我正在做一个项目,它与二叉树和植物 key 有关,现在我需要做的是将机器人 key 加载到程序中,然后工作能够按照我的方式按顺序到达每个节点确定树的类型,当我到达叶节点时,它将显示树的类型。现在我在移动它的部分时遇到了麻烦,我不知道如何让它工作,我尝试了很多不同的事情,包括预遍历,我只是想知道是否有人可以帮我把它从可以这么说。我需要编辑的程序部分的代码如下,“chooseOption”方法是用于遍历树的方法。 提前致谢。
/* * 要更改此模板,请选择“工具”|“模板 * 并在编辑器中打开模板。 */
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Stack;
/**
*
* @author Alex Drinkwater 10077473
*/
public class BotKey implements IBotKey {
private Node root;
private Node currentNode;
// This stack will come in handy when you implement the "undo" facility
private Stack<Node> choiceStack = new Stack<Node>();
@Override
public boolean isLoaded() {
return root != null;
}
@Override
public int getNbrChildren() {
if (currentNode == null) {
return 0;
} else {
return currentNode.getNbrChildren();
}
}
@Override
public String getLeadStatement(int i) {
return currentNode.getChildStatement(i);
}
@Override
public void chooseOption(int i) {
//You need to implement this method
if (root != null) {
root.getChildNode(i);
root.getChildStatement(i);
}
}
@Override
public void undoChoice() {
//You need to implement this method
}
@Override
public String getResults() {
//Dummy implementation. You need to supply a proper one
if (currentNode.isLeaf() == true) {
currentNode.getName();
}
else{
String s =currentNode + "";
if(currentNode!= null){
}
}
return "s";
}
@Override
public void loadExample1() {
Node southernLive = new Node("Southern live Oak: Quercus virginiana");
Node dwarfLive = new Node("Dwarf live oak: Quercus minima");
Node willow = new Node("Willow oak");
Node shingle = new Node("Shingle oak");
Node blackJack = new Node("Blackjack Oak");
Node northernRed = new Node("Northern red oak");
Node white = new Node("White oak Quercus alba");
Node swampChestnut = new Node("Swamp chestnut oak Quercus prinus");
Node node7 = new Node();
node7.addChild(white, "Leaves with 5-9 deep lobes");
node7.addChild(swampChestnut, "Leaves with 21-27 shallow lobes");
Node node6 = new Node();
node6.addChild(blackJack, "Leaves mostly with 3 lobes");
node6.addChild(northernRed, "Leaves mostly with 7-9 lobes");
Node node5 = new Node();
node5.addChild(node7,
"Lobes or teeth rounded or blunt-pointed, no bristles");
node5.addChild(node6, "Lobes or teeth bristle-tipped");
Node node4 = new Node();
node4.addChild(willow, "Leaf narrow, about 4-6 times as long as broad");
node4.addChild(shingle, "Leaf broad, about 2-3 times as long as broad");
Node node3 = new Node();
node3.addChild(dwarfLive, "Mature plant a small shrub");
node3.addChild(southernLive, "Mature plant a large tree");
Node node2 = new Node();
node2.addChild(node4, "Leaves not evergreen");
node2.addChild(node3, "Leaves evergreen");
Node node1 = new Node();
node1.addChild(node5, "Leaves usually with teeth or lobes");
node1.addChild(node2, "Leaves usually without teeth or lobes");
root = node1;
currentNode = root;
}
@Override
public void loadExample2() {
Node southernLive = new Node("Southern live Oak: Quercus virginiana");
Node dwarfLive = new Node("Dwarf live oak: Quercus minima");
Node willow = new Node("Willow oak");
Node shingle = new Node("Shingle oak");
Node blackJack = new Node("Blackjack Oak");
Node northernRed = new Node("Northern red oak");
Node white = new Node("White oak Quercus alba");
Node swampChestnut = new Node("Swamp chestnut oak Quercus prinus");
Node node7 = new Node();
node7.addChild(white, "Leaves with 5-9 deep lobes");
node7.addChild(swampChestnut, "Leaves with 21-27 shallow lobes");
Node node6 = new Node();
node6.addChild(blackJack, "Leaves mostly with 3 lobes");
node6.addChild(northernRed, "Leaves mostly with 7-9 lobes");
Node node5 = new Node();
node5.addChild(node7,
"Lobes or teeth rounded or blunt-pointed, no bristles");
node5.addChild(node6, "Lobes or teeth bristle-tipped");
Node node4 = new Node();
node4.addChild(willow, "Leaf narrow, about 4-6 times as long as broad");
node4.addChild(shingle, "Leaf broad, about 2-3 times as long as broad");
Node node3 = new Node();
node3.addChild(dwarfLive, "Mature plant a small shrub");
node3.addChild(southernLive, "Mature plant a large tree");
Node node2 = new Node();
node2.addChild(node4, "Leaves not evergreen");
node2.addChild(node3, "Leaves evergreen");
Node node1 = new Node();
node1.addChild(node5, "Leaves usually with teeth or lobes");
node1.addChild(node2, "Leaves usually without teeth or lobes");
root = node1;
currentNode = root;
//You need to implement this method
}
@Override
public void readFromFile(FileInputStream fs) {
//You need to implement this method
// You may find that a hashmap like this is useful
HashMap<Integer, Node> map = new HashMap<Integer, Node>();
}
@Override
public boolean canUndo() {
//Dummy implementation. You need to provide a proper one
if (choiceStack != null) {
return true;
} else {
return false;
}
}
}
最佳答案
您有一个植物“数据库”(树形结构),描述了每种植物,例如:“柳橡树”?那么您想了解搜索时给出了什么?例如,我们有对某种植物的描述:“叶子有 5-9 个深裂片”,我们想要获取该植物可能是的所有植物?在这种情况下,我们需要找到一个包含文本“Leaves with 5-9 deep lobes”的节点。我们称这个节点为startingNode。
然后我们必须从startingNode移动到它的父节点。我们将startingNode 的父节点称为parentNode。然后我们必须获取parentNode 的父节点等等。重复此操作,直到到达具有植物名称的节点。
关于java - 二叉树和 botkey troube - 无法遍历树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20217222/