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)));
        }
    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);
    }