3

I've written a quite primitive binary tree and it works just fine. The problem is, debugging it is a pain, actually seeing what each node contains and all their children and grandchildren is very tedious. Are there any visualizers so I can just get a tree representation of the data when I'm debugging it?

Overly Excessive
  • 2,095
  • 16
  • 31

1 Answers1

2

You can build "poor man's visualizer" by overriding ToString and producing a tree representation that humans can read, like this:

string ToString() {
    var leftSub = left != null ? left.ToString() : "-";
    var rightSub = right != null ? right.ToString() : "-";
    return string.Format("[{0}:{1},{2}]", data, leftSub, rightSub);
}

For example, for a binary tree that looks like this

      6
     / \
    /   9
   3
  /  \
 1    4

this code should produce this output:

[6:[3:[1:-,-],[4:-,-]],[9:-,-]]
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    If you cannot override ToString, consider using the DebugDisplayAttribute: http://msdn.microsoft.com/en-us/library/ms228992(v=vs.110).aspx which you could assign an arbitrary method to display the visual representation. – KevinS May 10 '14 at 11:47