diff options
Diffstat (limited to 'pkg/rbtree/print_test.go')
-rw-r--r-- | pkg/rbtree/print_test.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/pkg/rbtree/print_test.go b/pkg/rbtree/print_test.go new file mode 100644 index 0000000..c4154c8 --- /dev/null +++ b/pkg/rbtree/print_test.go @@ -0,0 +1,30 @@ +package rbtree + +import ( + "fmt" + "io" + "strings" +) + +func (t *Tree[K, V]) ASCIIArt() string { + var out strings.Builder + t.root.asciiArt(&out, "", "", "") + return out.String() +} + +func (node *Node[V]) asciiArt(w io.Writer, u, m, l string) { + if node == nil { + fmt.Fprintf(w, "%snil\n", m) + return + } + + node.Right.asciiArt(w, u+" ", u+" ,--", u+" | ") + + if node.Color == Red { + fmt.Fprintf(w, "%sR(%v)\n", m, node.Value) + } else { + fmt.Fprintf(w, "%sB(%v)\n", m, node.Value) + } + + node.Left.asciiArt(w, l+" | ", l+" `--", l+" ") +} |