Exemplo n.º 1
0
        public void RandomAddRemoveTest()
        {
            const int NUMBERS_TO_ADD = 200;

            MyAVLTREE <int, string> myAVLTREE = new MyAVLTREE <int, string>();
            List <int> addedValues            = new List <int>();
            Random     random = new Random(System.DateTime.Now.Millisecond.GetHashCode());

            for (int i = 0; i < NUMBERS_TO_ADD; i++)
            {
                int randomNumber = random.Next(100);
                myAVLTREE.Add(randomNumber, "");
                addedValues.Add(randomNumber);

                Assert.That(myAVLTREE.Height(), Is.LessThan(MaxHeight(myAVLTREE) + 1), $"Add from: {string.Join(",", addedValues)}");
            }

            var        allAdded       = addedValues.ToArray();
            List <int> removedIndeces = new List <int>();

            while (addedValues.Count > 0)
            {
                int rnd = random.Next(addedValues.Count());
                removedIndeces.Add(rnd);
                int toRemove = addedValues[rnd];
                addedValues.RemoveAt(rnd);

                myAVLTREE.Remove(toRemove);

                Assert.That(myAVLTREE.Height(), Is.LessThan(MaxHeight(myAVLTREE) + 1), $"Removed indices from {string.Join(",", addedValues)}: {string.Join(",", removedIndeces)}");
            }
        }
Exemplo n.º 2
0
        public void TestRotateRightLeft()
        {
            MyAVLTREE <int, string> myAVLTREE = new MyAVLTREE <int, string>();

            myAVLTREE.Add(3, "");
            myAVLTREE.Add(5, "");
            myAVLTREE.Add(4, "");

            Assert.That(myAVLTREE.Root.Key, Is.EqualTo(4));
            Assert.That(myAVLTREE.Root.Left.Key, Is.EqualTo(3));
            Assert.That(myAVLTREE.Root.Right.Key, Is.EqualTo(5));
            Assert.That(Math.Log(myAVLTREE.Count() + 1, 2) <= myAVLTREE.Height(), Is.EqualTo(true));
        }