Tree encoder
Move tree into its own source file
Working tree encoder, with a test for it