Exemplo n.º 1
0
      public void BasicAVLTreeTest()
      {
         var tree = new AVLTree<int>();
         Assert.AreEqual(0, tree.Height);

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

         Assert.AreEqual(4, tree.Height);
         Assert.AreEqual(10, tree.Size);

         for (int i = 0; i < 10; i++)
         {
            Assert.IsTrue(tree.Contains(i));
         }

         Assert.IsFalse(tree.Contains(11));

         for (int i = 0; i < 10; i++)
         {
            Assert.IsTrue(tree.Remove(i));
         }

         Assert.AreEqual(0, tree.Height);
         Assert.AreEqual(0, tree.Size);
      }
Exemplo n.º 2
0
        public void AVLContains()
        {
            AVLTree tree = AVLcreateTree();

            Assert.AreEqual(true, tree.Contains(3));
            Assert.AreEqual(true, tree.Contains(15));
            Assert.AreEqual(true, tree.Contains(10));
            Assert.AreNotEqual(true, tree.Contains(22));
        }
Exemplo n.º 3
0
        public void TestContains()
        {
            var testTree = new AVLTree <int>();
            var hasOne   = testTree.Contains(1);

            Assert.AreEqual(false, hasOne);

            testTree.Insert(1);
            var hasOneAfterInsert = testTree.Contains(1);

            Assert.AreEqual(true, hasOneAfterInsert);
        }
Exemplo n.º 4
0
        public void RemoveTest()
        {
            tree.AddRange(listStu.ToArray());
            Assert.IsTrue(tree.ToList().Count <= listStu.Count);
            Student stu  = null;
            var     list = new List <Student>(tree.ToList());

            foreach (var item in list)
            {
                var check         = tree.Remove(item);
                var checkContains = tree.Contains(new Node <Student>(item));
                Assert.IsFalse(checkContains);
            }
        }
        public void Contains_ExistingElement_ShouldReturnTrue()
        {
            // Arrange
            AVLTree <int> avl = new AVLTree <int>();

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

            // Act
            // Assert
            Assert.IsTrue(avl.Contains(1));
            Assert.IsTrue(avl.Contains(2));
            Assert.IsTrue(avl.Contains(3));
        }
Exemplo n.º 6
0
        public void DeleteTest_WhenLeftTreeIsNull_And_GivenNodeIsRightChildOfParent()
        {
            // Arrange
            var tree = new AVLTree <decimal>();

            tree.Insert(4m, false);       //         r(4)
            tree.Insert(7m, false);       //   l(null)    r(7)
            tree.Insert(8m, false);       //        l(null)   r(8)

            Assert.Equal(3, tree.Count);
            Assert.Equal(2, tree.Height());

            // Act
            tree.Delete(7m, false);

            // Assert
            tree.RecomputeHeight(tree.Root);
            Assert.Equal(2, tree.Count);
            Assert.Equal(1, tree.Height());
            Assert.False(tree.Contains(7m));
            Assert.Equal(4m, tree.Root.Value);
            Assert.Null(tree.Root.Left);
            Assert.Equal(8m, tree.Root.Right.Value);
            Assert.Equal(4m, tree.Root.Right.Parent.Value);
        }
Exemplo n.º 7
0
        public void DeleteTestWhenLeftTreeIsNullAndGivenNodeIsRoot()
        {
            // Arrange
            var tree = new AVLTree <decimal>();

            tree.Insert(4m, false);
            tree.Insert(5m, false);
            tree.Insert(6m, false);

            Assert.Equal(3, tree.Count);
            Assert.Equal(2, tree.Height());

            // Act
            tree.Delete(4m, false);

            // Assert
            tree.RecomputeHeight(tree.Root);
            Assert.Equal(2, tree.Count);
            Assert.Equal(1, tree.Height());
            Assert.False(tree.Contains(4m));
            Assert.Equal(5m, tree.Root.Value);
            Assert.Null(tree.Root.Left);
            Assert.Equal(6m, tree.Root.Right.Value);
            Assert.Equal(5m, tree.Root.Right.Parent.Value);
        }
Exemplo n.º 8
0
        public void DeleteTest_WhenRightTreeIsNull_And_GivenNodeIsLeftChildOfParent()
        {
            // Arrange
            var tree = new AVLTree <decimal>();

            tree.Insert(5m, false);      //       5
            tree.Insert(4m, false);      //     4   n
            tree.Insert(3m, false);      //   3   n
            tree.Insert(3.5m, false);    // n  3.5

            Assert.Equal(4, tree.Count);

            // Act
            tree.Delete(4m, false);

            // Assert
            Assert.Equal(3, tree.Count);
            Assert.False(tree.Contains(4m));
            Assert.Equal(5m, tree.Root.Value);
            Assert.Null(tree.Root.Right);
            Assert.Equal(3m, tree.Root.Left.Value);
            Assert.Equal(5m, tree.Root.Left.Parent.Value);
            Assert.Equal(3.5m, tree.Root.Left.Right.Value);
            Assert.Equal(3m, tree.Root.Left.Right.Parent.Value);
        }
Exemplo n.º 9
0
        public void RemoveAllExceptOne()
        {
            var tree = new AVLTree <int>();

            int elementsCount = 100000;

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.Contains(el))
                    {
                        tree.Add(el);
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            for (int i = 0; i < elementsCount - 1; i++)
            {
                if (!tree.Remove(i))
                {
                    removedEverything = false;
                }
            }

            Assert.IsTrue(tree.Count == 1 &&
                          removedEverything &&
                          tree.Root.Value == elementsCount - 1);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Check contains specified user.
 /// </summary>
 /// <param name="id">user id</param>
 /// <returns>if contains specified user, return true.</returns>
 public bool Contains(long id)
 {
     lock (_collectionLock)
     {
         return(_collection.Contains(id));
     }
 }
        public void Remove()
        {
            AVLTree <int> tree1 = new AVLTree <int>();

            tree1.Add(99);
            tree1.Add(23);
            tree1.Add(45);
            tree1.Add(99);
            tree1.Add(37);
            tree1.Add(16);
            tree1.Add(22);
            tree1.Add(3);
            Assert.AreEqual(tree1.Contains(45), true);
            tree1.remove(45);
            Assert.AreEqual(tree1.Contains(45), false);
        }
        public static void RunSearchingSpeedTest()
        {
            const int NUMBER_OF_ELEMENTS_IN_TREE1 = 1000;
            const int NUMBER_OF_ELEMENTS_IN_TREE2 = 10000;
            const int NUMBER_OF_ELEMENTS_IN_TREE3 = 1000000;
            const int TIMES_SEARCHED = 100000;
            AVLTree<int> tree1 = new AVLTree<int>();
            AVLTree<int> tree2 = new AVLTree<int>();
            AVLTree<int> tree3 = new AVLTree<int>();
            for (int i = 0; i < NUMBER_OF_ELEMENTS_IN_TREE3; i++)
            {
                if (i < NUMBER_OF_ELEMENTS_IN_TREE1)
                {
                    tree1.Insert(i);
                }

                if (i < NUMBER_OF_ELEMENTS_IN_TREE2)
                {
                    tree2.Insert(i);
                }

                tree3.Insert(i);
            }

            Console.WriteLine("Searching in tree1...");
            var watch1 = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i < TIMES_SEARCHED; i++)
            {
                tree1.Contains(GetRandomNumber(NUMBER_OF_ELEMENTS_IN_TREE1));
            }
            
            watch1.Stop();
            Console.WriteLine("Elapsed time: {0}ms\n", watch1.ElapsedMilliseconds);

            Console.WriteLine("Searching in tree2...");
            var watch2 = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i < TIMES_SEARCHED; i++)
            {
                tree1.Contains(GetRandomNumber(NUMBER_OF_ELEMENTS_IN_TREE2));
            }

            watch2.Stop();
            Console.WriteLine("Elapsed time: {0}ms\n", watch2.ElapsedMilliseconds);

            Console.WriteLine("Searching in tree3...");
            var watch3 = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i < TIMES_SEARCHED; i++)
            {
                tree1.Contains(GetRandomNumber(NUMBER_OF_ELEMENTS_IN_TREE3));
            }

            watch3.Stop();
            Console.WriteLine("Elapsed time: {0}ms\n", watch3.ElapsedMilliseconds);

            int magicNumberOutside = 1230;
            int magicNumberInside = 900;
            Console.WriteLine("Tree1 contains {0}: {1}\n", magicNumberOutside, tree1.Contains(magicNumberOutside)); // expected output - false
            Console.WriteLine("Tree1 contains {0}: {1}\n", magicNumberInside, tree1.Contains(magicNumberInside));   // expected output - true
        }
Exemplo n.º 13
0
 public static bool IsOfficialMuted(long userId)
 {
     CheckUpdateOfficialMutes();
     lock (_officialMuteUserIds)
     {
         return(_officialMuteUserIds.Contains(userId));
     }
 }
Exemplo n.º 14
0
 public static bool IsBlocked(long userId)
 {
     CheckUpdateBlocks();
     lock (_blockingUserIds)
     {
         return(_blockingUserIds.Contains(userId));
     }
 }
Exemplo n.º 15
0
 public static bool IsNoRetweet(long userId)
 {
     CheckUpdateNoRetweets();
     lock (_noRetweetUserIds)
     {
         return(_noRetweetUserIds.Contains(userId));
     }
 }
        public void Insert()
        {
            AVLTree <int> tree1 = new AVLTree <int>();

            tree1.Add(99);
            tree1.Add(23);
            tree1.Add(45);
            tree1.Add(99);
            tree1.Add(37);
            tree1.Add(16);
            tree1.Add(22);
            tree1.Add(3);
            Assert.AreEqual(tree1.Contains(99), true);
            Assert.AreEqual(tree1.Contains(16), true);
            Assert.AreEqual(tree1.Contains(37), true);
            Assert.AreEqual(tree1.Contains(22), true);
            Assert.AreEqual(tree1.Contains(3), true);
        }
Exemplo n.º 17
0
 public override Func <TwitterStatus, bool> GetEvaluator()
 {
     // accept all status via web.
     return(status =>
     {
         lock (_acceptIds)
         {
             return _acceptIds.Contains(status.Id);
         }
     });
 }
Exemplo n.º 18
0
 private bool CheckConversation(TwitterStatus status)
 {
     lock (_statuses)
     {
         if (_statuses.Contains(status.Id))
         {
             return(true);
         }
         if (status.InReplyToStatusId == null)
         {
             return(false);
         }
         if (_statuses.Contains(status.InReplyToStatusId.Value))
         {
             _statuses.Add(status.Id);
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 19
0
        public void ExistedKeyFound()
        {
            var tree = new AVLTree <int, int>();
            var n    = 10;

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

            Assert.AreEqual(tree.Contains(5), true);
        }
        public void AddTest()
        {
            AVLTree <int> AVL = new AVLTree <int>();

            Assert.AreEqual(0, AVL.Size());

            AVL.Insert(5);
            AVL.Insert(3);
            AVL.Insert(2);
            AVL.Insert(6);
            AVL.Insert(1);
            AVL.Insert(4);

            Assert.AreEqual(true, AVL.Contains(6));
            Assert.AreEqual(true, AVL.Contains(3));
            Assert.AreEqual(false, AVL.Contains(23));
            Assert.AreEqual(true, AVL.ValidateBSTInvariant());

            AVL.Insert(-6);
            Assert.AreEqual(true, AVL.ValidateBSTInvariant());
        }
Exemplo n.º 21
0
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Add
        ///</summary>
        public void AddTestHelper <T>()
        {
            AVLTree <double> target = new AVLTree <double>();
            double           data   = 3;

            target.Add(1);
            target.Add(2);
            target.Add(data);
            SearchResult actual = target.Contains(data);

            Assert.IsNotNull(actual.SearchPath);
        }
Exemplo n.º 22
0
        public void NotExistedKeyNotFound()
        {
            var tree = new AVLTree <int, int>();
            var n    = 10;

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

            Assert.AreEqual(tree.Contains(9000), false);
        }
Exemplo n.º 23
0
        public void SortedElementsAfterAddingAndRemoving()
        {
            var tree = new AVLTree <int>();

            int elementsCount = 100000;

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.Contains(el))
                    {
                        tree.Add(el);
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            //Removing every second number
            for (int i = 0; i < elementsCount; i += 2)
            {
                if (!tree.Remove(i))
                {
                    removedEverything = false;
                }
            }


            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item)
                {
                    elementsAreSorted = false;
                }
                last = item;
                count++;
            }

            Trace.WriteLine(tree.Count);

            Assert.IsTrue(tree.Count == count &&
                          elementsAreSorted &&
                          removedEverything);
        }
Exemplo n.º 24
0
        public void RemoveRootEveryTimeUntilTreeElementsAreHalfed()
        {
            var tree = new AVLTree <int>();

            int elementsCount = 100000;

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.Contains(el))
                    {
                        tree.Add(el);
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            while (tree.Count > elementsCount / 2)
            {
                if (!tree.Remove(tree.Root.Value))
                {
                    removedEverything = false;
                }
            }

            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item)
                {
                    elementsAreSorted = false;
                }
                last = item;
                count++;
            }

            Trace.WriteLine(tree.Count);

            Assert.IsTrue(tree.Count == count &&
                          elementsAreSorted &&
                          count == elementsCount / 2 &&
                          removedEverything);
        }
Exemplo n.º 25
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);
        }
Exemplo n.º 26
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);
        }
        public void Performance_Insert_Contains()
        {
            var avl = new AVLTree <int>();

            for (int i = 0; i < 100000; i++)
            {
                avl.Insert(i);
            }

            for (int i = 0; i < 100000; i++)
            {
                Assert.IsTrue(avl.Contains(i));
            }
        }
Exemplo n.º 28
0
        public void TestRemoveRoot()
        {
            var testTree        = new AVLTree <int>();
            var initialSize     = testTree.Size;
            var initialContains = testTree.Contains(4);

            Assert.AreEqual(0, initialSize);
            Assert.AreEqual(false, initialContains);

            testTree.Insert(4);
            var oneSize      = testTree.Size;
            var fourContains = testTree.Contains(4);

            Assert.AreEqual(1, oneSize);
            Assert.AreEqual(true, fourContains);

            testTree.Remove(4);
            var twoSize = testTree.Size;
            var fourContainsAfterRemove = testTree.Contains(4);

            Assert.AreEqual(0, twoSize);
            Assert.AreEqual(false, fourContainsAfterRemove);
        }
Exemplo n.º 29
0
        public void Add_And_Remove_1k_Unique_Items()
        {
            AVLTree <int> tree  = new AVLTree <int>();
            List <int>    items = new List <int>();

            // add random unique items to the tree
            Random rng = new Random();

            while (items.Count < 1000)
            {
                int next = rng.Next();
                if (!items.Contains(next))
                {
                    items.Add(next);
                    tree.Add(next);

                    Assert.AreEqual(items.Count, tree.Count, "items and tree collection should have the same count");
                }
            }

            // make sure they all exist in the tree
            foreach (int value in items)
            {
                Assert.IsTrue(tree.Contains(value), "The tree does not contain the expected value " + value.ToString());
            }

            // remove the item from the tree and make sure it's gone
            foreach (int value in items)
            {
                Assert.IsTrue(tree.Remove(value), "The tree does not contain the expected value " + value.ToString());
                Assert.IsFalse(tree.Contains(value), "The tree should not have contained the value " + value.ToString());
                Assert.IsFalse(tree.Remove(value), "The tree should not have contained the value " + value.ToString());
            }

            // now make sure the tree is empty
            Assert.AreEqual(0, tree.Count, "The tree should be empty");
        }
Exemplo n.º 30
0
        public void InsertAndContainsTest()
        {
            var treeString = new AVLTree <string>();
            var newTree    = new AVLTree <int>(new Node <int>(1, new Node <int>(1), new Node <int>(0)));
            var newTree2   = new AVLTree <int>(new Node <int>(1, new Node <int>(2), new Node <int>(0)));

            treeString.Insert(new Node <string>("S"));
            var item1C = tree.Contains(item1);
            var item2C = tree.Contains(item3);
            var strC   = treeString.Contains(new Node <string>("S"));

            var falseCon  = tree.Contains(new Node <int>());
            var falseNull = tree.Contains(null);

            Assert.IsTrue(strC);
            Assert.IsTrue(item1C);
            Assert.IsTrue(item2C);
            Assert.IsFalse(falseCon);
            Assert.IsFalse(falseNull);
        }
Exemplo n.º 31
0
        public void Enumerator_Of_Single()
        {
            var tree = new AVLTree <int>();

            foreach (var item in tree)
            {
                Assert.IsTrue(false, "An empty tree should not enumerate any values");
            }

            Assert.IsFalse(tree.Contains(10), "Tree should not contain 10 yet");

            tree.Add(10);

            Assert.IsTrue(tree.Contains(10), "Tree should contain 10");

            var count = 0;

            foreach (var item in tree)
            {
                count++;
                Assert.AreEqual(1, count);
                Assert.AreEqual(10, item);
            }
        }
        public void Contains_NonExistingElement_ShouldReturnFalse()
        {
            // Arrange
            AVLTree <int> avl = new AVLTree <int>();

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

            // Act
            bool contains = avl.Contains(5);

            // Assert
            Assert.IsFalse(contains);
        }
Exemplo n.º 33
0
        public void SingleElementsAddsUpToTree()
        {
            AVLTree<int> tree = new AVLTree<int>();

            foreach (int item in tree)
            {
                Assert.Fail("An empty tree should not enumerate any values");
            }

            Assert.IsFalse(tree.Contains(10), "Tree should not contain 10 yet");

            tree.Add(10);

            Assert.IsTrue(tree.Contains(10), "Tree should contain 10");

            int count = 0;
            foreach (int item in tree)
            {
                count++;
                Assert.AreEqual(1, count, "There should be exactly one item");
                Assert.AreEqual(item, 10, "The item value should be 10");
            }
        }