Beispiel #1
0
        private static void TraverseBinaryTreeExercise()
        {
            var tree = new SearchBinaryTree(new int[] { 20, 10, 30, 6, 14, 24, 3, 8, 26 });

            Console.WriteLine("Traverse pre-order: {0}",
                              string.Join(",",
                                          tree.TraversePreOrder()));

            Console.WriteLine("Traverse in order:{0}",
                              string.Join(",",
                                          tree.TraverseInOrder()));

            Console.WriteLine("Traverse postOrder{0}",
                              string.Join(",",
                                          tree.TraversePostOrder()));

            var secondTree = new SearchBinaryTree(new int[] { 20, 10, 30, 7, 14, 24, 3, 8, 26 });

            Console.WriteLine("Trees are equals? {0}", tree.Equals(secondTree).ToString());


            var validateTree = new SearchBinaryTree();

            validateTree.CreateInvalidTree();
            Console.WriteLine("Is valid the tree? {0}", validateTree.Validate().ToString());

            var treeK = new SearchBinaryTree(new int[] { 20, 10, 6, 21, 3, 8, 30, 31 });

            Console.WriteLine("K 3:{0}",
                              string.Join(",", treeK.NodesAtK(2)));
        }
Beispiel #2
0
        private static void SearchBinaryTreeExercise1()
        {
            var tree = new SearchBinaryTree(new int[] { 7, 9, 10, 4, 1, 6, 8 });

            Console.WriteLine("Find 10: {0}", tree.Find(10));
            Console.WriteLine("Find 8 {0} ", tree.Find(8));
            Console.WriteLine("Find 17: {0}", tree.Find(7));
            Console.WriteLine("Find 20: {0}", tree.Find(20));
        }
    public bool Equals(SearchBinaryTree tree)
    {
        if (tree == null)
        {
            throw new Exception("Can't compare against a null tree");
        }

        var leftTraverse  = TraverseInOrder();
        var rightTraverse = tree.TraverseInOrder();

        for (var i = 0; i < leftTraverse.Length - 1; i++)
        {
            if (leftTraverse[i] != rightTraverse[i])
            {
                return(false);
            }
        }

        return(true);
    }