【发布时间】:2015-06-11 01:29:55
【问题描述】:
我正在从文件中读取字符串,并在节点的子节点为空时记录了“~”。然后我将这些字符串添加到二叉树中。但是我的代码只是将所有字符串(包括“~”)添加到左树节点中。
如何让算法在到达“~”时停止添加左节点并插入右节点(当然,除非下一个字符串也是“~”)?
这是我的代码:
// Reads the elements in the tree in pre-order
public void fromFile()
{
BufferedReader in;
String s = null;
try {
in = new BufferedReader(new FileReader("animal_game.txt"));
StringBuffer stringBuffer = new StringBuffer();
while( (s=in.readLine()) != null )
{
stringBuffer.append(s);
stringBuffer.append("\n");
}
fromFile(stringBuffer.toString());
in.close();
}
catch (IOException ex)
{
Logger.getLogger(Tree.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void fromFile(String s)
{
if (root == null)
{
root = new Node<>((T)s);
size++;
}
else
{
fromFile(root, s);
}
}
// helper function
private void fromFile( Node<T> node, String s)
{
// if null tree node reached,
if(s==NULL_TREE_NODE)
{
fromFile(node, s);
}
// insert left node
if (node.no == null)
{
node.no = new Node<>((T)s);
}
else
{
fromFile(node.no, s);
}
// insert right node
if (node.yes == null)
{
node.yes = new Node<>((T)s);
}
else{
fromFile(node.yes, s);
}
}
这是我将树保存到文件的代码:
// Writes the elements in the tree in pre-order
public void toFile()
{
// Writes in preorder starting with the root node
if (root != null)
{
BufferedWriter out;
try {
out = new BufferedWriter(new FileWriter("animal_game.txt"));
toFile(out, root);
out.close();
}
catch (IOException ex)
{
Logger.getLogger(Tree.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// Helper function
private void toFile(BufferedWriter out, Node<T> node)
{
try {
if (node == null) {
out.write(NULL_TREE_NODE); // null
out.newLine();
return;
}
//assert !node.data.equals(NULL_TREE_NODE); // Reserver for us..
out.write((String)node.data); // these nodes hold Strings
out.newLine();
toFile(out, node.no);
toFile(out, node.yes);
}
catch (IOException ex)
{
Logger.getLogger(Tree.class.getName()).log(Level.SEVERE, null, ex);
}
}
这是我的文件
它是哺乳动物吗?
它是爬行动物吗?
是鱼吗?
鹈鹕
~
~
鲨鱼
~
~
它灭绝了吗?
乌龟
~
~
迅猛龙
~
~
它有毛吗?
大象
~
~
猫
【问题讨论】:
-
你能给我们整个文件吗?这个过程的总体目标是什么?目前,似乎 fromFile(Node, String) 将始终终止而不实际向节点添加任何信息,除非 s== NULL_TREE_NODE 在这种情况下它只会调用自身导致堆栈溢出?另外,您是否打算在那里使用 == 来表示字符串相等?
-
这是我的 Tree 类中的一个函数。我主要用 treename.fromFile(); 调用它
-
但这会创建一个包含所有左节点的树,包括“~”字符
-
在 s==NULL_TREE_NODE 的情况下我将如何调用它的父节点?
-
我的意思是你能告诉我们整个 Tree 类。我认为它正在生成所有左节点,因为它首先检查 node.no (左)是否为空。我仍然看不到字符串数据是如何进入树的。首先将所有对象放在一个字符串中是个好主意吗?用换行符分隔 List
是否更有意义? (您已经拥有读者提供的信息)。当到达一个空树节点时,您不应该“调用父节点”,而是返回,因为如果结构正确,您最初是从父节点调用的。