building encoding tree