Write to the file. (Binary search tree)

I cannot figure out how to write a binary search tree for a recursive file. I open BufferWriter with a file to wrtie too, in the Tree class. Then I send BufferWriter to the Node class to traverse the inorder tree and write to the file. But that does not work.

public void write(String filePath)
{
  if(root != null) {
    try {
      BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
      root.write(out);
    } catch (IOException e) {
    }
  }
}

public void write(BufferedWriter out)
{
    if (this.getLeft() != null) this.getLeft().write(out);
    out.write(this.data());
    if (this.getRight() != null) this.getRight().write(out);
}
+3
source share
1 answer

It doesn't look so bad! Maybe you just skip close()yours BufferedWriterwhen you're done? The file will probably not be written correctly if not close.

+4
source

Source: https://habr.com/ru/post/1742339/


All Articles