0

I have a method that is supposed to print a binary tree to a file. This is it:

public void writeFile(Node mainNode)
{
    FileOutputStream outputStream = null;
    PrintWriter printWriter = null;

    try
    {

        outputStream = new FileOutputStream("BinaryTree.txt");
        printWriter = new PrintWriter(outputStream); 


        while(mainNode != null)
        {
             writeFile(mainNode.leftChild);
             printWriter.print(mainNode);
             writeFile(mainNode.rightChild); 

        }

        printWriter.close();

  }catch(IOException e)
  {
     System.out.println("An error occured");
      printWriter.close();
  }

}

The problem is that it seems to eternally loop as it's not finding the end of the tree. Is there anything I can try.

Here's the Node class too.

class Node
{
int id;
int grade;
String name;

Node leftChild;
Node rightChild;


Node(int id, int grade, String name )
{
    this.id = id;
    this.grade = grade;
    this.name = name;
}


public String toString()
{
    return name + " has a grade of " + grade + " and their ID is " + id;
}
}
0

4 Answers 4

1

How do you expect this loop to end:

while(mainNode != null) {
    // never change mainNode
}

You need to pass your PrintWriter as an argument to your function, in order for all recursive calls to write (append) to the same file. Then provide a base case to stop:

public void writeFile(Node mainNode, PrintWriter w)
{
    if (mainNode == null)  // base case to stop recursion  
        return;
    top_call = false;  // Flag needed later
    if (w == null) {
        outputStream = new FileOutputStream("BinaryTree.txt");
        w = new PrintWriter(outputStream); 
        top_call = true;  // mark highest entry point to know when to close writer
    }
    writeFile(mainNode.leftChild, w);
    w.print(mainNode);
    writeFile(mainNode.rightChild, w);

    if (top_call)  // don't close writer in recursive calls
        w.close();
}
Sign up to request clarification or add additional context in comments.

8 Comments

Exactly. You might want to elaborate a bit more, though. I didn't see that at first! Good thinking.
Well, when the mainNode returns null then it should break out. Shouldn't it?
@Allan Well, the mainNode is not null when you enter the loop and it then never changes. The recursive calls inside are just repeated over and over even though somewhere in these recursive calls, there might be a null mainNode
How do I know when the binary tree ends then?
@Allan, since you're using recursion, by principle it will finish, i.e. start going back, as soon as mainNode is null. This behavior is applicable on both sides of the tree.
|
1

The entire writeFile method is wrong.

You have a loop over a single value without any go-to-next, so it'll never end.

It also calls itself recursively, trying to open the file again inside the recursive call. That's going to fail.

You have to split the method in two:

  • First method open the file, calls second method, then closes file (using try-with-resources, please!).
  • Second method does the three lines of call-self(left), write node, call-self(right).

Comments

0

Here's the solution that worked for anyone who'd like to know.

public void writeFile(Node mainNode)
{
    FileOutputStream outputStream = null;
    PrintWriter printWriter = null;

    try
    {

        outputStream = new FileOutputStream("BinaryTree.txt");
        printWriter = new PrintWriter(outputStream); 

        write(mainNode, printWriter);

        printWriter.flush();

  }catch(IOException e)
  {
     System.out.println("An error occured");
      printWriter.close();
  }

}

 public void write(Node mainNode, PrintWriter w)
 {
     if(mainNode != null){
      write(mainNode.leftChild, w);
      w.print(mainNode);
      write(mainNode.rightChild, w); 
    }
 }

Comments

0

I use an array to store nodes's data (Left - Node - Right) before this i write the data of the array to the file i want.

void LNR(TREE t, int a[], int &i){
   if(t != NULL)
   {
       LNR(t ->pLeft, a, i);
       a[i] = t ->data;
       i++;
       LNR(t ->pRight, a, i);
   }
}

void Output(const char *outfile, TREE t){
    int a[100];
    int i = 0;
    LNR(t, a, i);
    ofstream out;
    out.open(outfile);
    if (!out.is_open())
    {
       cout << "Unble to open the file.";
    }
    else
    {
       for (int j = 0; j < i; j++)
       {
           if (j < i)
               out << a[j] << " ";
           else
               out << a[j];
       }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.