Angelrawzz
Angelrawzz

Reputation: 849

c# - Simple Binary Tree

So, I have been learning C# over the past month and at the moment I am struggling with Binary Trees.

My question is that How am I able to call my tree to the Console Window? I've Tried Console.WriteLine(tree.Data); But this seems to Write 54 to my Console Window.

Here is my code if you need to check it out:

Main File

static void Main(string[] args)
{
    //Creating the Nodes for the Tree
    Node<int> tree = new Node<int>('6');
    tree.Left = new Node<int>('2');
    tree.Right = new Node<int>('5');  

    Console.WriteLine("Binary Tree Display");
    Console.WriteLine(tree.Data);
    Console.ReadLine();
}

Node Class

class Node<T> where T : IComparable
{
    private T data;
    public Node<T> Left, Right;

    public Node(T item)
    {
        data = item;
        Left = null;
        Right = null;
    }
    public T Data
    {
        set { data = value; }
        get { return data; }
    }
}

Are there any other methods of calling my Tree? or am I doing something wrong?

Upvotes: 7

Views: 14164

Answers (4)

dav_i
dav_i

Reputation: 28107

(Moved from previous answer for clarity)

If you're trying to return all the data for your Node<T> I think a better way of going about it would be to override the ToString method in your Node<T> class like so:

public override string ToString()
{
    var leftString = this.Left != null ? this.Left.ToString() : "null";
    var rightString = this.Right != null ? this.Right.ToString() : "null";
    var dataString = this.Data != null ? this.Data.ToString() : "null";

    leftString = String.Join("\n", leftString.Split('\n').Select(a => "\t" + a));
    rightString = String.Join("\n", rightString.Split('\n').Select(a => "\t" + a));

    return String.Format("\nData: {0}\n"
                        + "Left: {1}\n"
                        + "Right: {2}",
                        dataString, leftString, rightString);
}

Then call Console.WriteLine(tree.ToString()); which results in the following:

Data: 54
Left:   
  Data: 50
  Left:   null
  Right:   null
Right:   
  Data: 53
  Left:   null
  Right:   null

This isn't the prettiest implementation but I think proves the point.

For a prettier implementation see this answer

Upvotes: 2

dav_i
dav_i

Reputation: 28107

The reason why it's just showing 54 is because that is the what (int)'6' is!

You're calling tree.Data which returns in this case '6' cast to int.


I imagine what you're trying to do is either return 6 which you could do by using

new Node<char>('6'); 

or by

new Node<int>(6);

(More in separate answer, removed for clarity)

Upvotes: 8

Alex F
Alex F

Reputation: 43311

Node<int> tree = new Node<int>(6);

6, and not '6'. Now expected value will be printed. Your code is silently casts char value '6' to integer, which gives result 54.

Upvotes: 2

Andrew Kogler
Andrew Kogler

Reputation: 51

I believe the best way to do this would be to implement a concise recursive tree traversal algorithm which prints out the value of each node in the particular order you choose to encounter them. As for there being a pre-written method to do so within the C# libraries, I am not aware of it. Best of luck!

Upvotes: 0

Related Questions