Added function to filter ancestor nodes by a function.