( Transfer from the previous answer for clarity )
If you are trying to return all the data for your Node<T> , I think the best way to do this is to override the ToString method in your Node<T> class as follows:
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 will result in the following:
Data: 54 Left: Data: 50 Left: null Right: null Right: Data: 53 Left: null Right: null
This is not the most beautiful implementation, but I think it proves the point.
For a more beautiful implementation see this answer
dav_i source share