-
Notifications
You must be signed in to change notification settings - Fork 10
/
treeprint.go
87 lines (72 loc) · 1.98 KB
/
treeprint.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package avltree
import (
"fmt"
"io"
)
// printData keeps information used while printing a tree.
type printData[T any] struct {
lineDepth int // how deep we are in the line
itemSize int // size of each node value
array []rune // line and spacing characters
index int // index into array
iter iterateFunc[T] // function to print the item
w io.Writer // writer to print to
}
// printer iterates through the tree to print it in graphical form.
func (d *printData[T]) printer(node *treeNode[T]) {
d.iter(node.value)
fmt.Fprintf(d.w, "-%03d", node.size+1)
if node.bal == equal {
fmt.Fprintf(d.w, "⮕")
} else if node.bal == leftHigh {
fmt.Fprintf(d.w, "⬆")
} else {
fmt.Fprintf(d.w, "⬇")
}
d.lineDepth += (d.itemSize + 5)
if node.left != nil || node.right != nil {
fmt.Fprintf(d.w, "━┳━")
if node.left != nil {
d.lineDepth += 3
d.array[d.index] = '┃'
d.index++
d.printer(node.left)
d.index--
d.lineDepth -= 3
} else {
fmt.Fprintf(d.w, "\n")
}
for i := 0; i < d.lineDepth; i++ {
if ((i + 2) % (d.itemSize + 3 + 5)) == 0 {
fmt.Fprintf(d.w, "%c", (d.array[((i+2)/(d.itemSize+3+5))-1]))
} else {
fmt.Fprintf(d.w, " ")
}
}
fmt.Fprintf(d.w, " ┗━")
if node.right != nil {
d.lineDepth += 3
d.array[d.index] = ' '
d.index++
d.printer(node.right)
d.index--
d.lineDepth -= 3
} else {
fmt.Fprintf(d.w, "\n")
}
} else {
fmt.Fprintf(d.w, "\n")
}
d.lineDepth -= (d.itemSize + 5)
}
// Print prints the values of the Tree to the given writer.
func Print[T any](t *Tree[T], w io.Writer, f func(T) bool, itemSiz int) {
fmt.Fprintf(w, "treeNode━┳━Left \t ⬆ Left High\n")
fmt.Fprintf(w, " ┃ \t ⮕ Equal\n")
fmt.Fprintf(w, " ┗━Right\t ⬇ Right High\n\n")
maxHeight := t.Height()
if f != nil && t.root != nil {
d := &printData[T]{0, itemSiz, make([]rune, maxHeight), 0, f, w}
d.printer(t.root)
}
}