Add flatten and rebuild functions and tests

Flatten allows for flattening trees, while rebuild rebuilds them. Both
add empty 'children' arrays.