rewrite purgeTree function to avoid deleting a NULL pointer