示例#1
0
        public void SplayTree_StressTest()
        {
            var nodeCount = 1000 * 10;

            var rnd           = new Random();
            var randomNumbers = Enumerable.Range(1, nodeCount)
                                .OrderBy(x => rnd.Next())
                                .ToList();

            var tree = new SplayTree <int>();

            for (int i = 0; i < nodeCount; i++)
            {
                tree.Insert(randomNumbers[i]);
                Assert.IsTrue(tree.Count == i + 1);
            }


            //shuffle again before deletion tests
            randomNumbers = Enumerable.Range(1, nodeCount)
                            .OrderBy(x => rnd.Next())
                            .ToList();


            for (int i = 0; i < nodeCount; i++)
            {
                tree.Delete(randomNumbers[i]);
                Assert.IsTrue(tree.Count == nodeCount - 1 - i);
            }

            Assert.IsTrue(tree.Count == 0);
        }
示例#2
0
        public void SplayTree_Accuracy_Test()
        {
            var nodeCount = 1000;

            var rnd           = new Random();
            var sorted        = Enumerable.Range(1, nodeCount).ToList();
            var randomNumbers = sorted
                                .OrderBy(x => rnd.Next())
                                .ToList();

            var tree = new SplayTree <int>();

            for (int i = 0; i < nodeCount; i++)
            {
                tree.Insert(randomNumbers[i]);
                tree.Root.VerifyCount();
                Assert.IsTrue(tree.Count == i + 1);
            }

            for (int i = 0; i < sorted.Count; i++)
            {
                Assert.AreEqual(sorted[i], tree.ElementAt(i));
                Assert.AreEqual(i, tree.IndexOf(sorted[i]));
            }

            //shuffle again before deletion tests
            randomNumbers = Enumerable.Range(1, nodeCount)
                            .OrderBy(x => rnd.Next())
                            .ToList();

            //IEnumerable test using linq
            Assert.AreEqual(tree.Count, tree.Count());
            Assert.AreEqual(tree.Count, tree.AsEnumerableDesc().Count());

            for (int i = 0; i < nodeCount; i++)
            {
                if (rnd.NextDouble() >= 0.5)
                {
                    tree.Delete(randomNumbers[i]);
                }
                else
                {
                    var index = tree.IndexOf(randomNumbers[i]);
                    Assert.AreEqual(tree.ElementAt(index), randomNumbers[i]);
                    tree.RemoveAt(index);
                }

                tree.Root.VerifyCount();
                Assert.IsTrue(tree.Count == nodeCount - 1 - i);
            }

            Assert.IsTrue(tree.Count == 0);
        }
        public void SumOutOfRanfe_Test(int left, int right)
        {
            BinarySearchTreeNode root = null;

            root = SplayTree.Delete(40, root);
            root = SplayTree.Delete(35, root);
            root = SplayTree.Delete(30, root);
            root = SplayTree.Delete(25, root);
            root = SplayTree.Delete(20, root);
            root = SplayTree.Delete(15, root);

            Assert.IsNull(root);
        }
示例#4
0
        public void DeleteTest()
        {
            IEnumerable <long> preOrderList = new List <long>()
            {
                5, -1, 23, 17, 10, -1, 15, -1, -1, 21, 20,
                -1, -1, -1, 35, 30, 25, -1, -1, -1, -1
            };

            var root = BST.ParseBST(ref preOrderList);
            var st   = new SplayTree(root);

            Assert.AreEqual("5(-,23(17(10(-,15),21(20,-)),35(30(25,-),-)))", st.ToString());

            st.Delete(25);
            Assert.AreEqual("30(23(5(-,17(10(-,15),21(20,-))),-),35)", st.ToString());

            st.Delete(15);
            Assert.AreEqual("17(10(5,-),30(23(21(20,-),-),35))", st.ToString());

            st.Delete(12);
            Assert.AreEqual("17(10(5,-),30(23(21(20,-),-),35))", st.ToString());
        }
        public void Delete_AllFromBottom_Test()
        {
            var c = new Context();

            c.N40.Left = c.N35;
            c.N35.Left = c.N30;
            c.N30.Left = c.N25;
            c.N25.Left = c.N20;
            c.N20.Left = c.N15;

            var root = c.N40;

            root = SplayTree.Delete(15, root);
            root = SplayTree.Delete(20, root);
            root = SplayTree.Delete(25, root);
            root = SplayTree.Delete(30, root);
            root = SplayTree.Delete(35, root);
            root = SplayTree.Delete(40, root);

            Assert.IsNull(root);
        }
示例#6
0
        public void SplayTree_BulkInit_Test()
        {
            var nodeCount = 1000;

            var rnd           = new Random();
            var sortedNumbers = Enumerable.Range(1, nodeCount).ToList();

            var tree = new SplayTree <int>(sortedNumbers);

            Assert.IsTrue(tree.Root.IsBinarySearchTree(int.MinValue, int.MaxValue));
            Assert.AreEqual(tree.Count, tree.Count());
            tree.Root.VerifyCount();

            for (int i = 0; i < nodeCount; i++)
            {
                Assert.IsTrue(tree.Root.IsBinarySearchTree(int.MinValue, int.MaxValue));
                tree.Delete(sortedNumbers[i]);
                Assert.IsTrue(tree.Count == nodeCount - 1 - i);
            }

            Assert.IsTrue(tree.Count == 0);
        }
示例#7
0
        public void SplayTree_Smoke_Test()
        {
            //insert test
            var tree = new SplayTree <int>();


            tree.Insert(11);
            tree.Insert(6);
            tree.Insert(8);
            tree.Insert(19);
            tree.Insert(4);
            tree.Insert(10);
            tree.Insert(5);
            tree.Insert(17);
            tree.Insert(43);
            tree.Insert(49);
            tree.Insert(31);

            //delete
            tree.Delete(43);
            tree.Delete(11);
            tree.Delete(6);
            tree.Delete(8);
            tree.Delete(19);
            tree.Delete(4);
            tree.Delete(10);
            tree.Delete(5);
            tree.Delete(17);
            tree.Delete(49);
            tree.Delete(31);

            Assert.AreEqual(tree.Count, 0);

            tree.Insert(31);
        }
        public void TestAllOperationsOnRandomNumbers()
        {
            int             count = 100000; //MIN IS 100
            List <int>      numbers = new List <int>(count);
            List <int>      removed = new List <int>(count);
            SplayTree <int> tree = new SplayTree <int>();
            Random          random = new Random();
            int             addCount = 0, succAddCount = 0,
                            findCount = 0, succFindCount = 0,
                            deleteCount = 0, succDeleteCount = 0,
                            compareCheck = 0;


            for (int i = 0; i < count; i++)
            {
                int number = random.Next(0, 100 * count);
                if (number % 3 == 0)
                {
                    addCount++;
                    if (tree.Add(number))
                    {
                        succAddCount++;
                        numbers.Add(number);
                        if (removed.Contains(number))
                        {
                            removed.Remove(number);
                        }
                    }
                }
                else
                if (number % 3 == 1)
                {
                    findCount++;
                    if (numbers.Count > 0)
                    {
                        Assert.IsTrue(tree.Contains(numbers[random.Next(0, numbers.Count)]));
                        //COULD BE CHANGED TO PROFIT FROM SPLAY
                        succFindCount++;
                    }
                    if (removed.Count > 0)
                    {
                        Assert.IsFalse(tree.Contains(removed[random.Next(0, removed.Count)]));
                    }
                }
                else
                if (number % 3 == 2)
                {
                    deleteCount++;
                    if (numbers.Count > 0)
                    {
                        int toDelete = numbers[random.Next(0, numbers.Count)];
                        tree.Delete(toDelete);
                        succDeleteCount++;
                        numbers.Remove(toDelete);
                        removed.Add(toDelete);
                    }
                    if (removed.Count > 0)
                    {
                        tree.Delete(removed[random.Next(0, removed.Count)]); // SHOULD DO NOTHING
                    }
                }
                if (number % (count / 100) == 0)
                {
                    compareCheck++;
                    foreach (int num in numbers)
                    {
                        Assert.IsTrue(tree.Contains(num));
                    }
                    foreach (int num in removed)
                    {
                        Assert.IsFalse(tree.Contains(num));
                    }
                    Assert.IsTrue(tree.CheckTreeStructure());
                }
            }


            String treeTraversalLevelOrder = tree.TraverseLevelOrder();

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\splaytreeTestLevelOrder.txt",
                              treeTraversalLevelOrder);
            String treeTraversalInOrder = tree.TraverseInOrder();

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\splaytreeTestInOrder.txt",
                              treeTraversalInOrder);
            String checkInfo = "ADD checks: " + addCount + "\r\nSuccessfully added elements: " + succAddCount +
                               "\r\nFIND checks: " + findCount + "\r\nSuccessfully found elements: " + succFindCount +
                               "\r\nDELETE checks: " + deleteCount + "\r\nSuccessfully deleted elements: " + succDeleteCount +
                               "\r\nTree-List comparisons: " + compareCheck;

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\splaytreeTestStats.txt",
                              checkInfo);
        }