保存/加载二进制搜索树的内容 [英] Save/load contents of binary search tree

查看:37
本文介绍了保存/加载二进制搜索树的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我到处寻找了很多有关如何从二进制搜索树中获取元素并将其写入文件,然后再将它们作为加载"函数类型读回到树中的信息.我已经找到了一些解决方法,但是在将这些建议(例如此处看到的建议)落实到我的程序本身时,我遇到了问题.因此,我有几种排序方法,inOrder,postOrder和preOrder,但不必每次调用其中一种方法时都写入文件.我想有一个saveFile和loadFile方法,它们在被调用时会自动进行写入/读取,但是我是否必须在那些sort方法中调用writer/reader来获取BST中的每个节点?还是我可以有一些可以在外面做的方法?它们不一定需要分类到文件中,因为程序将在其内部完成该操作.有什么建议吗?

So I've looked around a lot for some information on how to take elements from a binary search tree and write them to a file, and then later read them back into the tree as a type of "load" function. I've found a few ways that people go about it, but I am having issues implementing those suggestions such as the one seen here, into my program itself. So I have several sort methods, inOrder, postOrder, and preOrder, but don't necessarily need to write to the file every time one of those methods is called. I would like to have a saveFile and a loadFile method that would automatically write/read when called, but do I have to call the writer/reader in those sort methods to get every node in the BST? Or can I have methods that will do it outside? They don't necessarily need to be sorted into the file, because the program will do that within itself. What are some suggestions about going about this?

这里是我的一个排序类的示例(从单独的菜单类中调用,为用户提供了选择排序方式的选项:

Here is an example of one of my sort classes (which is called from a seperate menu class giving the user the option to choose how it should be sorted:

public void inOrderTraverseTree (Node focusNode) {
    if (focusNode != null){
        inOrderTraverseTree(focusNode.leftChild);

        System.out.println(focusNode);

        inOrderTraverseTree(focusNode.rightChild);
    }//end if
}//end inOrderTraverseTree

以及调用时间的示例(从菜单类,在开关中):

And an example of when it's called (from the menu class, in a switch):

case 1:
    tree.inOrderTraverseTree(tree.root);
    menu();
    break;

请让我知道建议!谢谢!

Please let me know suggestions! thank you!

推荐答案

System.out.println写入标准输出,而不是文件.可以通过代码(您不应该这样做)或通过命令行将输出重定向到文件,使用管道非常简单(我认为在Linux中是|,在Linux中是> Windows),但确实有一些缺点.

System.out.println writes to standard output, not to file. It is possible to redirect that output to a file, either through code (which you shouldn't be doing) or through the command-line, which is pretty simple using a pipe (I think | in Linux, > in Windows), but does have some disadvantages.

另一种选择是将OutputStream传递给函数,如果您喜欢println方法,则可能传递PrintStream.

Another option would be to pass an OutputStream to your function, perhaps a PrintStream if you like your println method.

public void inOrderTraverseTree (Node focusNode, PrintStream printStream) {
    if (focusNode != null){
        inOrderTraverseTree(focusNode.leftChild, printStream);
        printStream.println(focusNode);
        inOrderTraverseTree(focusNode.rightChild, printStream);
    }//end if
}//end inOrderTraverseTree

要将其打印到文件中,可以使用:

To print it to a file, you could use:

inOrderTraverseTree(root, new PrintStream("fileName"));

请注意,new调用将在启动前清除文件,因此您可能希望将PrintStream存储在一个临时变量中,将同一对象传递给应写入该文件的所有函数调用,或者您可以尝试使用OutputStream代替的构造函数.

Note that the new call will clear out the file before starting, so you'd probably want to store the PrintStream in a temporary variable, passing the same object to all function calls which should write to it, or you could try the constructor that uses an OutputStream instead.

另一种选择是将所有节点存储在LinkedList或类似的结构中,然后将其传递给函数(类似于上述printStream的方式),然后您可以简单地对其进行遍历并编写要归档的节点.

Another option would be to store all the nodes in a LinkedList or similar structure, which you pass to the function (similarly to the way printStream was above), and then you can simply iterate through that afterwards and write the nodes to file.

public void inOrderTraverseTree (Node focusNode, LinkedList<Node> output) {
    if (focusNode != null){
        inOrderTraverseTree(focusNode.leftChild, output);
        output.add(focusNode);
        inOrderTraverseTree(focusNode.rightChild, output);
    }//end if
}//end inOrderTraverseTree

致电:

LinkedList<Node> output = new LinkedList<Node>();
inOrderTraverseTree(root, output);
// iterate through 'output' and write it to file

您可以返回LinkedList而不是通过对代码进行一些更改来传递它.

You could return the LinkedList instead of passing it around with a few changes to the code.

这有点复杂,但是确实可以为您的代码提供更多的模块化-如果遍历LinkedList的函数与文件I/O无关,则可能会更好.您正在编写的代码可能不大(就代码大小和生命周期而言),您不必为此担心(尽管您最终也倾向于在较大项目中完成在较小项目中所做的工作,并且较小的项目可能很快就会变得很大.)

This is a bit more complex, but does give your code more modularity - it's probably better if a function to traverse a LinkedList doesn't concern itself with file I/O, but, on the other hand, the code you're writing probably isn't of the scale (in terms of code size and lifetime) that you need to worry about that (although you tend to end up doing what you do in your smaller projects in your larger projects as well, and smaller projects can become large quickly).

这篇关于保存/加载二进制搜索树的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆