Классическая рекурсивная прогулка по дереву может помочь. Т.е. что-то вроде:
void wavelet_tree_to_stream(std::ostream& to, const wavelet_tree* node) {
to << "{"
to << "\"alphabet\":\"" << node->alphabet << "\"";
to << ',';
to << "\"middle\": \"" << node->middle << "\"";
if(nullptr != node->Left) {
to << ",\"Left\" : ";
wavelet_tree_to_stream(to, node->Left);
}
if(nullptr != node->Right) {
to << ",\"Right\" : ";
wavelet_tree_to_stream(to, node->Right);
}
to << "}";
}
......
wavelet_tree_to_stream(fstream, root);