예제 #1
0
        private string SearchTimeTest()
        {
            var element = new Random().Next(1, count - 1);
            var isFound = 0;

            sw      = Stopwatch.StartNew();
            isFound = tree.Find(element);
            return(isFound == 0 ? "failer in " : "passed in " + $"{sw.ElapsedMilliseconds} ms");
        }
예제 #2
0
        public void TestFindMissing()
        {
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(5);
            tree.Insert(4);
            tree.Insert(6);
            int result;

            if (tree.Find(1, out result) || result != 0)
            {
                Assert.Fail();
            }
        }
예제 #3
0
        public void TestFindRoot()
        {
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(5);
            tree.Insert(4);
            tree.Insert(6);
            int result;

            if (!tree.Find(5, out result) || result != 5)
            {
                Assert.Fail();
            }
        }
예제 #4
0
 public void Add(TKey key, TValue value)
 {
     try
     {
         var tmp = array.Find(new Node()
         {
             Key = key, Value = value
         });
         if (tmp == null)
         {
             array.Insert(new Node {
                 Key = key, Value = value
             });
         }
         else
         {
             throw new ArgumentException();
         }
     }
     catch (ArgumentException ex)
     {
         Console.WriteLine("Exception: Key " + key + " is already exist: Add(" + key + "," + value + ")");
     }
 }
예제 #5
0
        public void TestSimpleRemoveLeft()
        {
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(5);
            tree.Insert(4);
            tree.Insert(6);
            tree.Remove(4);
            int result = 0;

            if (tree.Find(4, out result))
            {
                Assert.Fail();
            }
            VerifyTree(tree);
        }
예제 #6
0
        public void TestFindGreaterThan()
        {
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(5);
            tree.Insert(3);
            tree.Insert(6);
            tree.Insert(4);
            tree.Insert(1);
            int result;

            if (!tree.Find(4, out result) || result != 4)
            {
                Assert.Fail();
            }
        }
예제 #7
0
        public void TestFindLessThan()
        {
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(5);
            tree.Insert(4);
            tree.Insert(6);
            tree.Insert(3);
            tree.Insert(2);
            int result;

            if (!tree.Find(3, out result) || result != 3)
            {
                Assert.Fail();
            }
        }
예제 #8
0
        public void TestRemoveParentLeft()
        {
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(5);
            tree.Insert(4);
            tree.Insert(7);
            tree.Insert(6);
            tree.Remove(7);
            int result = 0;

            if (tree.Find(7, out result))
            {
                Assert.Fail();
            }
            VerifyTree(tree);
        }
예제 #9
0
        public void TestRemoveOneSibling()
        {
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(5);
            tree.Insert(4);
            tree.Insert(8);
            tree.Insert(6);
            tree.Insert(9);
            tree.Remove(9);
            int result = 0;

            if (tree.Find(9, out result))
            {
                Assert.Fail();
            }
            VerifyTree(tree);
        }
예제 #10
0
        public void TestRemoveParentWithTwoChildren()
        {
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(5);
            tree.Insert(4);
            tree.Insert(8);
            tree.Insert(6);
            tree.Insert(9);
            tree.Remove(8);
            int result = 0;

            if (tree.Find(8, out result))
            {
                Assert.Fail();
            }
            VerifyTree(tree);
        }
예제 #11
0
        public void TestRemoveBlackSiblingRedMirror()
        {
            //case 1-2,2-2
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(5);
            tree.Insert(3);
            tree.Insert(8);
            tree.Insert(2);
            tree.Insert(4);
            tree.Insert(1);
            tree.Remove(8);
            int result = 0;

            if (tree.Find(8, out result))
            {
                Assert.Fail();
            }
            VerifyTree(tree);
        }
예제 #12
0
        public void TestRemoveBlackSiblingRed()
        {
            //case 1,2
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(5);
            tree.Insert(3);
            tree.Insert(10);
            tree.Insert(7);
            tree.Insert(13);
            tree.Insert(14);
            tree.Remove(3);
            int result = 0;

            if (tree.Find(3, out result))
            {
                Assert.Fail();
            }
            VerifyTree(tree);
        }
예제 #13
0
        public void TestRemoveRedLeftSibling()
        {
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(11);
            tree.Insert(2);
            tree.Insert(14);
            tree.Insert(1);
            tree.Insert(7);
            tree.Insert(15);
            tree.Insert(5);
            tree.Insert(8);
            tree.Insert(4);
            tree.Remove(5);
            int result = 0;

            if (tree.Find(5, out result))
            {
                Assert.Fail();
            }
            VerifyTree(tree);
        }
예제 #14
0
        public void TestRemoveBlackSiblingRedBlackNephewMirror()
        {
            //case 3-2, 4-2
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(10);
            tree.Insert(5);
            tree.Insert(15);
            tree.Insert(3);
            tree.Insert(8);
            tree.Insert(12);
            tree.Insert(18);
            tree.Insert(9);
            tree.Insert(13);
            tree.Remove(18);
            int result = 0;

            if (tree.Find(18, out result))
            {
                Assert.Fail();
            }
            VerifyTree(tree);
        }
예제 #15
0
        public void TestRemoveBlackSiblingNephewRed()
        {
            //case 1
            RBTree <int> tree = new RBTree <int>();

            tree.Insert(11);
            tree.Insert(2);
            tree.Insert(14);
            tree.Insert(1);
            tree.Insert(7);
            tree.Insert(15);
            tree.Insert(5);
            tree.Insert(8);
            tree.Insert(4);
            tree.Remove(2);
            int result = 0;

            if (tree.Find(2, out result))
            {
                Assert.Fail();
            }
            VerifyTree(tree);
        }
예제 #16
0
        public void SimpleTreeTest()
        {
            //Console.WriteLine("rbtree");

            Random       r = new Random((int)(DateTime.Now.Ticks ^ 0xffff));
            RBTree <int> tree = new RBTree <int>();
            List <int>   vals = new List <int>();
            List <int>   removed = new List <int>();
            int          i, j, k;

            //fill tree
            while (tree.Count != 1000)
            {
                i = r.Next(10000);
                if (tree.Find(i) != null)
                {
                    continue;
                }
                tree.Add(i);
                vals.Add(i);
            }

            //check presence of the values
            k = 0;
            foreach (int val in vals)
            {
                if (tree.Find(val) == null)
                {
                    k++;
                }
            }

            Assert.AreEqual(k, 0);
            //Console.WriteLine("{0}", k == 0 ? "ok" : "failed");


            //check order
            j = -1;
            foreach (RBTreeNode <int> node in tree)
            {
                if (node.Key < j)
                {
                    k++;
                }
            }
            Assert.AreEqual(k, 0);
            //Console.WriteLine("{0}", k == 0 ? "ok" : "failed");

            //check remove
            for (i = 0; i < vals.Count; i += 25)
            {
                tree.Remove(vals[i]);
                removed.Add(vals[i]);
                vals.RemoveAt(i);
            }

            k = 0;
            foreach (int val in vals)
            {
                if (tree.Find(val) == null)
                {
                    k++;
                }
            }
            foreach (int val in removed)
            {
                if (tree.Find(val) != null)
                {
                    k++;
                }
            }
            Assert.AreEqual(k, 0);
            //Console.WriteLine("{0}", k == 0 ? "ok" : "failed");

            //chaos test
            k = 0;
            for (j = 0; j < 50; j++)
            {
                tree.Clear();
                vals.Clear();
                removed.Clear();

                //fill tree
                while (tree.Count != 5000)
                {
                    i = r.Next(100000);
                    if (tree.Find(i) != null)
                    {
                        continue;
                    }
                    tree.Add(i);
                    vals.Add(i);
                }

                while (tree.Count != 100)
                {
                    i = r.Next(vals.Count);
                    tree.Remove(vals[i]);

                    removed.Add(vals[i]);
                    vals.RemoveAt(i);
                }

                foreach (int val in vals)
                {
                    if (tree.Find(val) == null)
                    {
                        k++;
                    }
                }
                foreach (int val in removed)
                {
                    if (tree.Find(val) != null)
                    {
                        k++;
                    }
                }
            }
            Assert.AreEqual(k, 0);
            //Console.WriteLine("{0}", k == 0 ? "ok" : "failed");
        }