How to store a tree on the disk and make add/delete/swap operations easy
===

All right, this question requires a bit of reading on your side. I'll try to keep this short and simple.

I have a tree (not a binary tree, just a tree) with data associated to each node (binary data, I don't know what they are AND I don't know how long they are)

Each node of the tree also has an index which isn't related to how it appears in the tree, to make it short it could be like that:

![enter image description here][1]


  [1]: http://i.stack.imgur.com/8jqzd.jpg

The index number represents the order the user WANTS the tree to be navigated and cannot be duplicated.

I need to store this structure in a file on the disk.

My problem is: **how to design a flexible disk storing format that can make loading and working on the tree as easy as possible**.

In fact the user should be allowed to

* Create a child block to an element (and this should be easy enough, it's sufficient to add data to the file paying attention to avoiding duplicated indices)
* Delete a child (I should prompt the user "do you want to delete all this node's children as well? or should I add its children to its parent?"). The tricky part about this is that deleting a node could also free up an index, and I can't let the user use that index again when adding another node (or the order he set could be messed up), I need to update the entire tree!
* Swap an index with another one

I'm using C++ and Qt and by now I thought of a lot of structures with a lot of fields like this one

    struct dataToBeStoredInTheFile
    {
        long data_size;
        byte *data; //... the data here
    
        int index;
        int number_of_children;
        int *children_indices; // ... array of integers
    }

this has the advantage to identify each node with its respective index, but it's highly slow when swapping indices between two nodes or deleting a node and updating each other node's index because you have to traverse all the nodes and all their "children_indices" arrays.

Would using something like an "hash" to identify each node be more flexible?
Should I use two indices, one for the position in the tree and one for the user's index? If you have any better idea to store the data, you're welcome

