Writing a Tree to a File (C++)

Started by
10 comments, last by Jkom329 15 years, 10 months ago
Do the following:
Convert your binary tree into an array starting at index 1. Given a node at array slot i, its children are at 2*i and 2*i+1. Then simply save the array which is essentially a continuous memory block.

I wouldnt use XML unless you want to manually edit the resulting file because XML has quite some overhead. If you are going for compatibility with other tools then XML may be a good idea though, since you can view it in different xml editors.

-CProgrammer
Advertisement
CProgrammer,

The only thing I don't like about that solution is that it's going to waste a lot of space when many nodes in the tree have one or zero children. That's going to be important for this project, because I plan to cram as many nodes as I possibly can into this tree (although many will still have less than two children).

On the other hand, this method may just be the best for me to use since I plan to compress large sections of the tree into super nodes. The empty space should compress nicely. I wonder how this method with compression compares to an idea I had about breaking the tree into branches to save on pointer space. There's also the depth first method suggested by Zahlman to save pointer space. Hmmm...

This topic is closed to new replies.

Advertisement