1

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

javaChipFrapp
  • 103
  • 2
  • 8
  • possible duplicate of [Java: how to create and write to a file](http://stackoverflow.com/questions/2885173/java-how-to-create-and-write-to-a-file) – Dawood ibn Kareem Mar 17 '14 at 22:35
  • 1
    @DavidWallace, since the op mentions he has a writeToFile method, I'm assuming he has already implemented file printing so it's probably not a duplicate. – Vlad Schnakovszki Mar 17 '14 at 22:47

2 Answers2

0

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.

Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
0

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");
    }
}
Sn.
  • 87
  • 2
  • 7