Not sure if it's elegant, but simple and straightforward: Assign a unique identifier to each node, whether it's a stem or leaf. Simple integer counting.
When saving to disk, move the tree, saving each link identifier node, yes, link identifier no, and the text of the question or answer. For null references, use null as the null value. You can either add a flag to indicate whether there is a question or answer, or, more simply put, to check whether both links are null. You should get something like this:
1,2,3,"Does it have wings?" 2,0,0,"a bird" 3,4,0,"Does it purr?" 4,0,0,"a cat"
Note that if you use the sequential integer approach, storing the node identifier may be redundant, as shown here. You can simply put them in order by ID.
To recover from disk, read the line and then add it to the tree. You will probably need a table or array to store nodes with a direct link, for example. when processing node 1, you will need to track 2 and 3 until you can fill in these values.
source share