handled empty list case for tree building