简体   繁体   中英

Printing an In-Order Traversal on a BST in C#

I am a student and can't accept code answers, but if anyone can point out the conceptual error I have with In-Order Tree Traversals I would greatly appreciate it. I have been over the internet/books/MSDN and I can't seem to figure out where I am going wrong.

My code will only print the root of the BST. Thanks for the help.

Tree class snipet:

        public String PrintInOrder()
    {
        String S = "";
        return PrintInOrderHelper(ref root, S);
    }

    public String PrintInOrderHelper(ref Node N, String S)
    {
        if (N != null)
        {
            PrintInOrderHelper(ref N.left, S);
            S = S + N.V.ToString().PadLeft(2);
            PrintInOrderHelper(ref N.right, S);
        }
        return S;
    }

Main snipet:

Tree t = new Tree(nums[0]);
            for (int i = 1; i < nums.Length; i++)
            {
                t.Add(nums[i]);
            }
            Console.WriteLine("Tree contents: " + t.PrintInOrder());
            Console.WriteLine("Tree statistics:");
            Console.WriteLine("  Number of nodes: " + t.Count());

I know the BST is being made properly because the Count() is correct. It's the printing I can't come to terms with.

Edit

Thanks to everyone who helped. This was my first time posting and I really appreciate it. I figured out the solution to anyone who looks at this post in the future:

Tree Class:

public void PrintInOrder()
    {
       PrintInOrderHelper(ref root);
    }

    public void PrintInOrderHelper(ref Node N)
    {
       if (N == null)
        {
            return;
        }
        PrintInOrderHelper(ref N.left);
        Console.Write(N.V.ToString().PadLeft(2));
        PrintInOrderHelper(ref N.right);
    }

Main:

   Tree t = new Tree(nums[0]);
            for (int i = 1; i < nums.Length; i++)
            {
                t.Add(nums[i]);
            }
            Console.Write("Tree contents: ");
            t.PrintInOrder();
            Console.WriteLine();
            Console.WriteLine("Tree statistics:");
            Console.WriteLine("  Number of nodes: " + t.Count());

I got rid of all the String passing as it wasn't necessary and made the methods void.

It seems you may have a misunderstanding of passing by value vs passing by reference or just forgot to use the return value. But just for reference (no pun indended)...

void main() {
   String s = ""
   foo(s);
   Console.WriteLine(s); // s still equals "" here because s is passed to foo by value
}

void foo(String s) {
   bar(s);
   s = s + "foo";
   // s == "foo"
}

void bar(String s) {
   s = "bar";
}

What I need to do instead is return the strings and then concatenate them.

void main() {
   String s = foo();
   Console.WriteLine(s);
   // s == "barfoo"
}

String foo() {
   return bar() + "foo"
}

String bar() {
   return "bar";
}

Note in the second example, there is no need to pass the strings as parameters at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM