Reputation: 33
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;
}
}
Upvotes: 0
Views: 4023
Reputation: 11
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];
}
}
}
Upvotes: 0
Reputation: 33
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);
}
}
Upvotes: 0
Reputation: 73460
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();
}
Upvotes: 1
Reputation: 159096
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:
Upvotes: 1