Refactor tree traversal to TreeTraversal trait