示例#1
0
        public void Find_TryFindValue_IfTreeIsEmtyReturnNull()
        {
            //Arrange
            MyBinaryTree <int> tree = new MyBinaryTree <int>();
            //Act
            var res = tree.Find(5);

            //Assert
            Assert.Null(res);
        }
示例#2
0
        public void Insert_InsertValueToRoot_TreeContainsValue()
        {
            //Arrange
            MyBinaryTree <int> tree = new MyBinaryTree <int>();

            //Act
            tree.Insert(5);
            //Assert
            var res = tree.Find(5);

            Assert.NotNull(res);
        }
示例#3
0
        public void Remove_RemoveRootValue_IfTreeIsEmptyDontThrowException()
        {
            //Arrange
            MyBinaryTree <int> tree = new MyBinaryTree <int>();

            //Act
            tree.Remove(5);
            //Assert
            var res = tree.Find(5);

            Assert.Null(res);
        }
示例#4
0
        public void Insert_InsertValueToLeaves_TreeContainsNewValue(string value)
        {
            //Arrange
            MyBinaryTree <string> tree = new MyBinaryTree <string>();

            tree.Insert("5");
            //Act
            tree.Insert(value);
            //Assert
            var res = tree.Find(value);

            Assert.NotNull(res);
        }
示例#5
0
        public void Find_TryFindValue_IfValuePresentsReturnNotNull(int first, int second, int find)
        {
            //Arrange
            MyBinaryTree <int> tree = new MyBinaryTree <int>();

            tree.Insert(first);
            tree.Insert(second);
            //Act
            var res = tree.Find(find);

            //Assert
            Assert.NotNull(res);
        }
示例#6
0
        public void Remove_RemoveRootValue_NodeRemovedAndTreeGetsNewRoot(string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8, string s9, string remove)
        {
            //Arrange
            MyBinaryTree <string> tree = new MyBinaryTree <string>();

            tree.Insert(s1);
            tree.Insert(s2);
            tree.Insert(s3);
            tree.Insert(s4);
            tree.Insert(s5);
            tree.Insert(s6);
            tree.Insert(s7);
            tree.Insert(s8);
            tree.Insert(s9);
            //Act
            tree.Remove(remove);
            //Assert
            var res = tree.Find(remove);

            Assert.Null(res);
            Assert.NotNull(tree.Root);
        }
示例#7
0
        public static void TestCÂY()
        {
            var tree = new MyBinaryTree <int>();
            var chon = 0;

            do
            {
                chon = MenuCây();
                switch (chon)
                {
                case 1: { FileToBST(tree); break; }

                case 2: { TreeRandom(tree); break; }

                case 3:
                {
                    Console.Write("\n\nNhap nut can them: ");
                    var keyadd = int.Parse(Console.ReadLine());
                    tree.Insert(keyadd);
                    break;
                }

                case 4:
                {
                    PrintTree(tree);
                    Console.Write("\n\nNhap nut can xoa ");
                    var keydelete = int.Parse(Console.ReadLine());
                    tree.Remove(ref tree.root, keydelete);
                    break;
                }

                case 5:
                {
                    PrintTree(tree);
                    Console.Write("\n\nNhap khoa can tim kiem: ");
                    var keysearch = int.Parse(Console.ReadLine());
                    var result    = tree.Find(keysearch);
                    if (result)
                    {
                        Console.WriteLine("Tim thay {0} trong cay nhi phan tim kiem", keysearch);
                    }
                    else
                    {
                        Console.WriteLine("Khong tim thay {0} trong cay nhi phan tim kiem", keysearch);
                    }
                    break;
                }

                case 6:
                {
                    tree.DeleteTree();
                    Console.WriteLine("\n => Da xoa toan bo cay");
                    break;
                }

                case 7:
                {
                    PrintTree(tree);
                    Console.WriteLine("\n => so nut tren cay: {0}", tree.CountChildren());
                    break;
                }

                case 8:
                {
                    PrintTree(tree);
                    Console.WriteLine("\n => so nut la tren cay: {0}", tree.Demsonutla());
                    break;
                }

                case 9:
                {
                    PrintTree(tree);
                    Console.WriteLine("\n => so nut chi co mot cay con: {0}", tree.Count_nut_co_1_con(tree.Root));
                    break;
                }

                case 10:
                {
                    PrintTree(tree);
                    Console.Write("\n\n- Nhap khoa 1: ");
                    var v1 = int.Parse(Console.ReadLine());
                    Console.Write("\n- Nhap khoa 2: ");
                    var v2  = int.Parse(Console.ReadLine());
                    var lca = tree.LCA(v1, v2);
                    Console.WriteLine("=> LCA of {0} and {1} is: {2}", v1, v2, (lca != null ? lca.Data.ToString() : "No LCA Found"));
                    break;
                }

                case 11:
                {
                    PrintTree(tree);
                    Console.Write("\n\nNhap khoa can tim kiem: ");
                    var key = int.Parse(Console.ReadLine());
                    Console.WriteLine(getLevelOfNode(tree.Root, key, 1).ToString());
                    break;
                }

                case 12:
                {
                    Console.WriteLine();
                    PrintTreeOnLevel(tree.root, 0);
                    break;
                }

                case 13:
                {
                    PrintTree(tree); break;
                }

                default:
                    Console.WriteLine("Unexpected Case");
                    break;
                }
                Console.ReadKey();
                Console.Clear();
            } while (chon != 0);
        }