简体   繁体   中英

Outputting Traversal of a BST to a File

I've made a program that does things with a binary serach tree, and I have a method that prints the post-order traversal of it.

public static void postOrderTrav(TreeNode node){
    if(node != null){
        postOrderTrav(node.getLeft());
        postOrderTrav(node.getRight());
        System.out.print(node.getVal() + " ");
    }
}

However, I also want to print the post-order traversal to a file as well. How do I do this, in this method that uses recursion? I've tried putting the call to the writeToFile method in various places, but they always write out only one number...

If in the writeToFile method you mention you are always opening, writing to and closing the same file, you are basically telling it to overwrite everything each time you print, resulting in the file containing only one value when the program terminates.

You can overcome this by opening the file in append mode if you insist on using your function that way, by opening the file like this (notice the true argument):

FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);

However, opening and closing the file for each write is really inefficient, so a better way to do this is as follows:

Assuming you are calling your method from main, you could do something like this if you print one element at a time:

private static BufferedWriter bw;

public static void main(String[] args) {

    File file = new File("/your/file/path/<filename>");

    // if file doesnt exists, then create it
    if (!file.exists()) {
        file.createNewFile();
    }

    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);

    postOrderTrav(node);

    bw.close();
}

and in your method you would have:

public static void postOrderTrav(TreeNode node){
    if(node != null){
        postOrderTrav(node.getLeft());
        postOrderTrav(node.getRight());
        System.out.print(node.getVal() + " ");
        bw.write(node.getVal() + " ");
    }
}

Have a look here about information for printing to a file. You can either print directly to the file when printing to output or you can store the result in a data structure (or String) and print it at the end.

It is better to use Apache common for such easier tasks to minimize no of line, and Just pass StrinBuilder class.

import org.apache.commons.io.FileUtils

public static printPostOrderTravInFiles(TreeNode node, String fileName) throws IOException{
  StringBuffer sb = new StringBuffer();
  postOrderTrav(node, sb);
  FileUtils.writeStringToFile(sb.toString());
}


public static void postOrderTrav(TreeNode node, StringBuffer sb){
    if(node != null){
        postOrderTrav(node.getLeft());
        postOrderTrav(node.getRight());
        sb.append(node.getVal() + " \n");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM