Implemented ContextTreeBuilder without recursion