public static void Main()
        {
            var testTree = new AATree<int>(10);
            testTree.Add(1);
            testTree.Add(7);
            testTree.Add(11);
            testTree.Add(-12);

            foreach (var item in testTree)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();

            testTree.Remove(7);
            foreach (var item in testTree)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();
            testTree.Remove(11);
            foreach (var item in testTree)
            {
                Console.WriteLine(item);
            }

            testTree.Remove(122);
        }
        public void RemoveSingleElementFromMultipleAdded()
        {
            AATree<int> tree = new AATree<int>();
            tree.Add(3);
            tree.Add(20);
            var newRoot = tree.Remove(20);

            Assert.AreEqual(3, newRoot.Value);
        }
Ejemplo n.º 3
0
        public void Add_KeyAlreadyInTree_ThrowsException()
        {
            var tree = new AATree <int>();

            tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            Assert.Throws <ArgumentException>(() => tree.Add(1));
        }
 public void AddRemoveSingleElementShouldWorkCorrectly()
 {
     AATree<int> tree = new AATree<int>();
     tree.Add(3);
     var result = tree.Remove(3);
     Assert.AreEqual(null, result);
 }
Ejemplo n.º 5
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.º 6
0
        public static void Test(int[] values)
        {
            AATree<int, int> tree = new AATree<int, int>();
            for (int i = 0; i < values.Length; i++)
            {
                if (!tree.Add(values[i], i + 1))
                {
                    Console.WriteLine("Failed to insert {0}", values[i]);
                }
            }

            for (int i = 0; i < values.Length; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    if (tree[values[j]] != 0)
                    {
                        Console.WriteLine("Found deleted key {0}", values[j]);
                    }
                }

                for (int j = i; j < values.Length; j++)
                {
                    if (tree[values[j]] != (j + 1))
                    {
                        Console.WriteLine("Could not find key {0}", values[j]);
                    }
                }

                if (!tree.Remove(values[i]))
                {
                    Console.WriteLine("Failed to delete {0}", values[i]);
                }
            }
        }
Ejemplo n.º 7
0
    static void Test(int[] values)
    {
        AATree <int, int> tree = new AATree <int, int>();

        for (int i = 0; i < values.Length; i++)
        {
            if (!tree.Add(values[i], (i + 1)))
            {
                Console.WriteLine("Failed to insert {0}", values[i]);
            }
        }
        for (int i = 0; i < values.Length; i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (tree[values[j]] != 0)
                {
                    Console.WriteLine("Found deleted key {0}", values[j]);
                }
            }
            for (int j = i; j < values.Length; j++)
            {
                if (tree[values[j]] != (j + 1))
                {
                    Console.WriteLine("Could not find key {0}", values[j]);
                }
            }
            if (!tree.Remove(values[i]))
            {
                Console.WriteLine("Failed to delete {0}", values[i]);
            }
        }
    }
Ejemplo n.º 8
0
 static void Main(string[] args)
 {
     AATree<int> tree = new AATree<int>();
     tree.Add(3);
     var result = tree.Remove(3);
     Console.WriteLine(result);
 }
Ejemplo n.º 9
0
        public void TestRemoveMethodWithValuesSortByDescOrder()
        {
            IComparer <int> comparer = Comparer <int> .Default;
            AATree <int>    aatree   = new AATree <int>();
            int             size     = 10000;
            List <int>      list     = new List <int>(size);

            for (int i = size - 1; i >= 0; i--)
            {
                int val = i;
                if (aatree.Add(val))
                {
                    list.Add(val);
                }
            }

            list.Sort();

            Assert.AreEqual(size, aatree.Count);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            int index = 10;

            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            index = 20;
            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            index = 30;
            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            index = 40;
            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            index = 50;
            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            index = list.Count - 1;
            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            index = 0;
            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            Assert.AreEqual(list.Count, aatree.Count);
        }
Ejemplo n.º 10
0
        public void TestRemoveMethodWithRandomValues()
        {
            Random          rnd      = new Random();
            IComparer <int> comparer = Comparer <int> .Default;
            AATree <int>    aatree   = new AATree <int>();
            int             size     = 1000;
            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);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            int index = 10;

            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));
            index = 20;
            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            index = 30;
            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            index = 40;
            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            index = 50;
            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            index = list.Count - 1;
            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            index = 0;
            Assert.IsTrue(aatree.Remove(list[index]));
            list.RemoveAt(index);
            Assert.IsTrue(ValidateInOrder(aatree, list, comparer));

            Assert.AreEqual(list.Count, aatree.Count);
        }
Ejemplo n.º 11
0
    public static void AddNumber(AATree <int, string> tree, int key, string value)
    {
        tree.Add(key, value);
        Console.WriteLine("Added " + key);

        DisplayTree(tree.Root, string.Empty);
        Console.WriteLine("----------------------");
    }
Ejemplo n.º 12
0
        public static void AddNumber(AATree<int, string> tree, int key, string value)
        {
            tree.Add(key, value);
            Console.WriteLine("Added " + key);

            DisplayTree(tree.Root, string.Empty);
            Console.WriteLine("----------------------");
        }
        public static void AddNumber(AATree<int> tree, int value)
        {
            if (tree.Root.IsSentinel())
                tree.Root = new AATree<int>.AANode(value);

            tree.Root = tree.Add(value, tree.Root);
            Console.WriteLine("Added " + value);

            DisplayTree(tree.Root, string.Empty);
            Console.WriteLine("----------------------");
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
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.º 16
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);
        }
        static void SearchExample()
        {
            var tree = new AATree<int>();
            tree.Add(4);
            tree.Add(10);
            tree.Add(2);
            tree.Add(6);
            tree.Add(12);
            tree.Add(3);
            tree.Add(1);
            tree.Add(8);
            tree.Add(13);
            tree.Add(11);
            tree.Add(5);
            tree.Add(9);
            tree.Add(7);

            Console.WriteLine("All results should be true");
            Console.WriteLine(tree.Find(4));
            Console.WriteLine(!tree.Find(-5));
            Console.WriteLine(!tree.Find(100));
            Console.WriteLine(!tree.Find(15));
            Console.WriteLine(tree.Find(13));
            Console.WriteLine(tree.Find(7));
        }
Ejemplo n.º 18
0
    static void SearchExample()
    {
        var tree = new AATree<int>();
        tree.Add(4);
        tree.Add(10);
        tree.Add(2);
        tree.Add(6);
        tree.Add(12);
        tree.Add(3);
        tree.Add(1);
        tree.Add(8);
        tree.Add(13);
        tree.Add(11);
        tree.Add(5);
        tree.Add(9);
        tree.Add(7);

        Console.WriteLine("All results should be true");
        Console.WriteLine(tree.Find(4));
        Console.WriteLine(!tree.Find(-5));
        Console.WriteLine(!tree.Find(100));
        Console.WriteLine(!tree.Find(15));
        Console.WriteLine(tree.Find(13));
        Console.WriteLine(tree.Find(7));
    }
        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.º 20
0
    static void InsertDeleteExample()
    {
        var tree = new AATree<int>();
        tree.Add(4);
        tree.Add(10);
        tree.Add(2);
        tree.Add(6);
        tree.Add(12);
        tree.Add(3);
        tree.Add(1);
        tree.Add(8);
        tree.Add(13);
        tree.Add(11);
        tree.Add(5);
        tree.Add(9);
        tree.Add(7);

        tree.Remove(1);
        tree.Print();

        tree.Remove(5);
        Console.WriteLine(new string('-', 40));
        tree.Print();
    }
        static void InsertDeleteExample()
        {
            var tree = new AATree<int>();
            tree.Add(4);
            tree.Add(10);
            tree.Add(2);
            tree.Add(6);
            tree.Add(12);
            tree.Add(3);
            tree.Add(1);
            tree.Add(8);
            tree.Add(13);
            tree.Add(11);
            tree.Add(5);
            tree.Add(9);
            tree.Add(7);

            tree.Remove(1);
            tree.Print();

            tree.Remove(5);
            Console.WriteLine(new string('-', 40));
            tree.Print();
        }