public List <KD_Point> K_Nearest(KD_Point position, int K) { KDTreeNodeCollection collection = new KDTreeNodeCollection(K, position); collection = K_Nearest(this.rootNode, position, collection); return(collection.GetPoints()); }
public KDTreeNodeCollection K_Nearest(KDTreeNode current, KD_Point position, KDTreeNodeCollection collection) { double d = ObtainDistanFromTwoPoint(current.DivisionPoint, position); collection.Add(current, d); double value; double median; if (current.DivisionType == EnumDivisionType.X) { value = position.X; median = current.DivisionPoint.X; } else { value = position.Y; median = current.DivisionPoint.Y; } double u = value - median; if (u <= 0) { if (current.LeftChild != null) { K_Nearest(current.LeftChild, position, collection); } if (current.RightChild != null && Math.Abs(u) <= collection.max) { K_Nearest(current.RightChild, position, collection); } } else { if (current.RightChild != null) { K_Nearest(current.RightChild, position, collection); } if (current.LeftChild != null && Math.Abs(u) <= collection.max) { K_Nearest(current.LeftChild, position, collection); } } return(collection); }