Beispiel #1
0
        public static TwoDNode insert(Point key, TwoDNode prev, TwoDNode tree, int level)
        {
            if (tree == null)
            {
                tree = new TwoDNode(key, prev);
            }
            else if (key == tree.key)
            {
                // Re-Insert
                if (tree.deleted)
                {
                    tree.deleted = false;
                    tree.prev    = prev;
                }
                // Else duplicate key
            }
            else if ((level == 0 && key.X > tree.key.X) || (level == 1 && key.Y > tree.key.Y))
            {
                tree.right = insert(key, prev, tree.right, (level + 1) % 2);
            }
            else
            {
                tree.left = insert(key, prev, tree.left, (level + 1) % 2);
            }

            return(tree);
        }
Beispiel #2
0
        public List <TwoDNode> Range(Point lowKey, Point upperKey)
        {
            List <TwoDNode> v = new List <TwoDNode>();

            TwoDNode.rsearch(new Point(lowKey), new Point(upperKey), root, 0, v);
            return(v);
        }
Beispiel #3
0
        public static TwoDNode insertWithError(Point key, TwoDNode prev, double allowedDistance, TwoDNode tree, int level)
        {
            if (tree == null)
            {
                tree = new TwoDNode(key, prev);
            }
            else if (key.Distance(tree.key) <= allowedDistance)
            {
                if (tree.deleted)
                {
                    tree.deleted = false;
                    tree.prev    = prev;
                }
                // Else duplicate key
                // Check if the distance is longer then it might be the complete edge
                if (tree.prev != null && key.Distance(prev.key) > tree.key.Distance(tree.prev.key))
                {
                    tree.prev = prev;
                }
            }
            else if ((level == 0 && key.X > tree.key.X) || (level == 1 && key.Y > tree.key.Y))
            {
                tree.right = insertWithError(key, prev, allowedDistance, tree.right, (level + 1) % 2);
            }
            else
            {
                tree.left = insertWithError(key, prev, allowedDistance, tree.left, (level + 1) % 2);
            }

            return(tree);
        }
Beispiel #4
0
        public static TwoDNode remove(Point key, TwoDNode tree, int level, ref bool deleted)
        {
            if (tree == null)
            {
                return(null);
            }

            if (!tree.deleted && key == tree.key)
            {
                tree.deleted = true;
                deleted      = true;
            }
            else if ((level == 0 && key.X > tree.key.X) || (level == 1 && key.Y > tree.key.Y))
            {
                tree.right = remove(key, tree.right, (level + 1) % 2, ref deleted);
            }
            else
            {
                tree.left = remove(key, tree.left, (level + 1) % 2, ref deleted);
            }

            if (!tree.deleted || tree.left != null || tree.right != null)
            {
                return(tree);
            }
            else
            {
                return(null);
            }
        }
Beispiel #5
0
 public void Remove(Point key)
 {
     bool deleted = false;
     root = TwoDNode.remove(new Point(key), root, 0, ref deleted);
     if (deleted)
         count--;
 }
Beispiel #6
0
#pragma warning disable 0642
        public static void rsearch(Point lowKey, Point upperKey, TwoDNode tree, int level, List <TwoDNode> v)
        {
            if (tree == null)
            {
                return;
            }

            if ((level == 0 && lowKey.X <= tree.key.X) || (level == 1 && lowKey.Y <= tree.key.Y))
            {
                rsearch(lowKey, upperKey, tree.left, (level + 1) % 2, v);
            }

            if (lowKey.X <= tree.key.X && upperKey.X >= tree.key.X)
            {
                ;
            }
            else if (lowKey.Y <= tree.key.Y && upperKey.Y >= tree.key.Y)
            {
                ;
            }
            else if (!tree.deleted)
            {
                v.Add(tree);
            }

            if ((level == 0 && upperKey.X > tree.key.X) || (level == 1 && upperKey.Y > tree.key.Y))
            {
                rsearch(lowKey, upperKey, tree.right, (level + 1) % 2, v);
            }
        }
Beispiel #7
0
 private void preorder(TwoDNode node, List<TwoDNode> list)
 {
     list.Add(node);
     if (node.Left != null)
         preorder(node.Left, list);
     if (node.Right != null)
         preorder(node.Right, list);
 }
Beispiel #8
0
        public void Remove(Point key)
        {
            bool deleted = false;

            root = TwoDNode.remove(new Point(key), root, 0, ref deleted);
            if (deleted)
            {
                count--;
            }
        }
Beispiel #9
0
 private void inorder(TwoDNode node, List <TwoDNode> list)
 {
     if (node.Left != null)
     {
         inorder(node.Left, list);
     }
     list.Add(node);
     if (node.Right != null)
     {
         inorder(node.Right, list);
     }
 }
Beispiel #10
0
 private void preorder(TwoDNode node, List <TwoDNode> list)
 {
     list.Add(node);
     if (node.Left != null)
     {
         preorder(node.Left, list);
     }
     if (node.Right != null)
     {
         preorder(node.Right, list);
     }
 }
Beispiel #11
0
        public TwoDNode[] Nearest(Point key, int n)
        {
            TwoDNode[]          nbrs = new TwoDNode[n];
            NearestNeighborList nnl  = new NearestNeighborList(n);

            Rect   rect         = Rect.inifiniteRect();
            double max_dist_sqd = Double.MaxValue;
            Point  keyp         = new Point(key);

            TwoDNode.nnbr(root, keyp, rect, max_dist_sqd, 0, nnl);

            for (int i = 0; i < n; ++i)
            {
                TwoDNode kd = (TwoDNode)nnl.removeHighest();
                nbrs[n - i - 1] = kd;
            }

            return(nbrs);
        }
Beispiel #12
0
        public TwoDNode[] Nearest(Point key, int n)
        {
            TwoDNode[] nbrs = new TwoDNode[n];
            NearestNeighborList nnl = new NearestNeighborList(n);

            Rect rect = Rect.inifiniteRect();
            double max_dist_sqd = Double.MaxValue;
            Point keyp = new Point(key);

            TwoDNode.nnbr(root, keyp, rect, max_dist_sqd, 0, nnl);

            for (int i = 0; i < n; ++i)
            {
                TwoDNode kd = (TwoDNode)nnl.removeHighest();
                nbrs[n - i - 1] = kd;
            }

            return nbrs;
        }
Beispiel #13
0
        public static TwoDNode search(Point key, TwoDNode tree)
        {
            for (int level = 0; tree != null; level = (level + 1) % 2)
            {
                if (!tree.deleted && key == tree.key)
                {
                    return(tree);
                }
                else if ((level == 0 && key.X > tree.key.X) || (level == 1 && key.Y > tree.key.Y))
                {
                    tree = tree.right;
                }
                else
                {
                    tree = tree.left;
                }
            }

            return(null);
        }
Beispiel #14
0
 private TwoDNode(Point key, TwoDNode prev)
 {
     this.key = key;
     this.prev = prev;
 }
Beispiel #15
0
        public static TwoDNode insert(Point key, TwoDNode prev, TwoDNode tree, int level)
        {
            if (tree == null)
                tree = new TwoDNode(key, prev);
            else if (key == tree.key)
            {
                // Re-Insert
                if (tree.deleted)
                {
                    tree.deleted = false;
                    tree.prev = prev;
                }
                // Else duplicate key
            }
            else if ((level == 0 && key.X > tree.key.X) || (level == 1 && key.Y > tree.key.Y))
                tree.right = insert(key, prev, tree.right, (level + 1) % 2);
            else
                tree.left = insert(key, prev, tree.left, (level + 1) % 2);

            return tree;
        }
Beispiel #16
0
        public static TwoDNode insertWithError(Point key, TwoDNode prev, double allowedDistance, TwoDNode tree, int level)
        {
            if (tree == null)
                tree = new TwoDNode(key, prev);
            else if (key.Distance(tree.key) <= allowedDistance)
            {
                if (tree.deleted)
                {
                    tree.deleted = false;
                    tree.prev = prev;
                }
                // Else duplicate key
                // Check if the distance is longer then it might be the complete edge
                if (tree.prev != null && key.Distance(prev.key) > tree.key.Distance(tree.prev.key))
                    tree.prev = prev;
            }
            else if ((level == 0 && key.X > tree.key.X) || (level == 1 && key.Y > tree.key.Y))
                tree.right = insertWithError(key, prev, allowedDistance, tree.right, (level + 1) % 2);
            else
                tree.left = insertWithError(key, prev, allowedDistance, tree.left, (level + 1) % 2);

            return tree;
        }
Beispiel #17
0
 private TwoDNode(Point key, TwoDNode prev)
 {
     this.key  = key;
     this.prev = prev;
 }
Beispiel #18
0
        public static TwoDNode remove(Point key, TwoDNode tree, int level, ref bool deleted)
        {
            if (tree == null)
                return null;

            if (!tree.deleted && key == tree.key)
            {
                tree.deleted = true;
                deleted = true;
            }
            else if ((level == 0 && key.X > tree.key.X) || (level == 1 && key.Y > tree.key.Y))
                tree.right = remove(key, tree.right, (level + 1) % 2, ref deleted);
            else
                tree.left = remove(key, tree.left, (level + 1) % 2, ref deleted);

            if (!tree.deleted || tree.left != null || tree.right != null)
                return tree;
            else
                return null;
        }
Beispiel #19
0
        public TwoDNode Search(Point key)
        {
            TwoDNode n = TwoDNode.search(key, root);

            return(n);
        }
Beispiel #20
0
 public void InsertWithError(Point key, TwoDNode prev, double distance)
 {
     root = TwoDNode.insertWithError(new Point(key), prev, distance, root, 0);
 }
Beispiel #21
0
        public static TwoDNode search(Point key, TwoDNode tree)
        {
            for (int level = 0; tree != null; level = (level + 1) % 2)
            {
                if (!tree.deleted && key == tree.key)
                    return tree;
                else if ((level == 0 && key.X > tree.key.X) || (level == 1 && key.Y > tree.key.Y))
                    tree = tree.right;
                else
                    tree = tree.left;
            }

            return null;
        }
Beispiel #22
0
#pragma warning restore 0642

        public static void nnbr(TwoDNode kd, Point target, Rect hr, double max_dist_sqd, int lev, NearestNeighborList nnl)
        {
            if (kd == null)
                return;

            int s = lev % 2;

            Point pivot = kd.key;
            double pivot_to_target = pivot.SquareDistance(target);

            Rect left_r = hr;
            Rect right_r = new Rect(hr);

            bool target_in_left;
            if (s == 0)
            {
                left_r.max.X = pivot.X;
                right_r.min.X = pivot.X;
                target_in_left = target.X < pivot.X;
            }
            else
            {
                left_r.max.Y = pivot.Y;
                right_r.min.Y = pivot.Y;
                target_in_left = target.Y < pivot.Y;
            }

            TwoDNode nearer_node;
            Rect nearer_rect;
            TwoDNode further_node;
            Rect further_rect;

            if (target_in_left)
            {
                nearer_node = kd.left;
                nearer_rect = left_r;
                further_node = kd.right;
                further_rect = right_r;
            }
            else
            {
                nearer_node = kd.right;
                nearer_rect = right_r;
                further_node = kd.left;
                further_rect = left_r;
            }

            nnbr(nearer_node, target, nearer_rect, max_dist_sqd, lev + 1, nnl);

            TwoDNode nearest = (TwoDNode)nnl.getHighest();
            double dist_sqd;

            if (!nnl.isCapacityReached())
                dist_sqd = Double.MaxValue;
            else
                dist_sqd = nnl.getMaxPriority();

            max_dist_sqd = Math.Min(max_dist_sqd, dist_sqd);

            Point closest = further_rect.Closest(target);
            if (closest.Distance(target) < Math.Sqrt(max_dist_sqd))
            {
                if (pivot_to_target < dist_sqd)
                {
                    nearest = kd;

                    dist_sqd = pivot_to_target;

                    if (!kd.deleted)
                        nnl.insert(kd, dist_sqd);

                    if (nnl.isCapacityReached())
                        max_dist_sqd = nnl.getMaxPriority();
                    else
                        max_dist_sqd = Double.MaxValue;
                }

                nnbr(further_node, target, further_rect, max_dist_sqd, lev + 1, nnl);
                TwoDNode temp_nearest = (TwoDNode)nnl.getHighest();
                double temp_dist_sqd = nnl.getMaxPriority();

                if (temp_dist_sqd < dist_sqd)
                {
                    nearest = temp_nearest;
                    dist_sqd = temp_dist_sqd;
                }
            }
            else if (pivot_to_target < max_dist_sqd)
            {
                nearest = kd;
                dist_sqd = pivot_to_target;
            }
        }
Beispiel #23
0
#pragma warning disable 0642
        public static void rsearch(Point lowKey, Point upperKey, TwoDNode tree, int level, List<TwoDNode> v)
        {
            if (tree == null)
                return;

            if ((level == 0 && lowKey.X <= tree.key.X) || (level == 1 && lowKey.Y <= tree.key.Y))
                rsearch(lowKey, upperKey, tree.left, (level + 1) % 2, v);

            if (lowKey.X <= tree.key.X && upperKey.X >= tree.key.X)
                ;
            else if (lowKey.Y <= tree.key.Y && upperKey.Y >= tree.key.Y)
                ;
            else if (!tree.deleted)
                v.Add(tree);

            if ((level == 0 && upperKey.X > tree.key.X) || (level == 1 && upperKey.Y > tree.key.Y))
                rsearch(lowKey, upperKey, tree.right, (level + 1) % 2, v);
        }
Beispiel #24
0
#pragma warning restore 0642

        public static void nnbr(TwoDNode kd, Point target, Rect hr, double max_dist_sqd, int lev, NearestNeighborList nnl)
        {
            if (kd == null)
            {
                return;
            }

            int s = lev % 2;

            Point  pivot           = kd.key;
            double pivot_to_target = pivot.SquareDistance(target);

            Rect left_r  = hr;
            Rect right_r = new Rect(hr);

            bool target_in_left;

            if (s == 0)
            {
                left_r.max.X   = pivot.X;
                right_r.min.X  = pivot.X;
                target_in_left = target.X < pivot.X;
            }
            else
            {
                left_r.max.Y   = pivot.Y;
                right_r.min.Y  = pivot.Y;
                target_in_left = target.Y < pivot.Y;
            }

            TwoDNode nearer_node;
            Rect     nearer_rect;
            TwoDNode further_node;
            Rect     further_rect;

            if (target_in_left)
            {
                nearer_node  = kd.left;
                nearer_rect  = left_r;
                further_node = kd.right;
                further_rect = right_r;
            }
            else
            {
                nearer_node  = kd.right;
                nearer_rect  = right_r;
                further_node = kd.left;
                further_rect = left_r;
            }

            nnbr(nearer_node, target, nearer_rect, max_dist_sqd, lev + 1, nnl);

            TwoDNode nearest = (TwoDNode)nnl.getHighest();
            double   dist_sqd;

            if (!nnl.isCapacityReached())
            {
                dist_sqd = Double.MaxValue;
            }
            else
            {
                dist_sqd = nnl.getMaxPriority();
            }

            max_dist_sqd = Math.Min(max_dist_sqd, dist_sqd);

            Point closest = further_rect.Closest(target);

            if (closest.Distance(target) < Math.Sqrt(max_dist_sqd))
            {
                if (pivot_to_target < dist_sqd)
                {
                    nearest = kd;

                    dist_sqd = pivot_to_target;

                    if (!kd.deleted)
                    {
                        nnl.insert(kd, dist_sqd);
                    }

                    if (nnl.isCapacityReached())
                    {
                        max_dist_sqd = nnl.getMaxPriority();
                    }
                    else
                    {
                        max_dist_sqd = Double.MaxValue;
                    }
                }

                nnbr(further_node, target, further_rect, max_dist_sqd, lev + 1, nnl);
                TwoDNode temp_nearest  = (TwoDNode)nnl.getHighest();
                double   temp_dist_sqd = nnl.getMaxPriority();

                if (temp_dist_sqd < dist_sqd)
                {
                    nearest  = temp_nearest;
                    dist_sqd = temp_dist_sqd;
                }
            }
            else if (pivot_to_target < max_dist_sqd)
            {
                nearest  = kd;
                dist_sqd = pivot_to_target;
            }
        }
Beispiel #25
0
 public void Insert(Point key, TwoDNode prev)
 {
     root = TwoDNode.insert(new Point(key), prev, root, 0);
 }
Beispiel #26
0
 public void InsertWithError(Point key, TwoDNode prev, double distance)
 {
     root = TwoDNode.insertWithError(new Point(key), prev, distance, root, 0);
 }
Beispiel #27
0
 public void Insert(Point key, TwoDNode prev)
 {
     root = TwoDNode.insert(new Point(key), prev, root, 0);
 }