I’m trying to save a binary tree to a file upon ending a program and to rebuild it when I again run the program. My save method looks something like this:
public static void save(TreeNode node, BufferedWriter out) {
if (node == null) return;
out.write(node.value()); // these nodes hold Strings
out.newLine();
save(node.left(), out);
save(node.right(), out);
}
The part I’m having trouble with is the rebuilding process, so help on that would be much appreciated.
EDIT: It will be known that every node has either 2 or 0 children.
If you want to save the tree in exactly the same branching structure, you’d need to represent null.