Merge "Move tree writing code into bitwriter.h." into nextgenv2