Example #1
0
        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);
            }
        }
Example #2
0
 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));
     }
 }
Example #3
0
 private Point DFSRightSearch(KDTreeNode node, Point searchPoint)
 {
     if (node.RightChild != null)
     {
         return(DFSSearch(node.RightChild, searchPoint));
     }
     else
     {
         return(node.DivisionPoint);
     }
 }
Example #4
0
 private Point DFSYsearch(KDTreeNode node, Point searchPoint)
 {
     if (node.DivisionPoint.Y > searchPoint.Y)
     {
         return(DFSLeftSearch(node, searchPoint));
     }
     else
     {
         return(DFSRightSearch(node, searchPoint));
     }
 }
Example #5
0
 private Point DFSBackTrackLeftSearch(KDTreeNode node, Point searchPoint)
 {
     if (node.LeftChild != null)
     {
         return(DFSSearch(node.LeftChild, searchPoint, false));
     }
     else
     {
         return(node.DivisionPoint);
     }
 }
Example #6
0
        private Point DFSBackTrackingSearch(KDTreeNode node, Point searchPoint)
        {
            backtrackStack.Push(node);

            if (node.DivisionType == EnumDivisionType.X)
            {
                return(DFSBackTrackingXsearch(node, searchPoint));
            }
            else
            {
                return(DFSBackTrackingYsearch(node, searchPoint));
            }
        }
Example #7
0
 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));
     }
 }
Example #8
0
 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);
     }
 }
Example #9
0
 public void CreateByPointList(List <Point> pointList)
 {
     rootNode = CreateTreeNode(pointList);
 }