Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Visualize-a-tree/Go/visualize-a-tree-1.go
Normal file
29
Task/Visualize-a-tree/Go/visualize-a-tree-1.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
Name string
|
||||
Children []*Node
|
||||
}
|
||||
|
||||
func main() {
|
||||
tree := &Node{"root", []*Node{
|
||||
&Node{"a", []*Node{
|
||||
&Node{"d", nil},
|
||||
&Node{"e", []*Node{
|
||||
&Node{"f", nil},
|
||||
}}}},
|
||||
&Node{"b", nil},
|
||||
&Node{"c", nil},
|
||||
}}
|
||||
b, err := json.MarshalIndent(tree, "", " ")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
31
Task/Visualize-a-tree/Go/visualize-a-tree-2.go
Normal file
31
Task/Visualize-a-tree/Go/visualize-a-tree-2.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
Name string
|
||||
Children []*Node
|
||||
}
|
||||
|
||||
func main() {
|
||||
tree := &Node{"root", []*Node{
|
||||
&Node{"a", []*Node{
|
||||
&Node{"d", nil},
|
||||
&Node{"e", []*Node{
|
||||
&Node{"f", nil},
|
||||
}}}},
|
||||
&Node{"b", nil},
|
||||
&Node{"c", nil},
|
||||
}}
|
||||
enc := toml.NewEncoder(os.Stdout)
|
||||
enc.Indent = " "
|
||||
err := enc.Encode(tree)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
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