Пример #1
0
        private static void Main()
        {
            var splay = new SplayTree <int>();

            splay.Insert(3);
            splay.Preorder();
            splay.Insert(5);
            splay.Preorder();
            splay.Insert(7);
            splay.Preorder();
            splay.Insert(6);
            splay.Preorder();
            splay.Find(3);
            splay.Preorder();
            splay.Insert(1);
            splay.Preorder();
            splay.Remove(6);
            splay.Preorder();
            splay.Find(3);
            splay.Preorder();
            splay.Insert(10);
            splay.Insert(2);
            splay.Preorder();
            Console.ReadKey();
        }
Пример #2
0
        public static void SplayTreeTest()
        {
            SplayTree tree = new SplayTree();

            for (int i = 7; i > 0; i--)
            {
                tree.Insert(i);
            }
            tree.Find(1);
            tree.Find(3);
        }
        public void SplayTreeConstructorWithKeyComparer()
        {
            var tree = new SplayTree <double, int>(NumericUtilities.FloatNumberComparer <double>());

            tree.Add(0.0000001, 0);
            var node = tree.Find(0.000001);

            Assert.IsTrue(node.HasValue);
            Assert.AreEqual(node.Value.Value, 0);
            tree.Add(0.00000012, 1); // Should overwrite previous inserted value
            node = tree.Find(0.00000012);
            Assert.IsTrue(node.HasValue);
            Assert.AreEqual(node.Value.Value, 1);
        }
Пример #4
0
        public void FindDeletedKeyTest()
        {
            SplayTree <int, int> t         = new SplayTree <int, int>();
            HashSet <int>        addedKeys = new HashSet <int>();

            foreach (int i in Enumerable.Range(0, 100))
            {
                int key = this._Rand.Next();
                addedKeys.Add(key);

                t.Add(key, this._Rand.Next());
            }

            foreach (var key in addedKeys)
            {
                t.Remove(key);

                Exception ex = null;
                try
                {
                    t.Find(key);
                }
                catch (Exception innerEx)
                {
                    ex = innerEx;
                }

                Assert.IsInstanceOfType(ex, typeof(KeyNotFoundException));
            }
        }
Пример #5
0
        public void TestChainFind()
        {
            var t = new SplayTree <ulong>();

            t.Root                               = createNode(7);
            t.Root.Left                          = createNode(6);
            t.Root.Left.Left                     = createNode(5);
            t.Root.Left.Left.Left                = createNode(4);
            t.Root.Left.Left.Left.Left           = createNode(3);
            t.Root.Left.Left.Left.Left.Left      = createNode(2);
            t.Root.Left.Left.Left.Left.Left.Left = createNode(1);

            t.Root.Left.Parent                          = t.Root;
            t.Root.Left.Left.Parent                     = t.Root.Left;
            t.Root.Left.Left.Left.Parent                = t.Root.Left.Left;
            t.Root.Left.Left.Left.Left.Parent           = t.Root.Left.Left.Left;
            t.Root.Left.Left.Left.Left.Left.Parent      = t.Root.Left.Left.Left.Left;
            t.Root.Left.Left.Left.Left.Left.Left.Parent = t.Root.Left.Left.Left.Left.Left;

            Assert.AreEqual("[7] [6] [5] [4] [3] [2] [1] ", t.ToString());

            t.Find(1);

            Assert.AreEqual("[1] [6] [4] [7] [2] [5] [3] ", t.ToString());
        }
Пример #6
0
        public static void TestSplayOperations()
        {
            var init = new int [] {8,3,10,1,6,4,7,14,13};
            var splayTree = new SplayTree(init);

            var result = splayTree.Find(6);
            Assert.AreEqual(result.Value, 6);
            Assert.AreEqual(splayTree.Root.Value, 6);

            splayTree.Remove(10);
            var resultRemove = splayTree.Find(10);
            Assert.IsNull(resultRemove);
            Assert.AreEqual(splayTree.Root.Value, 8);

            splayTree.Add(12);
            Assert.AreEqual(splayTree.Root.Value, 12);
            Assert.AreEqual(splayTree.Root.LeftChild.Value, 8);
            Assert.AreEqual(splayTree.Root.RightChild.Value, 13);
        }
Пример #7
0
        public void ContainsAndFindTest()
        {
            var tree = new SplayTree <int>();
            int N    = 200;

            for (int i = 0; i < N; ++i)
            {
                tree.Insert(i + 10);
            }
            Assert.AreEqual(N, tree.Count);
            for (int i = N - 1; i >= 0; --i)
            {
                Assert.AreEqual(10 + i, tree.Find(i + 10).Value);
            }
            for (int i = 0; i < N * N; ++i)
            {
                tree.Find(N - 1);
            }
            Assert.AreEqual(N - 1, tree.Root.Value);
        }
Пример #8
0
        public void Find()
        {
            SplayTree<int, string> tree = new SplayTree<int, string>();

            var a = tree.Add(5, "a");
            var b = tree.Add(3, "b");
            var c = tree.Add(1, "c");
            var d = tree.Add(2, "d");
            var e = tree.Add(10, "e");

            var found = tree.Find(5);
            Assert.IsTrue(found == a);
            Assert.IsTrue(tree.Root == a);
        }
Пример #9
0
        public void Find()
        {
            SplayTree <int, string> tree = new SplayTree <int, string>();

            var a = tree.Add(5, "a");
            var b = tree.Add(3, "b");
            var c = tree.Add(1, "c");
            var d = tree.Add(2, "d");
            var e = tree.Add(10, "e");

            var found = tree.Find(5);

            Assert.IsTrue(found == a);
            Assert.IsTrue(tree.Root == a);
        }
        public void TestSplayOperation()
        {
            SplayTree <int> tree = new SplayTree <int>();

            tree.Add(5);
            tree.Add(4);
            tree.Add(6);
            tree.Add(3);
            tree.Add(2);

            tree.Splay(tree.Find(2));

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\testSplay.txt",
                              tree.TraverseLevelOrder());
        }
Пример #11
0
        public void Find_Zig_Right_Test()
        {
            var c = new Context();

            c.N30.Left  = c.N15;
            c.N30.Right = c.N35;
            c.N20.Left  = c.N10;
            c.N20.Right = c.N30;

            var actual = SplayTree.Find(30, c.N20);

            actual.Should().Be(c.N30);

            c.N30.IsRoot();
            c.N20.IsLeftChildOf(c.N30);
            c.N35.IsRightChildOf(c.N30);
            c.N10.IsLeftChildOf(c.N20);
            c.N15.IsRightChildOf(c.N20);
        }
        public void SplayTreeFind()
        {
            var tree = new SplayTree <int, int>
            {
                new KeyValuePair <int, int>(1, 1),
                new KeyValuePair <int, int>(2, 2),
                new KeyValuePair <int, int>(3, 3)
            };
            var node = tree.Find(2);

            Assert.AreEqual(tree.Count, 3);
            Assert.IsTrue(tree.Root.HasValue);
            Assert.AreEqual(tree.Root.Value.Value, 2);
            Assert.IsTrue(node.HasValue);
            Assert.AreEqual(node.Value.Key, 2);
            Assert.AreEqual(node.Value.Value, 2);
            Assert.IsFalse(node.Value.Parent.HasValue);
            Assert.IsTrue(node.Value.Left.HasValue);
            Assert.IsTrue(node.Value.Right.HasValue);
            Assert.AreEqual(node.Value.Left.Value.Value, 1);
            Assert.AreEqual(node.Value.Right.Value.Value, 3);
        }
Пример #13
0
        public void TestChainFind()
        {
            var t = new SplayTree<ulong>();
            t.Root = createNode(7);
            t.Root.Left = createNode(6);
            t.Root.Left.Left = createNode(5);
            t.Root.Left.Left.Left = createNode(4);
            t.Root.Left.Left.Left.Left = createNode(3);
            t.Root.Left.Left.Left.Left.Left = createNode(2);
            t.Root.Left.Left.Left.Left.Left.Left = createNode(1);

            t.Root.Left.Parent = t.Root;
            t.Root.Left.Left.Parent = t.Root.Left;
            t.Root.Left.Left.Left.Parent = t.Root.Left.Left;
            t.Root.Left.Left.Left.Left.Parent = t.Root.Left.Left.Left;
            t.Root.Left.Left.Left.Left.Left.Parent = t.Root.Left.Left.Left.Left;
            t.Root.Left.Left.Left.Left.Left.Left.Parent = t.Root.Left.Left.Left.Left.Left;

            Assert.AreEqual("[7] [6] [5] [4] [3] [2] [1] ", t.ToString());

            t.Find(1);

            Assert.AreEqual("[1] [6] [4] [7] [2] [5] [3] ", t.ToString());
        }