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?
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:-,-]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With