summaryrefslogtreecommitdiff
path: root/pkg/rbtree/print_test.go
blob: c4154c830d8a44dd00ea17962eabe7ebaf061a4b (plain)
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
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+"     ")
}