Ejemplo n.º 1
0
        public void SplitBeachSection(Point site)
        {
            BeachSection containingBeachSection = BeachSectionContainingPoint(site);
            BeachSection newBeachSectionLeft    = new BeachSection(containingBeachSection.focus, containingBeachSection.leftBoundary, site);
            BeachSection newBeachSectionCentre  = new BeachSection(site, containingBeachSection.focus, containingBeachSection.focus);
            BeachSection newBeachSectionRight   = new BeachSection(containingBeachSection.focus, site, containingBeachSection.rightBoundary);

            beachSections.Remove(containingBeachSection);
            beachSections.Add(newBeachSectionLeft);
            beachSections.Add(newBeachSectionCentre);
            beachSections.Add(newBeachSectionRight);

            foreach (IntersectEventPoint iep in IntersectEventPoint.FromBeachSections(new List <BeachSection> {
                newBeachSectionLeft, newBeachSectionRight
            }))
            {
                if (iep.Point().y <= site.y)
                {
                    eventQueue.Add(iep);
                }
            }

            foreach (IntersectEventPoint iep in IntersectEventPoint.FromBeachSections(new List <BeachSection> {
                containingBeachSection
            }))
            {
                if (iep.Point().y <= site.y)
                {
                    eventQueue.Remove(iep);
                }
            }
        }
		public void SetUp() {
			twoElementTree = new RBTree<int> ();
			firstElement = 1;
			secondElement = 2;
			absentElement = 3;
			twoElementTree.Add (firstElement);
			twoElementTree.Add (secondElement);
		}
Ejemplo n.º 3
0
        public void AddNode_WithKey_SmallerThan_RootKey_ShouldBe_OnLeft()
        {
            RBTree <int> tree = new RBTree <int>();

            tree.Add(10);
            tree.Add(9);
            Assert.True(tree.Root.Key == 10);
            Assert.True(tree.Root.Left.Key == 9);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 计算从点s到其他所有点的路径
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public List <int> CalculatePath(int s)
        {
            List <bool>  visited = new List <bool>(polys.Count);
            List <float> length  = new List <float>(polys.Count);
            List <int>   from    = new List <int>(polys.Count);

            for (int i = 0; i < polys.Count; i++)
            {
                visited.Add(false);
                length.Add(Single.MaxValue);
                from.Add(-1);
            }

            RBTree <float, int> queue = new RBTree <float, int>();

            visited[s] = true;
            length[s]  = 0f;
            queue.Add(0f, s);
            while (queue.Count > 0)
            {
                KeyValuePair <float, int> vPair = queue.Min();
                int v = vPair.Value;
                queue.Remove(vPair.Key);
                //每次拿出来的最小值一定属于最短路径(贪心性质)
                visited[v] = true;

                if (NearPolys.ContainsKey(v))
                {
                    foreach (PolyConnection e in nearPolys[v])
                    {
                        int w = e.Other(v);
                        if (!visited[w])
                        {
                            float l = length[v] + e.Weight;
                            if (from[w] == -1 || l < length[w])
                            {
                                length[w] = l;
                                from[w]   = v;

                                if (queue.ContainsValue(w))
                                {//这里用sortedList的更新操作(删除再插入)来实现优先队列的更新
                                    queue.Remove(queue.KeyOfValue(w));
                                    queue.Add(l, w);
                                }
                                else
                                {
                                    queue.Add(l, w);
                                }
                            }
                        }
                    }
                }
            }
            return(from);
        }
Ejemplo n.º 5
0
        public void AddNode_WithKey_HigherThan_RootKey_ShouldBe_OnRightSubtree()
        {
            RBTree <int> tree = new RBTree <int>();

            tree.Add(10);
            tree.Add(100);
            Assert.True(tree.Root.Key == 10);
            Assert.True(tree.Root.Color == Black);
            Assert.True(tree.Root.Right.Key == 100);
            Assert.True(tree.Root.Right.Color == Red);
        }
Ejemplo n.º 6
0
        public void AddNode_InEmptyTree_ShouldBe_Root()
        {
            RBTree <int> tree = new RBTree <int>();

            tree.Add(10);
            Assert.True(tree.Root.Key == 10);
        }
Ejemplo n.º 7
0
        public void RedBlackTree_Should_BeBalanced()
        {
            RBTree <int> tree = new RBTree <int>();

            for (int i = 0; i < 10; i++)
            {
                tree.Add(i);
            }
            Assert.True(tree.IsBalanced());
        }
Ejemplo n.º 8
0
        public void GetNode_Should_ReturnNode()
        {
            RBTree <int> tree = new RBTree <int>();

            for (int i = 0; i < 10; i++)
            {
                tree.Add(i);
            }
            Assert.True(tree.GetNode(3).Key == 3);
        }
Ejemplo n.º 9
0
        public void GetNonExistentKey_ShouldBe_False()
        {
            RBTree <int> tree = new RBTree <int>();

            for (int i = 0; i < 10; i++)
            {
                tree.Add(i);
            }
            Assert.False(tree.GetNode(3).Key == 10);
        }
Ejemplo n.º 10
0
        public void Add_RedNode_Should_TriggerRotations_AndRecoloring()
        {
            RBTree <int> tree = new RBTree <int>();

            for (int i = 0; i < 10; i++)
            {
                tree.Add(i);
            }
            tree.Add(10);
            Assert.True(tree.Root.Right.Right.Right.Right.Key == 10);
            Assert.True(tree.Root.Right.Right.Right.Key == 9);
            Assert.True(tree.Root.Right.Right.Right.Left.Key == 8);
            Assert.True(tree.Root.Right.Right.Right.Left.Color == Red);
            Assert.True(tree.Root.Right.Right.Left.Key == 6);
            Assert.True(tree.Root.Right.Right.Left.Color == Black);
            Assert.True(tree.Root.Right.Key == 5);
            Assert.True(tree.Root.Right.Color == Black);
            Assert.True(tree.Root.Left.Key == 1);
            Assert.True(tree.Root.Left.Color == Black);
        }
Ejemplo n.º 11
0
        private static RBTree CreateTree()
        {
            RBTree tree = new RBTree();

            tree.Add(1);
            tree.Add(2);
            tree.Add(3);
            tree.Add(4);
            tree.Add(5);
            tree.Add(6);
            tree.Add(7);
            tree.Add(8);
            tree.Add(9);


            return(tree);
        }
Ejemplo n.º 12
0
        public void GetNonExistentKey_Should_ReturnDefault()
        {
            RBTree <int> tree = new RBTree <int>();

            for (int i = 0; i < 10; i++)
            {
                tree.Add(i);
            }
            var exception = Assert.Throws <KeyNotFoundException>(() => tree.GetNode(33));

            Assert.True(exception.Message == "Key not found.");
        }
Ejemplo n.º 13
0
        public void Delete_RedNode_With_NoChildren_Should_TriggerRotations_AndRecoloring()
        {
            RBTree <int> tree = new RBTree <int>();

            for (int i = 0; i < 10; i++)
            {
                tree.Add(i);
            }
            tree.Delete(tree.GetNode(7));
            var exception = Assert.Throws <KeyNotFoundException>(() => tree.GetNode(7));

            Assert.True(exception.Message == "Key not found.");
        }
Ejemplo n.º 14
0
        public BeachLine(IEnumerable <BeachSection> initialBeachSections, IEnumerable <Point> otherPoints)
        {
            beachSections = new RBTree <BeachSection>();
            eventQueue    = new EventQueue();

            foreach (BeachSection bs in initialBeachSections)
            {
                beachSections.Add(bs);
            }

            foreach (Point point in otherPoints)
            {
                eventQueue.Add(new SiteEventPoint(point));
            }
        }
Ejemplo n.º 15
0
        private static void TimeSpendTest()
        {
            RBTree   tree  = new RBTree();
            DateTime start = DateTime.Now;

            FileHelper.ReadFile(@"E:\数据\测试数据\numbers.txt", (num) => tree.Add(num));
            Console1.WriteLine("创建树耗时:" + (DateTime.Now - start).TotalMilliseconds);

            start = DateTime.Now;
            FileHelper.ReadFile(@"E:\数据\测试数据\numbers.txt", (num) => tree.FindNode(num));
            Console1.WriteLine("查找树耗时:" + (DateTime.Now - start).TotalMilliseconds);

            start = DateTime.Now;
            FileHelper.ReadFile(@"E:\数据\测试数据\numbers.txt", (num) => tree.Remove(num));
            Console1.WriteLine("清空树耗时:" + (DateTime.Now - start).TotalMilliseconds);
        }
Ejemplo n.º 16
0
        public void Test()
        {
            var tree = new RBTree <int, string>();

            tree.Add(1, "a");
            tree.Add(2, "b");
            tree.Add(3, "c");
            tree.Add(4, "d");
            tree.Add(5, "e");
            tree.Add(6, "f");

            var res = tree.GetValue(6);

            var t = tree;
        }