static void Main(string[] args)
        {
            var tree = new AATree <string>();

            tree.Add("January");
            tree.Add("February");
            tree.Add("March");
            tree.Add("April");
            tree.Add("May");
            tree.Add("June");
            tree.Add("July");
            tree.Add("August");
            tree.Add("September");
            tree.Add("October");
            tree.Add("November");
            tree.Add("December");

            //for (int i = 1; i <= 1303; i++)
            //{
            //    tree.Add(i.ToString());
            //}

            Console.WriteLine();
            Console.WriteLine($"Tree has {tree.Count} nodes, depth {tree.Depth}");

            Console.WriteLine($"Tree contains \"August\": {tree.Contains("August")}");
            Console.WriteLine($"Tree contains \"Borktember\": {tree.Contains("Borktember")}");

            Console.WriteLine("Ordered tree traversal:");
            tree.TraverseTree();
        }
Ejemplo n.º 2
0
        public void Contains_NonEmptyTree_ReturnsCorrectAnswer()
        {
            var tree = new AATree <int>();

            tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            tree.Contains(6).Should().BeTrue();
            tree.Contains(999).Should().BeFalse();
        }
Ejemplo n.º 3
0
        public void Contains_NonEmptyTree_ReturnsCorrectAnswer()
        {
            var tree = new AATree <int>();

            tree.AddRange(new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });

            Assert.IsTrue(tree.Contains(6));
            Assert.IsFalse(tree.Contains(999));
        }
Ejemplo n.º 4
0
 public void ContainsTest()
 {
     CreateTree();
     Assert.IsTrue(m_tree.Contains(5));
     Assert.IsTrue(m_tree.Contains(0));
     Assert.IsTrue(m_tree.Contains(-999));
     Assert.IsTrue(m_tree.Contains(int.MaxValue));
     Assert.IsFalse(m_tree.Contains(-1));
 }
Ejemplo n.º 5
0
        public static void Main()
        {
            var tree = new AATree<int>();
            Console.WriteLine("The AA tree created.");
            var nums = new[] { -5, 20, 14, 11, 8, -3, 111, 7, 100, -55 };
            for (int i = 0; i < nums.Length; i++)
            {
                AddNumber(tree, nums[i]);
            }

            Console.WriteLine(tree.Contains(-5));
            Console.WriteLine(tree.Contains(8));
            Console.WriteLine(tree.Contains(1000));
        }
Ejemplo n.º 6
0
        public void TestByAddingRandomValues()
        {
            Random rnd = new Random();

            AATree <int> aatree = new AATree <int>();
            int          size   = 100;
            List <int>   list   = new List <int>(size);

            while (list.Count < size)
            {
                int val = rnd.Next();
                if (aatree.Add(val))
                {
                    list.Add(val);
                }
            }

            list.Sort();

            Assert.AreEqual(size, aatree.Count);

            IComparer <int> comparer = Comparer <int> .Default;

            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            Assert.IsTrue(aatree.Contains(list[50]));
        }
Ejemplo n.º 7
0
        public void Remove_MultipleKeys_TreeStillValid()
        {
            var tree = new AATree <int>();

            tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });

            Remove(4).Should().NotThrow();
            tree.Contains(4).Should().BeFalse();
            tree.Count.Should().Be(9);

            Remove(8).Should().NotThrow();
            tree.Contains(8).Should().BeFalse();
            tree.Count.Should().Be(8);

            Remove(1).Should().NotThrow();
            tree.Contains(1).Should().BeFalse();
            tree.Count.Should().Be(7);

            Validate(tree.Root);

            Action Remove(int x) => () => tree.Remove(x);
        }
Ejemplo n.º 8
0
        public void Remove_MultipleKeys_TreeStillValid()
        {
            var tree = new AATree <int>();

            tree.AddRange(new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });

            Remove(4).Should().NotThrow();
            Assert.IsFalse(tree.Contains(4));
            Assert.AreEqual(9, tree.Count);

            Remove(8).Should().NotThrow();
            Assert.IsFalse(tree.Contains(8));
            Assert.AreEqual(8, tree.Count);

            Remove(1).Should().NotThrow();
            Assert.IsFalse(tree.Contains(1));
            Assert.AreEqual(7, tree.Count);

            Validate(tree.Root);

            Action Remove(int x) => () => tree.Remove(x);
        }
Ejemplo n.º 9
0
        public void Add_MultipleKeys_FormsCorrectTree()
        {
            var tree = new AATree <int>();

            foreach (var elem in new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })
            {
                tree.Add(elem);
                tree.Count.Should().Be(elem);
                tree.Contains(elem).Should().BeTrue();
            }

            tree.GetKeysInOrder().SequenceEqual(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).Should().BeTrue();
            tree.GetKeysPostOrder().SequenceEqual(new[] { 1, 3, 2, 5, 7, 10, 9, 8, 6, 4 }).Should().BeTrue();
            Validate(tree.Root);
        }
    // Test program; should print min and max and nothing else
    public static void Main(string[] args)
    {
        AATree <int> t    = new AATree <int>(-9999);
        const int    NUMS = 40000;
        const int    GAP  = 307;

        Console.WriteLine("Checking... (no bad output means success)");

        t.Insert(NUMS * 2);
        t.Insert(NUMS * 3);
        for (int i = GAP; i != 0; i = (i + GAP) % NUMS)
        {
            t.Insert(i);
        }
        Console.WriteLine("Inserts complete");

        t.Remove(t.FindMax( ));
        for (int i = 1; i < NUMS; i += 2)
        {
            t.Remove(i);
        }
        t.Remove(t.FindMax( ));
        Console.WriteLine("Removes complete");


        if (t.FindMin( ) != 2 || t.FindMax( ) != NUMS - 2)
        {
            Console.WriteLine("FindMin or FindMax error!");
        }

        for (int i = 2; i < NUMS; i += 2)
        {
            if (t.Find(i) != i)
            {
                Console.WriteLine("Error: find fails for " + i);
            }
        }

        for (int i = 1; i < NUMS; i += 2)
        {
            if (t.Contains(i))
            {
                Console.WriteLine("Error: Found deleted item " + i);
            }
        }
    }
Ejemplo n.º 11
0
        public void TestByAddingValuesSortedByAscOrder()
        {
            AATree <int> aatree = new AATree <int>();
            int          size   = 100;
            List <int>   list   = new List <int>(size);

            for (int i = 0; i < size; i++)
            {
                aatree.Add(i);
                list.Add(i);
            }

            Assert.AreEqual(size, aatree.Count);
            IComparer <int> comparer = Comparer <int> .Default;

            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            Assert.IsTrue(aatree.Contains(50));
        }
Ejemplo n.º 12
0
        public void Add_MultipleKeys_FormsCorrectTree()
        {
            var tree = new AATree <int>();

            foreach (int elem in new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })
            {
                tree.Add(elem);
                Assert.AreEqual(elem, tree.Count);
                Assert.IsTrue(tree.Contains(elem));
            }

            var expected = new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var actual   = tree.GetKeysInOrder();

            Assert.IsTrue(Enumerable.SequenceEqual(expected, actual));

            expected = new [] { 1, 3, 2, 5, 7, 10, 9, 8, 6, 4 };
            actual   = tree.GetKeysPostOrder();
            Assert.IsTrue(Enumerable.SequenceEqual(expected, actual));

            Validate(tree.Root);
        }
Ejemplo n.º 13
0
        public void Contains_EmptyTree_ReturnsFalse()
        {
            var tree = new AATree <int>();

            tree.Contains(999).Should().BeFalse();
        }
Ejemplo n.º 14
0
        public void Contains_EmptyTree_ReturnsFalse()
        {
            var tree = new AATree <int>();

            Assert.IsFalse(tree.Contains(999));
        }
Ejemplo n.º 15
0
        public static Polygon2D Vision(Polygon2DWithHoles polygon, Vector2 x)
        {
            if (!(polygon.ContainsInside(x) || polygon.OnBoundary(x)))
            {
                throw new ArgumentException(x + " is not inside polygon: " + polygon);
            }

            float initAngle;
            var   events = Preprocess(polygon, x, out initAngle);

            var status     = new AATree <StatusItem>();
            var visibility = new List <Vector2>();

            // create ray in positive x direction
            var ray = new Ray2D(Vector2.zero, new Vector2(1f, 0f));

            VisibilityEvent xEvent = null;

            // initialize the status
            foreach (var v in events)
            {
                if (MathUtil.EqualsEps(v.vertex, Vector2.zero))
                {
                    xEvent = v;
                    continue;
                }

                var seg = v.item1.seg;
                if (!seg.IsEndpoint(x))
                {
                    var intersect = seg.Intersect(ray);
                    if (intersect.HasValue && intersect.Value.x > 0 && (seg.Point1.y >= 0 != seg.Point2.y >= 0) &&
                        !MathUtil.EqualsEps(intersect.Value, Vector2.zero))
                    {
                        status.Insert(v.item1);
                    }
                }
            }

            if (xEvent != null)
            {
                if (!xEvent.isHole)
                {
                    status.Insert(xEvent.item1);
                }
                else
                {
                    status.Delete(xEvent.item1);
                }

                if (!xEvent.isHole)
                {
                    status.Delete(xEvent.item2);
                }
                else
                {
                    status.Insert(xEvent.item2);
                }

                if (!xEvent.isHole)
                {
                    visibility.Add(xEvent.item2.seg.Point2);
                    visibility.Add(Vector2.zero);
                    visibility.Add(xEvent.item1.seg.Point1);
                }
                else
                {
                    visibility.Add(xEvent.item2.seg.Point1);
                    visibility.Add(Vector2.zero);
                    visibility.Add(xEvent.item1.seg.Point2);
                }
            }

            // handle events
            StatusItem top        = null;
            var        insertions = new HashSet <StatusItem>();

            if (status.Count > 0)
            {
                status.FindMin(out top);
            }
            for (var i = 0; i < events.Count; i++)
            {
                var v = events[i];

                if (MathUtil.EqualsEps(v.vertex, Vector2.zero))
                {
                    continue;
                }

                ray = new Ray2D(Vector2.zero, v.vertex);

                // first handle deletions

                // handle first segment
                if (status.Contains(v.item1))
                {
                    status.Delete(v.item1);
                }
                else if (insertions.Contains(v.item1))
                {
                    insertions.Remove(v.item1);
                }
                else
                {
                    insertions.Add(v.item1);
                }

                // handle second segment
                if (status.Contains(v.item2))
                {
                    status.Delete(v.item2);
                }
                else if (insertions.Contains(v.item2))
                {
                    insertions.Remove(v.item2);
                }
                else
                {
                    insertions.Add(v.item2);
                }

                // skip if next event colinear with current
                if (i < events.Count - 1 && Line.Colinear(Vector2.zero, v.vertex, events[i + 1].vertex))
                {
                    // skip until all colinear events are handled
                    continue;
                }

                // handle insertions (after potential skip for colinear events
                foreach (var item in insertions)
                {
                    status.Insert(item);
                }
                insertions.Clear();

                StatusItem newTop;
                status.FindMin(out newTop);

                // do stuff if current top different from previous
                if (top != newTop)
                {
                    // add intersections with previous top segment
                    if (top != null)
                    {
                        HandleTopSegment(ref visibility, top.seg, ray);
                    }

                    // add intersections with new top segment
                    if (newTop != null)
                    {
                        HandleTopSegment(ref visibility, newTop.seg, ray);
                    }

                    top = newTop;
                }
            }

            return(Postprocess(visibility, x, initAngle));
        }