             
             //check the best estimate side
             let closerSubtree = isLeftOfValue ? left : right
-            var (bestNewElement, bestNewDistance) = closerSubtree.nearest(to: searchElement, bestValue: bestValue, bestDistance: bestDistance)
+            var (bestNewElement, bestNewDistance) = closerSubtree.nearest(to: searchElement, bestValue: bestValue, bestDistance: bestDistance, condition: condition)
             
             // if the bestDistance so far intersects the hyperplane at the other side of this value
             // there could be points in the other subtree
             if dimensionDifference*dimensionDifference < bestNewDistance {
                 //check the nodes value
-                if searchElement != value {
+                if searchElement != value && condition(value) {
                     let currentDistance = value.squaredDistance(to: searchElement)
                     if currentDistance < bestNewDistance { (bestNewElement, bestNewDistance) = (value, currentDistance) }
                 }
                 
                 let otherSubtree = isLeftOfValue ? right : left
-                (bestNewElement, bestNewDistance) = otherSubtree.nearest(to: searchElement, bestValue: bestNewElement, bestDistance: bestNewDistance)
+                (bestNewElement, bestNewDistance) = otherSubtree.nearest(to: searchElement, bestValue: bestNewElement, bestDistance: bestNewDistance, condition: condition)
             }
             
             return (bestNewElement, bestNewDistance)

    /// Returns the k nearest `KDTreePoint`s to the search point,
    ///
    /// - Complexity: O(log N).
    public func nearestK(_ number: Int, to searchElement: Element) -> [Element] {
        var neighbours: Neighbours = Neighbours(goalNumber: number)
        self.nearestK(to: searchElement, bestValues: &neighbours)
        return neighbours.nearestValues.map { $0.point as! Element }
    }
    
    fileprivate func nearestK(to searchElement: Element, bestValues: inout Neighbours) {
        switch self {
        case let .node(left, value, dim, right):
            let dimensionDifference = value.kdDimension(dim) - searchElement.kdDimension(dim)

            
            //check the best estimate side
            let closerSubtree = isLeftOfValue ? left : right
            closerSubtree.nearestK(to: searchElement, bestValues: &bestValues, where: condition)

            if condition(value) {
                //check the nodes value
                let currentDistance = value.squaredDistance(to: searchElement)
                bestValues.append(value, distance: currentDistance)
            }

            //if the bestDistance so far intersects the hyperplane at the other side of this value
            //there could be points in the other subtree
            if dimensionDifference*dimensionDifference < bestValues.biggestDistance || !bestValues.full {
                let otherSubtree = isLeftOfValue ? right : left
                otherSubtree.nearestK(to: searchElement, bestValues: &bestValues, where: condition)
            }
        case .leaf: break
        }
