打印二叉树到文件
printing binary tree to file
我有一个方法可以将二叉树打印到文件中。就是这样:
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();
}
}
问题是它似乎永远循环,因为它找不到树的尽头。有什么我可以尝试的。
这里也是节点 class。
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;
}
}
你希望这个循环如何结束:
while(mainNode != null) {
// never change mainNode
}
您需要将 PrintWriter
作为参数传递给您的函数,以便所有递归调用写入(追加)到同一个文件。然后提供一个停止的基本情况:
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();
}
整个writeFile
方法都是错误的。
您对单个值进行了循环,没有任何下一步,因此它永远不会结束。
它还递归调用自身,尝试在递归调用中再次打开文件。那会失败的。
您必须将方法一分为二:
- 第一种方法打开文件,调用第二种方法,然后关闭文件(请使用 try-with-resources!)。
- 第二种方法就是call-self(左),write node,call-self(右)三行。
这是对任何想知道的人都有效的解决方案。
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);
}
}
我使用数组来存储节点的数据(左 - 节点 - 右)
在此之前,我将数组的数据写入我想要的文件。
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];
}
}
}
我有一个方法可以将二叉树打印到文件中。就是这样:
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();
}
}
问题是它似乎永远循环,因为它找不到树的尽头。有什么我可以尝试的。
这里也是节点 class。
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;
}
}
你希望这个循环如何结束:
while(mainNode != null) {
// never change mainNode
}
您需要将 PrintWriter
作为参数传递给您的函数,以便所有递归调用写入(追加)到同一个文件。然后提供一个停止的基本情况:
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();
}
整个writeFile
方法都是错误的。
您对单个值进行了循环,没有任何下一步,因此它永远不会结束。
它还递归调用自身,尝试在递归调用中再次打开文件。那会失败的。
您必须将方法一分为二:
- 第一种方法打开文件,调用第二种方法,然后关闭文件(请使用 try-with-resources!)。
- 第二种方法就是call-self(左),write node,call-self(右)三行。
这是对任何想知道的人都有效的解决方案。
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);
}
}
我使用数组来存储节点的数据(左 - 节点 - 右) 在此之前,我将数组的数据写入我想要的文件。
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];
}
}
}