Add tree sorting functionality