Filter a tree using a function