Implements Tree sort.