build pattern tree