Esempio n. 1
0
        public void TestAdd_3()
        {
            aBST abst = new aBST(1);

            Assert.AreEqual(0, abst.AddKey(16));
            Assert.AreEqual(2, abst.AddKey(74));
            Assert.AreEqual(1, abst.AddKey(-329));
            Assert.AreEqual(-1, abst.AddKey(-339));
        }
Esempio n. 2
0
        public void TestFind_4()
        {
            aBST abst = new aBST(2);

            abst.AddKey(16);

            Assert.AreEqual(-1, abst.FindKeyIndex(10));
            Assert.AreEqual(-2, abst.FindKeyIndex(18));

            abst.AddKey(18);

            Assert.AreEqual(2, abst.FindKeyIndex(18));
            Assert.AreEqual(-6, abst.FindKeyIndex(20));
        }
Esempio n. 3
0
        public void TestAdd_4()
        {
            aBST abst = new aBST(0);

            Assert.AreEqual(0, abst.AddKey(16));
            Assert.AreEqual(-1, abst.AddKey(74));
            Assert.AreEqual(-1, abst.AddKey(-329));
            Assert.AreEqual(-1, abst.AddKey(-339));

            aBST abst2 = new aBST(1);

            Assert.AreEqual(0, abst2.AddKey(16));
            Assert.AreEqual(1, abst2.AddKey(11));
            Assert.AreEqual(2, abst2.AddKey(19));
            Assert.AreEqual(-1, abst2.AddKey(10));
        }
        public void AddKey_If_Tree_Has_Tree_Only()
        {
            aBST testTree       = new aBST(2);
            int  index          = testTree.AddKey(4);
            int  index2         = testTree.AddKey(2);
            int  index3         = testTree.AddKey(6);
            int  expectedIndex  = 0;
            int? expectedIndex2 = 1;
            int? expectedIndex3 = 2;

            Assert.IsTrue(testTree.Tree.Length == 7);
            Assert.IsTrue(testTree.Tree[0] == 4);
            Assert.IsTrue(testTree.Tree[1] == 2);
            Assert.IsTrue(testTree.Tree[2] == 6);
            Assert.AreEqual(expectedIndex, index);
            Assert.AreEqual(expectedIndex2, index2);
            Assert.AreEqual(expectedIndex3, index3);
        }
Esempio n. 5
0
        public void TestFind_3()
        {
            aBST abst = GetTree_1();
            int  b1   = abst.AddKey(19);
            int  b2   = abst.AddKey(18);
            int  b3   = abst.AddKey(17);

            Assert.AreEqual(11, b1);
            Assert.AreEqual(23, b2);
            Assert.AreEqual(-1, b3);

            Assert.AreEqual(-24, abst.FindKeyIndex(20));
            Assert.AreEqual(0, abst.FindKeyIndex(16));
            Assert.AreEqual(5, abst.FindKeyIndex(22));
            Assert.AreEqual(11, abst.FindKeyIndex(19));
            Assert.AreEqual(23, abst.FindKeyIndex(18));
            Assert.AreEqual(null, abst.FindKeyIndex(17));
        }
Esempio n. 6
0
        public void AddEmpty()
        {
            var tree = new aBST(0);

            Assert.True(tree.AddKey(15) == 0);

            Assert.True(tree.FindKeyIndex(15) == 0);
            Assert.True(tree.FindKeyIndex(11) == null);
        }
        public void AddKey_If_Tree_is_Empty()
        {
            aBST testTree      = new aBST(3);
            int  index         = testTree.AddKey(4);
            int  expectedIndex = 0;

            Assert.IsTrue(testTree.Tree.Length == 15);
            Assert.IsTrue(testTree.Tree[0] == 4);
            Assert.AreEqual(expectedIndex, index);
        }
        public void FindKeyIndex_If_Tree_Has_Tree_Only()
        {
            aBST testTree       = new aBST(3);
            int  index          = testTree.AddKey(4);
            int? index2         = testTree.FindKeyIndex(2);
            int  expectedIndex  = 0;
            int? expectedIndex2 = -1;

            Assert.IsTrue(testTree.Tree.Length == 15);
            Assert.IsTrue(testTree.Tree[0] == 4);
            Assert.IsTrue(testTree.Tree[1] == null);
            Assert.AreEqual(expectedIndex, index);
            Assert.AreEqual(expectedIndex2, index2);
        }
Esempio n. 9
0
 public void TestAddKey()
 {
     Assert.AreEqual(0, _tree.AddKey(50));
     Assert.AreEqual(1, _tree.AddKey(25));
     Assert.AreEqual(5, _tree.AddKey(62));
     Assert.AreEqual(14, _tree.AddKey(92));
     Assert.AreEqual(3, _tree.AddKey(24));
     Assert.AreEqual(13, _tree.AddKey(83));
 }
Esempio n. 10
0
        public void FindKeyIndexTest()
        {
            aBST aBST = new aBST(3);

            int[] ff = new int[10] {
                25, 31, 37, 43, 50, 55, 62, 75, 84, 92
            };
            foreach (int aa in ff)
            {
                aBST.AddKey(aa);
            }
            if (aBST.FindKeyIndex(50) == -1)
            {
                Assert.Fail();
            }
        }
        public void AddKey_If_2_Level_Tree_Is_Full()
        {
            aBST testTree = new aBST(2);
            int  index    = testTree.AddKey(4);
            int  index2   = testTree.AddKey(2);
            int  index3   = testTree.AddKey(6);
            int  index4   = testTree.AddKey(1);
            int  index5   = testTree.AddKey(3);
            int  index6   = testTree.AddKey(5);
            int  index7   = testTree.AddKey(7);

            int indexFull = testTree.AddKey(9);

            int expectedIndex  = 0;
            int expectedIndex2 = 1;
            int expectedIndex3 = 2;
            int expectedIndex4 = 3;
            int expectedIndex5 = 4;
            int expectedIndex6 = 5;
            int expectedIndex7 = 6;

            int expectedIndexFull = -1;

            Assert.IsTrue(testTree.Tree.Length == 7);
            Assert.IsTrue(testTree.Tree[0] == 4);
            Assert.IsTrue(testTree.Tree[1] == 2);
            Assert.IsTrue(testTree.Tree[2] == 6);
            Assert.IsTrue(testTree.Tree[3] == 1);
            Assert.IsTrue(testTree.Tree[4] == 3);
            Assert.IsTrue(testTree.Tree[5] == 5);
            Assert.IsTrue(testTree.Tree[6] == 7);

            Assert.AreEqual(expectedIndex, index);
            Assert.AreEqual(expectedIndex2, index2);
            Assert.AreEqual(expectedIndex3, index3);
            Assert.AreEqual(expectedIndex4, index4);
            Assert.AreEqual(expectedIndex5, index5);
            Assert.AreEqual(expectedIndex6, index6);
            Assert.AreEqual(expectedIndex7, index7);
            Assert.AreEqual(expectedIndexFull, indexFull);
        }
Esempio n. 12
0
        public void aBSTTest()
        {
            aBST aBST = new aBST(3);

            if (aBST.AddKey(50) == -1)
            {
                Assert.Fail();
            }
            if (aBST.FindKeyIndex(50) != 0)
            {
                Assert.Fail();
            }
            if (aBST.AddKey(25) == -1)
            {
                Assert.Fail();
            }
            if (aBST.FindKeyIndex(25) != 1)
            {
                Assert.Fail();
            }
            if (aBST.AddKey(75) == -1)
            {
                Assert.Fail();
            }
            if (aBST.AddKey(37) == -1)
            {
                Assert.Fail();
            }
            if (aBST.AddKey(62) == -1)
            {
                Assert.Fail();
            }
            if (aBST.AddKey(84) == -1)
            {
                Assert.Fail();
            }
            if (aBST.AddKey(31) == -1)
            {
                Assert.Fail();
            }
            if (aBST.AddKey(43) == -1)
            {
                Assert.Fail();
            }
            if (aBST.AddKey(55) == -1)
            {
                Assert.Fail();
            }
            if (aBST.AddKey(92) == -1)
            {
                Assert.Fail();
            }
            if (aBST.Tree[3] != null)
            {
                Assert.Fail();
            }
            if (aBST.FindKeyIndex(92) != 14)
            {
                Assert.Fail();
            }
        }
        public void AddKey_If_3_Level_Tree_Is_Full()
        {
            aBST testTree = new aBST(3);
            int  index1   = testTree.AddKey(8);
            int  index2   = testTree.AddKey(4);
            int  index3   = testTree.AddKey(12);
            int  index4   = testTree.AddKey(2);
            int  index5   = testTree.AddKey(6);
            int  index6   = testTree.AddKey(10);
            int  index7   = testTree.AddKey(14);
            int  index8   = testTree.AddKey(1);
            int  index9   = testTree.AddKey(3);
            int  index10  = testTree.AddKey(5);
            int  index11  = testTree.AddKey(7);
            int  index12  = testTree.AddKey(9);
            int  index13  = testTree.AddKey(11);
            int  index14  = testTree.AddKey(13);
            int  index15  = testTree.AddKey(15);

            int indexFull      = testTree.AddKey(0);
            int indexDuplicate = testTree.AddKey(7);

            int expectedAddedIndex1  = 0;
            int expectedAddedIndex2  = 1;
            int expectedAddedIndex3  = 2;
            int expectedAddedIndex4  = 3;
            int expectedAddedIndex5  = 4;
            int expectedAddedIndex6  = 5;
            int expectedAddedIndex7  = 6;
            int expectedAddedIndex8  = 7;
            int expectedAddedIndex9  = 8;
            int expectedAddedIndex10 = 9;
            int expectedAddedIndex11 = 10;
            int expectedAddedIndex12 = 11;
            int expectedAddedIndex13 = 12;
            int expectedAddedIndex14 = 13;
            int expectedAddedIndex15 = 14;

            int expectedIndexFull      = -1;
            int expectedIndexDuplicate = 10;

            Assert.IsTrue(testTree.Tree.Length == 15);
            Assert.IsTrue(testTree.Tree[0] == 8);
            Assert.IsTrue(testTree.Tree[1] == 4);
            Assert.IsTrue(testTree.Tree[2] == 12);
            Assert.IsTrue(testTree.Tree[3] == 2);
            Assert.IsTrue(testTree.Tree[4] == 6);
            Assert.IsTrue(testTree.Tree[5] == 10);
            Assert.IsTrue(testTree.Tree[6] == 14);
            Assert.IsTrue(testTree.Tree[7] == 1);
            Assert.IsTrue(testTree.Tree[8] == 3);
            Assert.IsTrue(testTree.Tree[9] == 5);
            Assert.IsTrue(testTree.Tree[10] == 7);
            Assert.IsTrue(testTree.Tree[11] == 9);
            Assert.IsTrue(testTree.Tree[12] == 11);
            Assert.IsTrue(testTree.Tree[13] == 13);
            Assert.IsTrue(testTree.Tree[14] == 15);

            Assert.AreEqual(expectedAddedIndex1, index1);
            Assert.AreEqual(expectedAddedIndex2, index2);
            Assert.AreEqual(expectedAddedIndex3, index3);
            Assert.AreEqual(expectedAddedIndex4, index4);
            Assert.AreEqual(expectedAddedIndex5, index5);
            Assert.AreEqual(expectedAddedIndex6, index6);
            Assert.AreEqual(expectedAddedIndex7, index7);
            Assert.AreEqual(expectedAddedIndex8, index8);
            Assert.AreEqual(expectedAddedIndex9, index9);
            Assert.AreEqual(expectedAddedIndex10, index10);
            Assert.AreEqual(expectedAddedIndex11, index11);
            Assert.AreEqual(expectedAddedIndex12, index12);
            Assert.AreEqual(expectedAddedIndex13, index13);
            Assert.AreEqual(expectedAddedIndex14, index14);
            Assert.AreEqual(expectedAddedIndex15, index15);

            Assert.AreEqual(expectedIndexFull, indexFull);
            Assert.AreEqual(expectedIndexDuplicate, indexDuplicate);
        }
Esempio n. 14
0
        /*
         *               16
         *           8       24
         *        4   12   22   28
         *      -1  11 13   23
         *
         */
        private aBST GetTree_1()
        {
            aBST abst = new aBST(4);

            abst.AddKey(16);
            abst.AddKey(8);
            abst.AddKey(4);
            abst.AddKey(-1);
            abst.AddKey(12);
            abst.AddKey(13);
            abst.AddKey(11);
            abst.AddKey(24);
            abst.AddKey(22);
            abst.AddKey(23);
            abst.AddKey(28);
            return(abst);
        }
Esempio n. 15
0
        public void TestOverAll_1()
        {
            aBST abst = new aBST(3);

            Assert.AreEqual(0, abst.AddKey(16));

            Assert.AreEqual(1, abst.AddKey(4));
            Assert.AreEqual(2, abst.AddKey(24));

            Assert.AreEqual(3, abst.AddKey(-2));
            Assert.AreEqual(4, abst.AddKey(12));
            Assert.AreEqual(5, abst.AddKey(20));
            Assert.AreEqual(6, abst.AddKey(28));

            Assert.AreEqual(7, abst.AddKey(-3));
            Assert.AreEqual(8, abst.AddKey(-1));
            Assert.AreEqual(9, abst.AddKey(8));
            Assert.AreEqual(10, abst.AddKey(14));
            Assert.AreEqual(11, abst.AddKey(18));
            Assert.AreEqual(12, abst.AddKey(22));
            Assert.AreEqual(13, abst.AddKey(26));
            Assert.AreEqual(14, abst.AddKey(30));

            Assert.AreEqual(14, abst.AddKey(30));
            Assert.AreEqual(-1, abst.AddKey(32));

            Assert.AreEqual(0, abst.FindKeyIndex(16));

            Assert.AreEqual(1, abst.FindKeyIndex(4));
            Assert.AreEqual(2, abst.FindKeyIndex(24));

            Assert.AreEqual(3, abst.FindKeyIndex(-2));
            Assert.AreEqual(4, abst.FindKeyIndex(12));
            Assert.AreEqual(5, abst.FindKeyIndex(20));
            Assert.AreEqual(6, abst.FindKeyIndex(28));

            Assert.AreEqual(7, abst.FindKeyIndex(-3));
            Assert.AreEqual(8, abst.FindKeyIndex(-1));
            Assert.AreEqual(9, abst.FindKeyIndex(8));
            Assert.AreEqual(10, abst.FindKeyIndex(14));
            Assert.AreEqual(11, abst.FindKeyIndex(18));
            Assert.AreEqual(12, abst.FindKeyIndex(22));
            Assert.AreEqual(13, abst.FindKeyIndex(26));
            Assert.AreEqual(14, abst.FindKeyIndex(30));

            abst.Tree[14] = null;

            Assert.AreEqual(-14, abst.FindKeyIndex(30));
            Assert.AreEqual(14, abst.AddKey(30));
            Assert.AreEqual(14, abst.FindKeyIndex(30));
        }
        public void FindKey_If_3_Level_Tree_has_Empty_4_Level()
        {
            aBST testTree = new aBST(3);
            int  index1   = testTree.AddKey(8);
            int  index2   = testTree.AddKey(4);
            int  index3   = testTree.AddKey(12);
            int  index4   = testTree.AddKey(2);
            int  index5   = testTree.AddKey(6);
            int  index6   = testTree.AddKey(10);
            int  index7   = testTree.AddKey(14);

            int?index8  = testTree.FindKeyIndex(1);
            int?index9  = testTree.FindKeyIndex(3);
            int?index10 = testTree.FindKeyIndex(5);
            int?index11 = testTree.FindKeyIndex(7);
            int?index12 = testTree.FindKeyIndex(9);
            int?index13 = testTree.FindKeyIndex(11);
            int?index14 = testTree.FindKeyIndex(13);
            int?index15 = testTree.FindKeyIndex(15);

            int expectedAddedIndex1  = 0;
            int expectedAddedIndex2  = 1;
            int expectedAddedIndex3  = 2;
            int expectedAddedIndex4  = 3;
            int expectedAddedIndex5  = 4;
            int expectedAddedIndex6  = 5;
            int expectedAddedIndex7  = 6;
            int expectedAddedIndex8  = -7;
            int expectedAddedIndex9  = -8;
            int expectedAddedIndex10 = -9;
            int expectedAddedIndex11 = -10;
            int expectedAddedIndex12 = -11;
            int expectedAddedIndex13 = -12;
            int expectedAddedIndex14 = -13;
            int expectedAddedIndex15 = -14;

            Assert.IsTrue(testTree.Tree.Length == 15);
            Assert.IsTrue(testTree.Tree[0] == 8);
            Assert.IsTrue(testTree.Tree[1] == 4);
            Assert.IsTrue(testTree.Tree[2] == 12);
            Assert.IsTrue(testTree.Tree[3] == 2);
            Assert.IsTrue(testTree.Tree[4] == 6);
            Assert.IsTrue(testTree.Tree[5] == 10);
            Assert.IsTrue(testTree.Tree[6] == 14);

            Assert.IsNull(testTree.Tree[7]);
            Assert.IsNull(testTree.Tree[8]);
            Assert.IsNull(testTree.Tree[9]);
            Assert.IsNull(testTree.Tree[10]);
            Assert.IsNull(testTree.Tree[11]);
            Assert.IsNull(testTree.Tree[12]);
            Assert.IsNull(testTree.Tree[13]);
            Assert.IsNull(testTree.Tree[14]);

            Assert.AreEqual(expectedAddedIndex1, index1);
            Assert.AreEqual(expectedAddedIndex2, index2);
            Assert.AreEqual(expectedAddedIndex3, index3);
            Assert.AreEqual(expectedAddedIndex4, index4);
            Assert.AreEqual(expectedAddedIndex5, index5);
            Assert.AreEqual(expectedAddedIndex6, index6);
            Assert.AreEqual(expectedAddedIndex7, index7);
            Assert.AreEqual(expectedAddedIndex8, index8);
            Assert.AreEqual(expectedAddedIndex9, index9);
            Assert.AreEqual(expectedAddedIndex10, index10);
            Assert.AreEqual(expectedAddedIndex11, index11);
            Assert.AreEqual(expectedAddedIndex12, index12);
            Assert.AreEqual(expectedAddedIndex13, index13);
            Assert.AreEqual(expectedAddedIndex14, index14);
            Assert.AreEqual(expectedAddedIndex15, index15);
        }
Esempio n. 17
0
        public void Add()
        {
            var tree = new aBST(2);

            Assert.True(tree.AddKey(15) == 0);
            Assert.True(tree.AddKey(10) == 1);
            Assert.True(tree.AddKey(20) == 2);
            Assert.True(tree.AddKey(12) == 4);
            Assert.True(tree.AddKey(17) == 5);
            Assert.True(tree.AddKey(11) == -1);

            Assert.True(tree.AddKey(10) == 1);
            Assert.True(tree.AddKey(15) == 0);
            Assert.True(tree.AddKey(20) == 2);
            Assert.True(tree.AddKey(12) == 4);
            Assert.True(tree.AddKey(17) == 5);

            Assert.True(tree.FindKeyIndex(15) == 0);
            Assert.True(tree.FindKeyIndex(11) == null);
            Assert.True(tree.FindKeyIndex(5) == -3);
        }
Esempio n. 18
0
        public void TestAdd_1()
        {
            aBST abst = new aBST(4);

            abst.AddKey(16);

            abst.AddKey(8);
            abst.AddKey(3);
            abst.AddKey(10);
            abst.AddKey(12);
            abst.AddKey(9);

            abst.AddKey(24);
            abst.AddKey(19);
            abst.AddKey(17);
            abst.AddKey(28);
            abst.AddKey(26);
            abst.AddKey(29);
            abst.AddKey(30);
            int index = abst.AddKey(31);

            Assert.AreEqual(31, abst.Tree.Length);

            Assert.AreEqual(16, abst.Tree[0]);
            Assert.AreEqual(8, abst.Tree[1]);
            Assert.AreEqual(24, abst.Tree[2]);
            Assert.AreEqual(3, abst.Tree[3]);
            Assert.AreEqual(10, abst.Tree[4]);
            Assert.AreEqual(19, abst.Tree[5]);
            Assert.AreEqual(28, abst.Tree[6]);
            Assert.AreEqual(null, abst.Tree[7]);
            Assert.AreEqual(null, abst.Tree[8]);
            Assert.AreEqual(9, abst.Tree[9]);
            Assert.AreEqual(12, abst.Tree[10]);
            Assert.AreEqual(17, abst.Tree[11]);
            Assert.AreEqual(null, abst.Tree[12]);
            Assert.AreEqual(26, abst.Tree[13]);
            Assert.AreEqual(29, abst.Tree[14]);
            Assert.AreEqual(30, abst.Tree[30]);
            Assert.AreEqual(-1, index);

            /*
             *               16
             *           8       24
             *        3   10   19   28
             *           9 12 17   26 29
             *
             */
        }