예제 #1
0
        public void DrawTree(Graphics g, Pen pen, SolidBrush brush, KDTreeNode <E> node, int xPos, int yPos, int xPad, int depth)
        {
            double maxNumPerDepth = Math.Pow(2, depth);

            if (node == null)
            {
                return;
            }
            if (node.Left != null && node.Left.Value != null)
            {
                DrawTree(g, pen, brush, node.Left, (xPos - xPad), yPos + 100, xPad - 15, depth + 1);
                if (node.Value != null)
                {
                    g.DrawLine(pen, new Point(xPos + 5, yPos + 5), new Point((xPos - xPad) + 5, (yPos + 100) + 5));
                }
            }
            if (node.Value != null)
            {
                g.DrawString(node.ToString(), new Font(FontFamily.GenericSansSerif, 7), brush, xPos, yPos - 20);
                g.FillEllipse(brush, xPos, yPos, 10, 10);
            }
            if (node.Right != null && node.Right.Value != null)
            {
                DrawTree(g, pen, brush, node.Right, (xPos + xPad), yPos + 100, xPad - 15, depth + 1);
                g.DrawLine(pen, new Point(xPos + 5, yPos + 5), new Point((xPos + xPad) + 5, (yPos + 100) + 5));
            }
        }
예제 #2
0
        public KDTreeNode <E> FindMinimum(KDTreeNode <E> t, int axisToSearch, int cuttingDimension)
        {
            int cd = (cuttingDimension + 1) % Dimension;

            if (axisToSearch == cuttingDimension)
            {
                if (t.Left == null)
                {
                    return(t);
                }
                else
                {
                    return(FindMinimum(t.Left, axisToSearch, cd));
                }
            }
            else
            {
                if (t.Left != null && t.Right != null)
                {
                    return(FindMinimum(t.Right, axisToSearch, cd).Value[axisToSearch].CompareTo(FindMinimum(t.Left, axisToSearch, cd).Value[axisToSearch]) > 0 ? FindMinimum(t.Left, axisToSearch, cd) : FindMinimum(t.Right, axisToSearch, cd));
                }
                else if (t.Left != null && t.Right == null)
                {
                    return(FindMinimum(t, axisToSearch, cd).Value[axisToSearch].CompareTo(FindMinimum(t.Left, axisToSearch, cd).Value[axisToSearch]) > 0 ? FindMinimum(t, axisToSearch, cd) : FindMinimum(t.Left, axisToSearch, cd));
                }
                else if (t.Left == null && t.Right != null)
                {
                    return(FindMinimum(t, axisToSearch, cd).Value[axisToSearch].CompareTo(FindMinimum(t.Right, axisToSearch, cd).Value[axisToSearch]) < 0 ? FindMinimum(t, axisToSearch, cd) : FindMinimum(t.Right, axisToSearch, cd));
                }
                else
                {
                    return(t);
                }
            }
        }
예제 #3
0
        public KDTreeNode(T[] data, KDTreeNode <T> left = null, KDTreeNode <T> right = null)
        {
            Value     = data;
            Dimension = Value.Length;

            Children = new NodeList <T>(2);

            if (left != null)
            {
                Children[0] = left;
            }
            if (right != null)
            {
                Children[1] = right;
            }
        }
예제 #4
0
        public KDTreeNode <E> DeleteNode(KDTreeNode <E> t, E[] point, int cuttingDimension)
        {
            if (t == null)
            {
                return(null);
            }

            int cd = (cuttingDimension + 1) % Dimension;

            if (ArrayEquals(point, t.Value))
            {
                List <E[]> nodes = GetChildren(t);
                nodes.RemoveAt(0);
                if (t.Parent != null)
                {
                    if (t == t.Parent.Left)
                    {
                        t.Parent.Left = null;
                    }
                    else if (t == t.Parent.Right)
                    {
                        t.Parent.Right = null;
                    }
                }
                foreach (var x in nodes)
                {
                    AddNode(x);
                }
                t = null;
            }
            else if (point[cuttingDimension].CompareTo(t.Value[cuttingDimension]) < 0)
            {
                t.Left = DeleteNode(t.Left, t.Value, cd);
            }
            else if (point[cuttingDimension].CompareTo(t.Value[cuttingDimension]) > 0)
            {
                t.Right = DeleteNode(t.Right, t.Value, cd);
            }
            return(t);
        }
예제 #5
0
        public int GetDepth(E[] dataToFind)
        {
            int depth = -1;

            KDTreeNode <E> Current = Root;

            if (dataToFind.Length != Dimension)
            {
                return(-1);
            }

            while (Current != null)
            {
                if (depth == Dimension - 1)
                {
                    depth = 0;
                }
                else
                {
                    ++depth;
                }

                if (ArrayEquals(Current.Value, dataToFind))
                {
                    return(depth);
                }
                else if (Current.Value[depth].CompareTo(dataToFind[depth]) > 0)
                {
                    Current = Current.Left;
                }
                else if (Current.Value[depth].CompareTo(dataToFind[depth]) < 0)
                {
                    Current = Current.Right;
                }
            }

            return(-1);
        }
예제 #6
0
 public void PostOrderTraversal(KDTreeNode <E> node)
 {
     if (node == null)
     {
         return;
     }
     if (node.Left != null)
     {
         PostOrderTraversal(node.Left);
     }
     if (node.Right != null)
     {
         PostOrderTraversal(node.Right);
     }
     if (node.Value != null)
     {
         Console.Write("\n" + string.Format("Value : {0:00},  Parent : {1}, Left Child : {2}, Right Child : {3}",
                                            node.ToString(),
                                            node.Parent != null ? node.Parent.ToString() : "No Parent",
                                            node.Left != null ? node.Left.ToString() : "No Left Child",
                                            node.Right != null ? node.Right.ToString() : "No Right Child")
                       );
     }
 }
예제 #7
0
 public List <E[]> GetChildren(KDTreeNode <E> node, List <E[]> list = null)
 {
     if (list == null)
     {
         list = new List <E[]>();
     }
     if (node == null)
     {
         return(list);
     }
     if (node.Value != null)
     {
         list.Add(node.Value);
     }
     if (node.Left != null)
     {
         GetChildren(node.Left, list);
     }
     if (node.Right != null)
     {
         GetChildren(node.Right, list);
     }
     return(list);
 }
예제 #8
0
        public bool AddNode(E[] dataToAdd)
        {
            KDTreeNode <E> tempNode = new KDTreeNode <E>(dataToAdd);
            KDTreeNode <E> Current = Root, parent = null;
            int            depth = -1;

            if (dataToAdd.Length != Dimension)
            {
                return(false);
            }

            while (Current != null)
            {
                if (depth == Dimension - 1)
                {
                    depth = 0;
                }
                else
                {
                    ++depth;
                }

                if (Current.Value[depth].CompareTo(dataToAdd[depth]) == 0)
                {
                    return(false);
                }
                else if (Current.Value[depth].CompareTo(dataToAdd[depth]) > 0)
                {
                    parent  = Current;
                    Current = Current.Left;
                }
                else if (Current.Value[depth].CompareTo(dataToAdd[depth]) < 0)
                {
                    parent  = Current;
                    Current = Current.Right;
                }
            }

            if (parent == null)
            {
                Root        = tempNode;
                Root.Parent = null;
            }
            else
            {
                if (parent.Value[depth].CompareTo(dataToAdd[depth]) > 0)
                {
                    parent.Left        = tempNode;
                    parent.Left.Parent = parent;
                }
                else if (parent.Value[depth].CompareTo(dataToAdd[depth]) < 0)
                {
                    parent.Right        = tempNode;
                    parent.Right.Parent = parent;
                }
                depth = 0;
                return(true);
            }

            return(false);
        }
예제 #9
0
 private void Nullify(KDTreeNode <E> node)
 {
     node = null;
 }