What would the best (most efficient) way to store a binary tree in a file? I've been thinking of one way, but if there is a better one I'd like to know.

Imagine a tree like this:

When storing it in a file it is stored in level order. Since it's not neccessarily balanced you must use some special character for empty branches. Except for the first character, which is the root, the characters are in pairs (siblings to a node). The first pair is the children to the first character, the second pair to the second character, etc... with the exception of a special character that cannot have any childrenCode:A / \ B C / / \ D E F / \ G H

The above tree would turn into:

A B C D ¤ E F ¤ ¤ G H

with pairs: A (B C) (D ¤) (E F) (¤ ¤) (¤ ¤) (G H)

This means that (B C) is the children to A, (D ¤) is the children to B, (E F) is the children to C, (¤ ¤) is the children to D (two special nodes in a pair means no children at all, but it needs to be stored anyway), the next (¤ ¤) would be the children to ¤, but that's invalid so they will be the children to the next character instead, E. Finally (G H) will be the children to F. No more pairs exists so the tree is finsihed. No need to store the last 2 empty pairs (for G and H).

Hope you understands my thinkings. Any thoughts? Is this a pretty ok approach?

EDIT: Fixed a mistake in the algorithm