示例#1
0
        public void Contains_EmptyTree_ReturnsFalse()
        {
            var tree = new AvlTree <int>();

            tree.Contains(5).Should().BeFalse();
            tree.Contains(-12).Should().BeFalse();
        }
示例#2
0
        public void AvlTreeOperationsTest()
        {
            String value;
            AvlTree <Int32, String> tree = new AvlTree <Int32, String>();

            foreach (KeyValuePair <Int32, String> keyValuePair in this.values)
            {
                tree.Insert(keyValuePair.Key, keyValuePair.Value);
            }

            tree.Count.ShouldBe(this.values.Length);

            foreach (KeyValuePair <Int32, String> keyValuePair in this.values)
            {
                tree.Contains(keyValuePair.Key).ShouldBeTrue();
                tree.Search(keyValuePair.Key).ShouldBe(keyValuePair.Value);

                tree.TrySearch(keyValuePair.Key, out value).ShouldBeTrue();
                value.ShouldBe(keyValuePair.Value);
            }

            for (Int32 number = -1000; number < 1000; number++)
            {
                tree.Remove(number).ShouldBe(this.values.Select(keyValuePair => keyValuePair.Key).Contains(number));
            }

            tree.Count.ShouldBe(0);

            foreach (KeyValuePair <Int32, String> keyValuePair in this.values)
            {
                tree.Insert(keyValuePair.Key, keyValuePair.Value);
            }

            tree.Clear();

            tree.Count.ShouldBe(0);

            foreach (KeyValuePair <Int32, String> keyValuePair in this.values)
            {
                tree.Contains(keyValuePair.Key).ShouldBeFalse();
                tree.TrySearch(keyValuePair.Key, out value).ShouldBeFalse();
            }

            // exceptions

            Should.Throw <ArgumentException>(() => tree.Search(0));
            Should.Throw <ArgumentException>(() =>
            {
                tree.Insert(0, String.Empty);
                tree.Insert(0, String.Empty);
            });

            AvlTree <Object, String> objectTree = new AvlTree <Object, String>();

            Should.Throw <ArgumentNullException>(() => objectTree.Insert(null, null));
            Should.Throw <ArgumentNullException>(() => objectTree.Contains(null));
            Should.Throw <ArgumentNullException>(() => objectTree.TrySearch(null, out value));
            Should.Throw <ArgumentNullException>(() => objectTree.Remove(null));
        }
示例#3
0
        public void Contains_CorrectReturn()
        {
            var tree = new AvlTree <int>();

            tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            tree.Contains(3).Should().BeTrue();
            tree.Contains(7).Should().BeTrue();
            tree.Contains(24).Should().BeFalse();
            tree.Contains(-1).Should().BeFalse();
        }
        public void AddSameItems()
        {
            var item = 42;

            for (var i = 0; i < 42; i++)
            {
                _avlTree.Insert(item);
            }
            _avlTree.Contains(item).Should().BeTrue();
            _avlTree.Count.Should().Be(42);
        }
示例#5
0
        public void CompareSearchTime()
        {
            var itemsCount = 10000;

            var list = new List <int>();

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

            var startTime = DateTime.Now;

            list.Contains(5000);

            var listContainsTime = DateTime.Now - startTime;

            var avlTree = new AvlTree <int>();

            for (int i = 0; i < itemsCount; i++)
            {
                avlTree.Add(i);
            }

            startTime = DateTime.Now;

            avlTree.Contains(5000);

            var avlContainsTime = DateTime.Now - startTime;
        }
        public void CompareSearchTime()
        {
            var itemsCount = 10000;
            
            var list = new List<int>();
            for (int i = 0; i < itemsCount; i++)
            {
                list.Add(i);
            }

            var startTime = DateTime.Now;

            list.Contains(5000);

            var listContainsTime = DateTime.Now - startTime;

            var avlTree = new AvlTree<int>();

            for (int i = 0; i < itemsCount; i++)
            {
                avlTree.Add(i);
            }

            startTime = DateTime.Now;

            avlTree.Contains(5000);

            var avlContainsTime = DateTime.Now - startTime;
        }
示例#7
0
        private bool TestFind()
        {
            Console.WriteLine("Starting find test...");
            foreach (var i in HelpList)
            {
                var result = AvlTree.Find(i);
                if (result == 0)
                {
                    Console.WriteLine($"could not find value {i}");
                    return(false);
                }

                if (result != i)
                {
                    Console.WriteLine($"tree should find {i} but found {result}");
                    return(false);
                }

                if (!AvlTree.Contains(i))
                {
                    Console.WriteLine($"Contains test failed... result = {result} i = {i}");
                    return(false);
                }
            }

            Console.WriteLine("Find test successful");
            return(true);
        }
示例#8
0
        public void AvlTreeOperationsTest()
        {
            // test case 1: integer sequence

            AvlTree <Int32, String> avl = new AvlTree <Int32, String>();

            for (Int32 i = 0; i < 1000; i++)
            {
                avl.Insert(i, i.ToString());
            }

            for (Int32 i = 0; i < 1000; i++)
            {
                Assert.AreEqual(i.ToString(), avl.Search(i));
            }

            for (Int32 i = 0; i < 1000; i++)
            {
                Assert.IsTrue(avl.Remove(i));
            }

            Assert.AreEqual(0, avl.Count);


            // test case 2: random numbers

            avl = new AvlTree <Int32, String>();

            List <Int32> keyList = new List <Int32>();

            Random random = new Random();

            for (Int32 i = 0; i < 10000; i++)
            {
                Int32 key = random.Next(1, 100000);
                if (!avl.Contains(key))
                {
                    avl.Insert(key, key.ToString());
                    keyList.Add(key);
                }
            }

            Assert.AreEqual(keyList.Count, avl.Count);

            for (Int32 i = 0; i < keyList.Count; i++)
            {
                Assert.AreEqual(keyList[i].ToString(), avl.Search(keyList[i]));
            }

            for (Int32 i = 0; i < keyList.Count; i++)
            {
                Assert.IsTrue(avl.Remove(keyList[i]));
            }

            Assert.AreEqual(0, avl.Count);
        }
示例#9
0
        public void TestRemove()
        {
            var tree = new AvlTree <TestNode> ();
            var t1   = new TestNode(1);
            var t2   = new TestNode(2);
            var t3   = new TestNode(3);

            tree.Add(t2);
            tree.Add(t1);
            tree.Add(t3);
            Assert.AreEqual(3, tree.Count);

            Assert.IsTrue(tree.Remove(t2));

            Assert.AreEqual(2, tree.Count);
            Assert.IsTrue(tree.Contains(t1));
            Assert.IsFalse(tree.Contains(t2));
            Assert.IsTrue(tree.Contains(t3));
        }
示例#10
0
        public void TestInOrderRemoval()
        {
            var tree = new AvlTree <TestNode> ();

            TestNode[] nodes = new TestNode[10];
            for (int i = 0; i < 10; i++)
            {
                tree.Add(nodes [i] = new TestNode(i));
            }
            Assert.AreEqual(10, tree.Count);

            for (int i = 0; i < 10; i++)
            {
                Assert.IsTrue(tree.Contains(nodes[i]), "case : " + i);
                tree.Remove(nodes[i]);
                Assert.IsFalse(tree.Contains(nodes[i]), "case : " + i);
            }
            Assert.AreEqual(0, tree.Count);
        }
示例#11
0
        private bool TestDelete(int count)
        {
            Console.WriteLine("Starting delete test...");
            var random = new Random(50);

            HelpList.Sort((i1, i2) => i1 - i2);
            for (int i = 0; i < count; i++)
            {
                var index = random.Next(0, HelpList.Count - 1);
                var value = HelpList[index];
                HelpList.Remove(value);
                if (!AvlTree.Contains(value))
                {
                    Console.WriteLine($"Tree does not contain that value {value}");
                    return(false);
                }
                var deletedValue = AvlTree.Delete(value);
                if (value != deletedValue)
                {
                    Console.WriteLine($"Wrong data deleted... should: {value}, was: {deletedValue}");
                    return(false);
                }

                if (AvlTree.Contains(value))
                {
                    Console.WriteLine($"Tree still contains removed value {value}");
                    return(false);
                }

                if (i % 100 == 0)
                {
                    if (!InOrderTest(false))
                    {
                        Console.WriteLine("In order test failed during deletion");
                        return(false);
                    }
                }
            }

            if (!CountTest(true))
            {
                Console.WriteLine("Count test failed right after deletion");
                return(false);
            }

            if (!InOrderTest(true))
            {
                Console.WriteLine("In order test failed right after deletion");
                return(false);
            }

            Console.WriteLine("delete test successful");
            return(true);
        }
    static void Main()
    {
        var tree = new AvlTree <int>();

        Console.WriteLine("Count: " + tree.Count());
        Console.WriteLine("Height: " + tree.Height());

        for (int i = 0; i < 10000; ++i)
        {
            tree.Add(i);
        }

        Console.WriteLine("Count: " + tree.Count());
        Console.WriteLine("Height: " + tree.Height());

        for (int i = 0; i < 10000; ++i)
        {
            if (!tree.Contains(i))
            {
                throw new Exception("Doesn't work");
            }
        }

        for (int i = 0; i < 10000; ++i)
        {
            tree.Remove(i);
        }

        Console.WriteLine("Count: " + tree.Count());
        Console.WriteLine("Height: " + tree.Height());

        for (int i = 0; i < 10000; ++i)
        {
            if (tree.Contains(i))
            {
                throw new Exception("Doesn't work");
            }
        }

        Console.WriteLine("Works");
    }
示例#13
0
        public void Remove_MultipleKeys_TreeStillValid()
        {
            var tree = new AvlTree <int>();

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

            tree.Remove(7);
            tree.Count.Should().Be(9);
            tree.Contains(7).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 1, 2, 3, 4, 5, 6, 8, 9, 10 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 4, 2, 1, 3, 8, 6, 5, 9, 10 }).Should().BeTrue();

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

            tree.Remove(1);
            tree.Count.Should().Be(7);
            tree.Contains(1).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 3, 4, 5, 6, 8, 9, 10 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 8, 4, 3, 6, 5, 9, 10 }).Should().BeTrue();

            tree.Remove(9);
            tree.Count.Should().Be(6);
            tree.Contains(9).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 3, 4, 5, 6, 8, 10 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 6, 4, 3, 5, 8, 10 }).Should().BeTrue();

            tree.Remove(3);
            tree.Remove(4);
            tree.Remove(5);
            tree.Remove(6);
            tree.Remove(8);
            tree.Remove(10);

            tree.Count.Should().Be(0);
            tree.GetKeysInOrder().SequenceEqual(Array.Empty <int>()).Should().BeTrue();
        }
示例#14
0
        public void Contains_NonAddedElement_ShouldReturnFalse()
        {
            var numbers = TestUtils.ToIntArrayUnique("1 7 3 -4 10 0");
            var tree    = new AvlTree <int>();

            foreach (int number in numbers)
            {
                tree.Add(number);
            }

            var contains2 = tree.Contains(2);

            Assert.IsFalse(contains2);
        }
示例#15
0
        public void Contains_AddedElement_ShouldReturnTrue()
        {
            var numbers = TestUtils.ToIntArrayUnique("-2 3 10 0 1 -16");
            var tree    = new AvlTree <int>();

            foreach (int number in numbers)
            {
                tree.Add(number);
            }

            var contains3 = tree.Contains(3);

            Assert.IsTrue(contains3);
        }
示例#16
0
        private void TestInsertion(int numberOfItems)
        {
            Console.WriteLine("Starting insertion...");
            var random = new Random();

            for (var i = 0; i < numberOfItems; i++)
            {
                var number = random.Next();
                if (number == 0 || AvlTree.Contains(number))
                {
                    continue;
                }
                AvlTree.Insert(number);
                HelpList.Add(number);
            }

            Console.WriteLine("Insertion finished");
        }
        public int TestAvlTree()
        {
            var count = 0;

            for (int i = 0; i < _data.Length; i++)
            {
                var number = _data[i];
                if (_tree.Contains(number))
                {
                    ++count;
                }
            }
            if (count != 0)
            {
                throw new Exception("Search error in AvlTree");
            }
            return(count);
        }
示例#18
0
        private bool RandomRemoveInsertTest(int rounds)
        {
            Console.WriteLine("Starting random test...");
            var randomSeed         = new Random();
            var randomCount        = new Random(randomSeed.Next());
            var randomInsOrDel     = new Random(randomSeed.Next());
            var randomInsertNumber = new Random(randomSeed.Next());
            var randomDeleteNumber = new Random(randomSeed.Next());
            //zistit kolko krat ideme robit mazanie/vkladanie
            var help = 0;

            while (help < rounds)
            {
                var count    = randomCount.Next(100, 500);
                var insOrDel = randomInsOrDel.NextDouble(); //0 insert, 1 delete
                for (var i = 0; i < count; i++)
                {
                    if (AvlTree.IsEmpty)
                    {
                        insOrDel = 0.2; //ak je prazdny nech radsej insertuje
                    }
                    if (insOrDel < 0.5)
                    {
                        var number = randomInsertNumber.Next();
                        if (number == 0 || AvlTree.Contains(number))
                        {
                            continue;
                        }
                        AvlTree.Insert(number);
                        HelpList.Add(number);
                    }
                    else
                    {
                        var index = randomDeleteNumber.Next(0, HelpList.Count - 1);
                        var value = HelpList[index];
                        HelpList.Remove(value);
                        if (!AvlTree.Contains(value))
                        {
                            Console.WriteLine($"Tree does not contain that value {value}");
                            return(false);
                        }
                        var deletedValue = AvlTree.Delete(value);
                        if (value != deletedValue)
                        {
                            Console.WriteLine($"Wrong data deleted... should: {value}, was: {deletedValue}");
                            return(false);
                        }

                        if (AvlTree.Contains(value))
                        {
                            Console.WriteLine($"Tree still contains removed value {value}");
                            return(false);
                        }
                    }
                }
                if (!CountTest(false))
                {
                    Console.WriteLine("Count test failed during random test");
                    return(false);
                }

                if (!InOrderTest(false))
                {
                    Console.WriteLine("In order test failed during random test");
                    return(false);
                }

                help++;
            }


            if (!CountTest(true))
            {
                Console.WriteLine("Count test failed right after random test");
                return(false);
            }
            if (!InOrderTest(true))
            {
                Console.WriteLine("In order test failed right after random test");
                return(false);
            }

            Console.WriteLine("Random test successful");
            return(true);
        }
示例#19
0
        public void AvlTreeBalanceTest()
        {
            // test case 1: ++, +

            AvlTree <Int32, String> avl = new AvlTree <Int32, String>();

            avl.Insert(1, "1");
            avl.Insert(2, "2");
            avl.Insert(3, "3");

            Assert.IsTrue(avl.Select(element => element.Key).SequenceEqual(Enumerable.Range(1, 3)));
            Assert.AreEqual(1, avl.Height);


            // test case 2: --, -

            avl = new AvlTree <Int32, String>();
            avl.Insert(3, "1");
            avl.Insert(2, "2");
            avl.Insert(1, "3");

            Assert.IsTrue(avl.Select(element => element.Key).SequenceEqual(Enumerable.Range(1, 3)));
            Assert.AreEqual(1, avl.Height);

            // test case 3: ++, -

            avl = new AvlTree <Int32, String>();
            avl.Insert(4, "1");
            avl.Insert(2, "2");
            avl.Insert(9, "3");
            avl.Insert(6, "4");
            avl.Insert(3, "5");
            avl.Insert(10, "6");
            avl.Insert(8, "7");
            avl.Insert(11, "8");
            avl.Insert(1, "9");
            avl.Insert(5, "10");
            avl.Insert(7, "11");

            Assert.IsTrue(avl.Select(element => element.Key).SequenceEqual(Enumerable.Range(1, 11)));
            Assert.AreEqual(3, avl.Height);


            // test case 3: --, +

            avl = new AvlTree <Int32, String>();
            avl.Insert(8, "1");
            avl.Insert(10, "2");
            avl.Insert(3, "3");
            avl.Insert(5, "4");
            avl.Insert(11, "5");
            avl.Insert(9, "6");
            avl.Insert(1, "7");
            avl.Insert(4, "8");
            avl.Insert(2, "9");
            avl.Insert(6, "10");
            avl.Insert(7, "11");

            Assert.IsTrue(avl.Select(element => element.Key).SequenceEqual(Enumerable.Range(1, 11)));
            Assert.AreEqual(3, avl.Height);


            // test case 4: ++, =

            avl = new AvlTree <Int32, String>();
            avl.Insert(4, "1");
            avl.Insert(2, "2");
            avl.Insert(5, "3");
            avl.Insert(1, "4");
            avl.Insert(3, "5");
            avl.Remove(5);

            Assert.IsTrue(avl.Select(element => element.Key).SequenceEqual(Enumerable.Range(1, 4)));
            Assert.AreEqual(2, avl.Height);


            // test case 5: --, =

            avl = new AvlTree <Int32, String>();
            avl.Insert(2, "1");
            avl.Insert(1, "2");
            avl.Insert(4, "3");
            avl.Insert(5, "4");
            avl.Insert(3, "5");
            avl.Remove(1);

            Assert.IsTrue(avl.Select(element => element.Key).SequenceEqual(Enumerable.Range(2, 4)));
            Assert.AreEqual(2, avl.Height);


            // test case 7: random tests

            avl = new AvlTree <Int32, String>();

            Random random = new Random();

            for (Int32 i = 0; i < 10000; i++)
            {
                Int32 key = random.Next(1, 100000);
                if (!avl.Contains(key))
                {
                    avl.Insert(key, i.ToString());
                }
            }
            Int32 prevKey = 0;
            Int32 count   = 0;

            foreach (KeyValuePair <Int32, String> element in avl)
            {
                Assert.IsTrue(element.Key > prevKey);
                prevKey = element.Key;
                count++;
            }

            for (Int32 i = 0; i < 50000; i++)
            {
                Int32 key = random.Next(1, 100000);
                if (avl.Contains(key))
                {
                    avl.Remove(key);
                }
            }

            prevKey = 0;
            foreach (KeyValuePair <Int32, String> element in avl)
            {
                Assert.IsTrue(element.Key > prevKey);
                prevKey = element.Key;
            }
        }
示例#20
0
 public void AddFirstElement()
 {
     _tree.Insert(1);
     Assert.IsTrue(_tree.Contains(1));
     Assert.AreEqual(1, _tree.Count);
 }
示例#21
0
        public void AvlTreeEnumeratorTest()
        {
            // enumerator

            AvlTree <Int32, String> tree = new AvlTree <Int32, String>();

            foreach (KeyValuePair <Int32, String> keyValuePair in this.values)
            {
                if (!tree.Contains(keyValuePair.Key))
                {
                    tree.Insert(keyValuePair.Key, keyValuePair.Value);
                }
            }

            Int32 prevKey = this.values.Min(keyValuePair => keyValuePair.Key) - 1;
            Int32 count   = 0;

            foreach (KeyValuePair <Int32, String> element in tree)
            {
                element.Key.ShouldBeGreaterThan(prevKey);
                prevKey = element.Key;
                count++;
            }

            count.ShouldBe(tree.Count);

            // search tree enumerator

            tree = new AvlTree <Int32, String>();

            foreach (KeyValuePair <Int32, String> keyValuePair in this.values)
            {
                if (!tree.Contains(keyValuePair.Key))
                {
                    tree.Insert(keyValuePair.Key, keyValuePair.Value);
                }
            }

            ISearchTreeEnumerator <Int32, String> enumerator = tree.GetTreeEnumerator();

            List <Int32> forwardList  = new List <Int32>();
            List <Int32> backwardList = new List <Int32>();

            if (enumerator.MoveMin())
            {
                do
                {
                    forwardList.Add(enumerator.Current.Key);
                }while (enumerator.MoveNext());
            }

            if (enumerator.MoveMax())
            {
                do
                {
                    backwardList.Add(enumerator.Current.Key);
                }while (enumerator.MovePrev());
            }

            backwardList.Reverse();
            forwardList.ShouldBe(backwardList);
        }
        public void Contains_AddedElement_ShouldReturnTrue()
        {
            var numbers = TestUtils.ToIntArrayUnique("-2 3 10 0 1 -16");
            var tree = new AvlTree<int>();
            foreach (int number in numbers)
            {
                tree.Add(number);
            }

            var contains3 = tree.Contains(3);
            Assert.IsTrue(contains3);
        }
示例#23
0
        public void Test2()
        {
            var s = new SortedDictionary <int, float>();

            const int cnt   = 10000000;
            var       raw   = Enumerable.Range(1, cnt).ToArray();
            var       datas = raw.ToArray();
            var       rand  = new Random();

            for (var i = datas.Length - 1; i > 0; i--)
            {
                var value       = datas[i];
                var randomIndex = rand.Next(0, i);
                datas[i]           = datas[randomIndex];
                datas[randomIndex] = value;
            }

            var tree = new AvlTree <int>();

            foreach (var i in datas)
            {
                tree.Add(i);
            }

            Assert.True(tree.Min == 1);
            Assert.True(tree.Max == cnt);
            Assert.True(tree.Count == raw.Length);

            var ind = 0;

            foreach (var i in tree)
            {
                Assert.True(tree.Contains(i));
                Assert.True(raw[ind] == i);
                ind++;
            }

            Assert.False(tree.Contains(0));
            Assert.False(tree.Contains(int.MinValue));

            foreach (var i in tree)
            {
                var node = tree.FindNode(i);
                Assert.True(node.HasValue);
                var prev = tree.PreviousNode(node);
                var next = tree.NextNode(node);
                if (i == 1)
                {
                    Assert.False(prev.HasValue);
                }
                else
                {
                    Assert.True(prev.HasValue);
                }

                if (i == cnt)
                {
                    Assert.False(next.HasValue);
                }
                else
                {
                    Assert.True(next.HasValue);
                }

                var nearNext = tree.NearestNext(i, int.MaxValue);
                var nearPrev = tree.NearestPrevious(i, int.MinValue);
                if (i == cnt)
                {
                    Assert.True(nearNext == int.MaxValue);
                }
                else
                {
                    Assert.True(nearNext == i + 1);
                }

                if (i == 1)
                {
                    Assert.True(nearPrev == int.MinValue);
                }
                else
                {
                    Assert.True(nearPrev == i - 1);
                }
            }

            Assert.True(tree.MaxHeight == tree.RecordHeight);
            Console.WriteLine(tree.MaxHeight);
            Console.WriteLine(Log2(cnt));

            tree.CheckHeight();
        }
示例#24
0
        public void TestRemove()
        {
            var tree = new AvlTree<TestNode> ();
            var t1 = new TestNode (1);
            var t2 = new TestNode (2);
            var t3 = new TestNode (3);

            tree.Add (t2);
            tree.Add (t1);
            tree.Add (t3);
            Assert.AreEqual (3, tree.Count);

            Assert.IsTrue (tree.Remove (t2));

            Assert.AreEqual (2, tree.Count);
            Assert.IsTrue (tree.Contains (t1));
            Assert.IsFalse (tree.Contains (t2));
            Assert.IsTrue (tree.Contains (t3));
        }
示例#25
0
        public void TestReverseOrderRemoval()
        {
            var tree = new AvlTree<TestNode> ();
            TestNode[] nodes = new TestNode[10];
            for (int i = 0; i < 10; i++) {
                tree.Add (nodes [i] = new TestNode (i));
            }
            Assert.AreEqual (10, tree.Count);

            for (int i = 0; i < 10; i++) {
                Assert.IsTrue (tree.Contains (nodes[9 - i]), "case : " + (9 - i));
                tree.Remove (nodes[9 - i]);
                Assert.IsFalse (tree.Contains (nodes[9 - i]), "case : " + (9 - i));
            }
            Assert.AreEqual (0, tree.Count);
        }
示例#26
0
        public static void TestAvlTree()
        {
            AvlTree <int, int> avl = new AvlTree <int, int>();

            avl.Add(10, 10);
            avl.Add(9, 9);
            avl.Add(8, 8);
            avl.Add(7, 7);
            avl.Add(6, 6);
            avl.Add(5, 5);
            avl.Add(4, 4);
            avl.Add(3, 3);
            avl.Add(2, 2);
            avl.Add(1, 1);
            //avl.AddByRecursion(10, 10);
            //avl.AddByRecursion(9, 9);
            //avl.AddByRecursion(8, 8);
            //avl.AddByRecursion(7, 7);
            //avl.AddByRecursion(6, 6);
            //avl.AddByRecursion(5, 5);
            //avl.AddByRecursion(4, 4);
            //avl.AddByRecursion(3, 3);
            //avl.AddByRecursion(2, 2);
            //avl.AddByRecursion(1, 1);
            Console.WriteLine("avl size:" + avl.Size);
            Console.WriteLine("avl IsEmpty:" + avl.IsEmpty);
            Console.WriteLine("avl IsBalanceBinaryTree:" + avl.IsBalanceBinaryTree());
            Console.WriteLine("avl IsBinarySearchTree:" + avl.IsBinarySearchTree());
            Console.WriteLine("avl Contains 4:" + avl.Contains(4));
            Console.WriteLine("avl ContainsByRecursion 4:" + avl.ContainsByRecursion(4));
            avl.PreOrderTraversal();
            Console.WriteLine(avl.PreOrder.ToList().Translate());
            avl.PreOrderTraversalByRecursion();
            Console.WriteLine(avl.PreOrder.ToList().Translate());

            avl.SequentialTraversal();
            Console.WriteLine(avl.SequentialOrder.ToList().Translate());
            avl.SequentialTraversalByRecursion();
            Console.WriteLine(avl.SequentialOrder.ToList().Translate());

            avl.PostOrderTraversal();
            Console.WriteLine(avl.PostOrder.ToList().Translate());
            avl.PostOrderTraversalByRecursion();
            Console.WriteLine(avl.PostOrder.ToList().Translate());

            avl.LevelOrderTraversal();
            Console.WriteLine(avl.LevelOrder.ToList().Translate());
            avl.LevelOrderTraversalByRecursion();
            Console.WriteLine(avl.LevelOrder.ToList().Translate());
            //avl.Remove(4);
            //avl.Remove(6);
            //avl.Remove(7);
            //avl.Remove(9);
            //avl.RemoveByRecursion(4);
            //avl.RemoveByRecursion(6);
            //avl.RemoveByRecursion(7);
            //avl.RemoveByRecursion(9);
            avl.RemoveMax();
            avl.RemoveMin();
            avl.RemoveMaxByRecursion();
            avl.RemoveMinByRecursion();
            Console.WriteLine("avl size:" + avl.Size);
            Console.WriteLine("avl IsEmpty:" + avl.IsEmpty);
            Console.WriteLine("avl IsBalanceBinaryTree:" + avl.IsBalanceBinaryTree());
            Console.WriteLine("avl IsBinarySearchTree:" + avl.IsBinarySearchTree());
            Console.WriteLine("avl Contains 4:" + avl.Contains(4));
            Console.WriteLine("avl ContainsByRecursion 4:" + avl.ContainsByRecursion(4));
        }
        public void Contains_NonAddedElement_ShouldReturnFalse()
        {
            var numbers = TestUtils.ToIntArrayUnique("1 7 3 -4 10 0");
            var tree = new AvlTree<int>();
            foreach (int number in numbers)
            {
                tree.Add(number);
            }

            var contains2 = tree.Contains(2);
            Assert.IsFalse(contains2);
        }
示例#28
0
        public void BinarySearchTreeEnumeratorTest()
        {
            // test case 1

            AvlTree <Int32, String> avl = new AvlTree <Int32, String>();

            Random random = new Random();

            for (Int32 i = 0; i < 1000; i++)
            {
                Int32 key = random.Next(1, 1000);
                if (!avl.Contains(key))
                {
                    avl.Insert(key, i.ToString());
                }
            }
            Int32 prevKey = 0;
            Int32 count   = 0;

            foreach (KeyValuePair <Int32, String> element in avl)
            {
                Assert.IsTrue(element.Key > prevKey);
                prevKey = element.Key;
                count++;
            }
            Assert.AreEqual(avl.Count, count);


            // test case 2

            avl = new AvlTree <Int32, String>();

            random = new Random();
            for (Int32 i = 0; i < 1000; i++)
            {
                Int32 key = random.Next(1, 1000);
                if (!avl.Contains(key))
                {
                    avl.Insert(key, i.ToString());
                }
            }

            ISearchTreeEnumerator <Int32, String> enumerator = avl.GetTreeEnumerator();

            List <Int32> forwardList  = new List <Int32>();
            List <Int32> backwardList = new List <Int32>();

            if (enumerator.MoveMin())
            {
                do
                {
                    forwardList.Add(enumerator.Current.Key);
                } while (enumerator.MoveNext());
            }

            if (enumerator.MoveMax())
            {
                do
                {
                    backwardList.Add(enumerator.Current.Key);
                } while (enumerator.MovePrev());
            }

            backwardList.Reverse();
            Assert.IsTrue(forwardList.SequenceEqual(backwardList));
        }