写入文件。 (二叉搜索树)
我不知道如何编写二叉搜索树以递归方式归档。我在 Tree 类中打开一个 BufferWriter,其中包含要写入的文件。然后,我将 BufferWriter 发送到 Node 类以按顺序遍历树并写入文件。但这不起作用。
public void write(String filePath)
{
if(root != null) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
root.write(out);
} catch (IOException e) {
}
}
}
public void write(BufferedWriter out)
{
if (this.getLeft() != null) this.getLeft().write(out);
out.write(this.data());
if (this.getRight() != null) this.getRight().write(out);
}
I can't figure out how to write a Binary Search Tree to file recursively. I open a BufferWriter with the file to wrtie too, in the Tree class. I then send the BufferWriter to the Node class to traverse the tree inorder and write to file. But it doesn't work.
public void write(String filePath)
{
if(root != null) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
root.write(out);
} catch (IOException e) {
}
}
}
public void write(BufferedWriter out)
{
if (this.getLeft() != null) this.getLeft().write(out);
out.write(this.data());
if (this.getRight() != null) this.getRight().write(out);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来没那么糟糕!可能是您在完成后缺少
BufferedWriter
上的close()
吗?如果没有close
,文件可能无法正确写入。That doesn't look so bad! Could it be you're just missing the
close()
on yourBufferedWriter
when you're done? The file will likely not be written correctly if there's noclose
.