Question

I am trying to traverse Binary search tree with the follwoing code and my recursion is just taking care of Right side if the tree, which I see why But what should I do to improve the logic here?

public void Print() {
    Console.WriteLine(this.Value + "  ");
    if (this.Right != null) {
        this.Right.Print();
    }
    if (this.Left != null) {
        this.Left.Print();
    }
    Console.Read();
}
Was it helpful?

Solution

The problem, I think, is that you are reading from the console at the end of the function. When the code gets to the bottom of the tree on the right, it executes Console.Read() and won't continue printing the rest of the tree until it gets something.

Eliminate that and it should print everything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top