-
Notifications
You must be signed in to change notification settings - Fork 0
/
treeprint.go
204 lines (201 loc) · 5.75 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package treeprint
import (
"fmt"
"reflect"
"strings"
)
// TreeNode binary tree interface
type TreeNode interface {
GetKey() interface{}
GetValue() interface{}
RangeNode() chan TreeNode
}
// Sprint get print text
func Sprint(entryNode TreeNode) string {
rt := reflect.TypeOf(entryNode)
rv := reflect.ValueOf(entryNode)
if rt.Kind() != reflect.Ptr {
return "<error: entryNode has to be a node pointer>"
} else if rv.IsNil() {
return "<nil>"
}
type nodeInfo struct {
node TreeNode
layer int
count int
index int
len int
str string
str2 string
hasBar bool
segLen map[int]int
parentNode *nodeInfo
leafNode []*nodeInfo
}
layer := make([][]*nodeInfo, 0)
entryNodeInfo := &nodeInfo{
node: entryNode,
layer: 1,
count: 1,
str: fmt.Sprintf("%v%v", entryNode.GetKey(), entryNode.GetValue()),
segLen: make(map[int]int),
}
entryNodeInfo.len = len(entryNodeInfo.str)
for i := 0; i < entryNodeInfo.len; i++ {
entryNodeInfo.str2 += " "
}
queue := []*nodeInfo{entryNodeInfo}
currentLayer := 2
currentLayerIndex := 0
currentLayerCount := 1
i := 0
for {
if i > len(queue)-1 {
break
}
current := queue[i]
leafIndex := 0
for currentLeafNode := range current.node.RangeNode() {
if reflect.ValueOf(currentLeafNode).IsNil() {
current.leafNode = append(current.leafNode, nil)
} else {
currentLeafNodeInfo := &nodeInfo{
node: currentLeafNode,
layer: current.layer + 1,
str: fmt.Sprintf("%v%v", currentLeafNode.GetKey(), currentLeafNode.GetValue()),
segLen: make(map[int]int),
parentNode: current,
}
currentLeafNodeInfo.len = len(currentLeafNodeInfo.str)
currentLeafNodeInfo.segLen = map[int]int{ 1: currentLeafNodeInfo.len }
for j := 0; j < currentLeafNodeInfo.len; j++ {
currentLeafNodeInfo.str2 += " "
}
if currentLeafNodeInfo.layer != currentLayer {
currentLayer = currentLeafNodeInfo.layer
currentLayerIndex = 0
currentLayerCount = 1
}
currentLeafNodeInfo.count = currentLayerCount
currentLayerCount++
currentLeafNodeInfo.index = currentLayerIndex
currentLayerIndex += currentLeafNodeInfo.len + 1
queue = append(queue, currentLeafNodeInfo)
current.leafNode = append(current.leafNode, currentLeafNodeInfo)
current.segLen[currentLeafNodeInfo.count] = current.len
}
leafIndex++
}
for ; leafIndex < 2; leafIndex++ {
current.leafNode = append(current.leafNode, nil)
}
if current.layer > len(layer) {
layer = append(layer, make([]*nodeInfo, 0))
}
layer[current.layer-1] = append(layer[current.layer-1], current)
i++
}
var alignFirst func(*nodeInfo)
var alignOther func(*nodeInfo)
alignFirst = func(current *nodeInfo) {
if current.leafNode[0] == nil {
return
} else if val := current.index - current.leafNode[0].index; val > 0 { // 下一层移位
for k := current.leafNode[0].count - 1; k < len(layer[current.layer]); k++ {
layer[current.layer][k].index += val
}
} else if val < 0 { // 本层移位
for k := current.count - 1; k < len(layer[current.layer-1]); k++ {
layer[current.layer-1][k].index += -val
}
}
for j := current.leafNode[0].count - 1; j < len(layer[current.layer]); j++ {
alignFirst(layer[current.layer][j])
alignOther(layer[current.layer][j])
}
}
alignOther = func(current *nodeInfo) {
lastLeafIndex := len(current.leafNode) - 1
for i := 1; i <= lastLeafIndex; i++ {
if current.leafNode[i] == nil {
continue
} else if !current.hasBar {
if val := current.leafNode[i].index - current.index - current.len; val > 0 {
for k := 0; k < val; k++ {
current.str += "-"
current.str2 += " "
}
current.str += "+"
current.str2 += "|"
newLen := len(current.str)
offset := newLen - current.len
current.len = newLen
current.segLen[current.leafNode[i].count] = current.len
for k := current.count; k < len(layer[current.layer-1]); k++ {
layer[current.layer-1][k].index += offset
}
} else {
current.str += "-+"
current.str2 += " |"
newLen := len(current.str)
offset := newLen - current.len
current.len = newLen
current.segLen[current.leafNode[i].count] = current.len
for k := current.count; k < len(layer[current.layer-1]); k++ {
layer[current.layer-1][k].index += offset
}
offset2 := -val + 1
for k := current.leafNode[i].count - 1; k < len(layer[current.layer]); k++ {
layer[current.layer][k].index += offset2
}
}
if i == lastLeafIndex {
current.hasBar = true
}
} else {
parentIndex := current.index + current.segLen[current.leafNode[i].count] - 1
selfIndex := current.leafNode[i].index
gap := parentIndex - selfIndex
for k := current.leafNode[i].count - 1; k < len(layer[current.layer]); k++ {
layer[current.layer][k].index += gap
}
}
for j := current.leafNode[i].count - 1; j < len(layer[current.layer]); j++ {
alignFirst(layer[current.layer][j])
alignOther(layer[current.layer][j])
}
}
}
for i := len(layer) - 2; i >= 0; i-- {
for j := 0; j < len(layer[i]); j++ {
alignFirst(layer[i][j])
alignOther(layer[i][j])
}
}
text := ""
for i := 0; i < len(layer); i++ {
line1 := ""
line2 := ""
for j := 0; j < len(layer[i]); j++ {
if val := layer[i][j].index - len(line1); val > 0 {
for k := 0; k < val; k++ {
line1 += " "
line2 += " "
}
}
line1 += layer[i][j].str
if layer[i][j].leafNode[0] != nil {
line2 += "|"
} else {
line2 += " "
}
line2 += layer[i][j].str2[1:]
}
line1 = strings.ReplaceAll(line1, "-+-", "─┬─")
line1 = strings.ReplaceAll(line1, "-+", "─┐")
line1 = strings.ReplaceAll(line1, "-", "─")
text += line1 + "\n"
text += line2 + "\n"
}
return text
}