Save/load contents of binary search tree

走远了吗. 提交于 2019-12-11 08:08:07

问题


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!


回答1:


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.

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"));

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.


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

Call with:

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

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

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).




回答2:


If i understand correctly, you need to save the tree to file and later load that tree. You can approach like this.

  • To save the tree, generate the inorder traversal along with preorder/postorder and save to file
  • To load, read from the file and reconstruct same tree uniquely using two traversals.(Example)

You can use same traversal function with a flag

public void inOrderTraverseTree (Node focusNode, boolean toSave) {
    if (focusNode != null){
        inOrderTraverseTree(focusNode.leftChild);            
        if(toSave)
           //write to file
        else
        System.out.println(focusNode);

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


来源:https://stackoverflow.com/questions/23015827/save-load-contents-of-binary-search-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!