add buildEncodingTree and freeTree methods