1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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]) String() string {
switch {
case node == nil:
return "nil"
case node.Color == Red:
return fmt.Sprintf("R(%v)", node.Value)
default:
return fmt.Sprintf("B(%v)", node.Value)
}
}
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, "%s%v\n", m, node)
} else {
fmt.Fprintf(w, "%s%v\n", m, node)
}
node.Left.asciiArt(w, l+" | ", l+" `--", l+" ")
}
|