refactor tree.py