Implementing tree handling functions