Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
46
Task/Visualize-a-tree/Go/visualize-a-tree-3.go
Normal file
46
Task/Visualize-a-tree/Go/visualize-a-tree-3.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type tree []node
|
||||
|
||||
type node struct {
|
||||
label string
|
||||
children []int // indexes into tree
|
||||
}
|
||||
|
||||
func main() {
|
||||
vis(tree{
|
||||
0: node{"root", []int{1, 2, 3}},
|
||||
1: node{"ei", []int{4, 5}},
|
||||
2: node{"bee", nil},
|
||||
3: node{"si", nil},
|
||||
4: node{"dee", nil},
|
||||
5: node{"y", []int{6}},
|
||||
6: node{"eff", nil},
|
||||
})
|
||||
}
|
||||
|
||||
func vis(t tree) {
|
||||
if len(t) == 0 {
|
||||
fmt.Println("<empty>")
|
||||
return
|
||||
}
|
||||
var f func(int, string)
|
||||
f = func(n int, pre string) {
|
||||
ch := t[n].children
|
||||
if len(ch) == 0 {
|
||||
fmt.Println("╴", t[n].label)
|
||||
return
|
||||
}
|
||||
fmt.Println("┐", t[n].label)
|
||||
last := len(ch) - 1
|
||||
for _, ch := range ch[:last] {
|
||||
fmt.Print(pre, "├─")
|
||||
f(ch, pre+"│ ")
|
||||
}
|
||||
fmt.Print(pre, "└─")
|
||||
f(ch[last], pre+" ")
|
||||
}
|
||||
f(0, "")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue