private KDTreeNode CreateTreeNode(List <Point> pointList) { if (pointList.Count > 0) { // 计算方差 double xObtainVariance = ObtainVariance(CreateXList(pointList)); double yObtainVariance = ObtainVariance(CreateYList(pointList)); // 根据方差确定分裂维度 EnumDivisionType divisionType = SortListByXOrYVariances(xObtainVariance, yObtainVariance, ref pointList); // 获得中位数 Point medianPoint = ObtainMedian(pointList); int medianIndex = pointList.Count / 2; // 构建节点 KDTreeNode treeNode = new KDTreeNode() { DivisionPoint = medianPoint, DivisionType = divisionType, LeftChild = CreateTreeNode(pointList.Take(medianIndex).ToList()), RightChild = CreateTreeNode(pointList.Skip(medianIndex + 1).ToList()) }; return(treeNode); } else { return(null); } }
private Point BacktrcakSearch(Point searchPoint, Point nearestPoint) { if (backtrackStack.IsEmpty()) { return(nearestPoint); } else { KDTreeNode trackNode = backtrackStack.Pop(); double backtrackDistance = ObtainDistanFromTwoPoint(searchPoint, trackNode.DivisionPoint); double nearestPointDistance = ObtainDistanFromTwoPoint(searchPoint, nearestPoint); if (backtrackDistance < nearestPointDistance) { KDTreeNode searchNode = new KDTreeNode() { DivisionPoint = trackNode.DivisionPoint, DivisionType = trackNode.DivisionType, LeftChild = trackNode.LeftChild, RightChild = trackNode.RightChild }; nearestPoint = DFSBackTrackingSearch(searchNode, searchPoint); } return(BacktrcakSearch(searchPoint, nearestPoint)); } }
private Point DFSRightSearch(KDTreeNode node, Point searchPoint) { if (node.RightChild != null) { return(DFSSearch(node.RightChild, searchPoint)); } else { return(node.DivisionPoint); } }
private Point DFSYsearch(KDTreeNode node, Point searchPoint) { if (node.DivisionPoint.Y > searchPoint.Y) { return(DFSLeftSearch(node, searchPoint)); } else { return(DFSRightSearch(node, searchPoint)); } }
private Point DFSBackTrackLeftSearch(KDTreeNode node, Point searchPoint) { if (node.LeftChild != null) { return(DFSSearch(node.LeftChild, searchPoint, false)); } else { return(node.DivisionPoint); } }
private Point DFSBackTrackingSearch(KDTreeNode node, Point searchPoint) { backtrackStack.Push(node); if (node.DivisionType == EnumDivisionType.X) { return(DFSBackTrackingXsearch(node, searchPoint)); } else { return(DFSBackTrackingYsearch(node, searchPoint)); } }
private Point DFSSearch(KDTreeNode node, Point searchPoint, bool pushStack = true) { if (pushStack == true) { backtrackStack.Push(node); } if (node.DivisionType == EnumDivisionType.X) { return(DFSXsearch(node, searchPoint)); } else { return(DFSYsearch(node, searchPoint)); } }
private Point DFSBackTrackingYsearch(KDTreeNode node, Point searchPoint) { if (node.DivisionPoint.Y > searchPoint.Y) { node.LeftChild = null; Point rightSearchPoint = DFSBackTrackRightSearch(node, searchPoint); node.RightChild = null; return(rightSearchPoint); } else { node.RightChild = null; Point leftSearchPoint = DFSBackTrackLeftSearch(node, searchPoint); node.LeftChild = null; return(leftSearchPoint); } }
public void CreateByPointList(List <Point> pointList) { rootNode = CreateTreeNode(pointList); }