Exemplo n.º 1
0
        public void Clear_ClearTreeFilledWithStudent_ShouldReturnNullRoot()
        {
            //arrange
            Student          A      = new Student("", "", "", 50);
            BSTree <Student> bSTree = new BSTree <Student>(A);

            //act
            bSTree.Clear();
            var res = bSTree.Root;

            //assert
            Assert.AreEqual(null, res);
        }
Exemplo n.º 2
0
    static public void Main()
    {
        // build a binary search tree
        IBSTree aBSTree = new BSTree();

        aBSTree.Insert('M');
        aBSTree.Insert('D');
        aBSTree.Insert('G');
        aBSTree.Insert('A');
        aBSTree.Insert('W');
        aBSTree.Insert('P');

        // pre-order traversal
        aBSTree.PreOrderTraverse();
        // in-order traversal
        aBSTree.InOrderTraverse();
        // post-order traversal
        aBSTree.PostOrderTraverse();

        // delete a leaf A
        aBSTree.Delete('A');

        // pre-order traversal
        aBSTree.PreOrderTraverse();
        // in-order traversal
        aBSTree.InOrderTraverse();
        // post-order traversal
        aBSTree.PostOrderTraverse();

        // put A back aBStree
        aBSTree.Insert('A');
        // delete a node W, which has only one child
        aBSTree.Delete('W');

        // pre-order traversal
        aBSTree.PreOrderTraverse();
        // in-order traversal
        aBSTree.InOrderTraverse();
        // post-order traversal
        aBSTree.PostOrderTraverse();

        // clear the binary tree
        aBSTree.Clear();

        // pre-order traversal
        aBSTree.PreOrderTraverse();
    }
    static public void Main()
    {
        // build a binary search tree
        Console.WriteLine("Initially, an empty BST is created");
        IBSTree  aBSTree    = new BSTree();
        Customer customer01 = new Customer("Huanyi", "Qian", 17701449052);
        Customer customer02 = new Customer("Dick", "Pussy", 12345678910);
        Customer customer03 = new Customer("Stop", "it", 17701449052);
        Customer customer04 = new Customer("Ass", "F**k", 12345678910);
        Customer customer05 = new Customer("Idiot", "Freeman", 17701449052);
        Customer customer06 = new Customer("Saul", "Goodman", 12345678910);
        Customer customer07 = new Customer("Shabi", "ZhuXinhu", 12345678910);


        aBSTree.Insert(customer01);
        aBSTree.Insert(customer02);
        aBSTree.Insert(customer03);
        aBSTree.Insert(customer04);
        aBSTree.Insert(customer05);
        aBSTree.Insert(customer06);
        aBSTree.Insert(customer07);
        Console.WriteLine();
        Console.WriteLine();

        // pre-order traversal
        aBSTree.PreOrderTraverse();
        // in-order traversal

        aBSTree.InOrderTraverse();
        // post-order traversal

        aBSTree.PostOrderTraverse();

        Console.WriteLine();
        Console.WriteLine();

        // delete a leaf A

        aBSTree.Delete(customer07);
        Console.WriteLine("Zhuxinhu has been deleted from the BST");
        Console.WriteLine();
        Console.WriteLine();

        // pre-order traversal

        aBSTree.PreOrderTraverse();
        // in-order traversal

        aBSTree.InOrderTraverse();
        // post-order traversal

        aBSTree.PostOrderTraverse();

        Console.WriteLine();
        Console.WriteLine();

        // put A back aBStree
        aBSTree.Insert(customer07);
        Console.WriteLine("Zhuxinhu has been added back to the BST");
        // delete a node W, which has only one child

        aBSTree.Delete(customer06);
        Console.WriteLine("Goodman has been deleted from the BST");
        Console.WriteLine();
        Console.WriteLine();

        // pre-order traversal

        aBSTree.PreOrderTraverse();
        // in-order traversal

        aBSTree.InOrderTraverse();
        // post-order traversal

        aBSTree.PostOrderTraverse();

        Console.WriteLine();
        Console.WriteLine();

        // clear the binary tree
        aBSTree.Clear();
        Console.WriteLine("The BST has been cleard");
        Console.WriteLine();
        Console.WriteLine();

        // pre-order traversal

        aBSTree.PreOrderTraverse();
        // in-order traversal

        aBSTree.InOrderTraverse();
        // post-order traversal

        aBSTree.PostOrderTraverse();

        Console.WriteLine();
        Console.WriteLine();
        Console.ReadKey();
    }