Here is a modified version of this answer:
class BinaryTreeNode<T> {
BinaryTreeNode(this.value, {this.leftChild, this.rightChild});
T value;
BinaryTreeNode<T>? leftChild;
BinaryTreeNode<T>? rightChild;
@override
String toString() {
final out = StringBuffer();
rightChild?._buildTree(out, true, '');
out.writeln(value);
leftChild?._buildTree(out, false, '');
return out.toString();
}
void _buildTree(StringBuffer out, bool isRight, String indent) {
rightChild?._buildTree(out, true, indent + (isRight ? ' ' : '│ '));
out
..write(indent)
..write(isRight ? '┌─── ' : '└─── ')
..writeln(value);
leftChild?._buildTree(out, false, indent + (isRight ? '│ ' : ' '));
}
}
If you build a tree like so:
void main() {
final tree = BinaryTreeNode('D',
leftChild: BinaryTreeNode('A'),
rightChild: BinaryTreeNode(
'R',
leftChild: BinaryTreeNode('T'),
rightChild: BinaryTreeNode('Fun'),
));
print(tree);
}
The output looks like this:
┌─── Fun
┌─── R
│ └─── T
D
└─── A
Feel free to modify my answer to simply the code. I feel like the toString
method could be simplified to not repeat so much code.
Suggested solution from lrn
The following solution is more efficient since we are avoiding creating intermediate string objects as we go though the tree structure. Thanks to lrn for suggesting this approach:
class BinaryTreeNode<T> {
BinaryTreeNode(this.value, {this.leftChild, this.rightChild});
T value;
BinaryTreeNode<T>? leftChild;
BinaryTreeNode<T>? rightChild;
@override
String toString() {
final out = StringBuffer();
final indents = <String>[];
rightChild?._buildTree(out, true, indents);
out.writeln(value);
leftChild?._buildTree(out, false, indents);
return out.toString();
}
void _buildTree(StringBuffer out, bool isRight, List<String> indents) {
if (rightChild != null) {
indents.add(isRight ? ' ' : '│ ');
rightChild!._buildTree(out, true, indents);
indents.removeLast();
}
out
..writeAll(indents)
..write(isRight ? '┌─── ' : '└─── ')
..writeln(value);
if (leftChild != null) {
indents.add(isRight ? '│ ' : ' ');
leftChild!._buildTree(out, false, indents);
indents.removeLast();
}
}
}