Implement functions for tree traversal and reduction.