// You can edit this code!
// Click here and start typing.
package main
import (
"encoding/json"
"log"
)
//樹
type Tree struct {
List map[int]*Node
Children map[int]Node
Parents map[int]Node
}
//節點
type Node struct {
Id int `json:"id"`
Pid int `json:"pid"`
Name string `json:"name"`
Child []*Node `json:"child"`
}
//將原始數據創建樹結構
func (this *Tree) BuildTree(nodes []Node) {
this.List = make(map[int]*Node, 0)
bs, _ := json.Marshal(nodes)
log.Println("nodes:", string(bs))
for index, _ := range nodes {
id := nodes[index].Id
nodes[index].Child = make([]*Node, 0)
this.List[id] = &nodes[index]
}
log.Println("list:", this.List)
for k, _ := range this.List {
pid := this.List[k].Pid
if _, ok := this.List[pid]; ok {
this.List[pid].Child = append(this.List[pid].Child, this.List[k])
}
}
//取以節點1展開的樹
for k, _ := range this.List {
if this.List[k].Id > 1 {
delete(this.List, k)
}
}
}
//GetAllNode ... 獲取所有子節點
func GetAllNode(node *Node) (nodes []string) {
if len(node.Child) == 0 {
nodes = append(nodes, node.Name)
return nodes
}
for _, t := range node.Child {
for _, n := range GetAllNode(t) {
nodes = append(nodes, n)
}
}
return nodes
}
func main() {
//原始數據格式 目前支持轉成該種方式 [{"id":1,"name":"集團總部","pid":0},{"id":3,"name":"三體集團","pid":1},{"id":2,"name":"三體有限公司","pid":1},{"id":4,"name":"有限本部","pid":2},{"id":5,"name":"集團本部","pid":3},{"id":6,"name":"三體防禦","pid":2}]
menus := []byte(`[{"id":1,"name":"集團總部","pid":0},{"id":3,"name":"三體集團","pid":1},{"id":2,"name":"三體有限公司","pid":1},
{"id":4,"name":"有限本部","pid":2},{"id":5,"name":"集團本部","pid":3},{"id":6,"name":"三體防禦","pid":2}]
`)
TestNode(menus)
}
//標準格式 樹生成 需要轉成標準格式字段
func TestNode(menus []byte) {
var nodes []Node
err := json.Unmarshal(menus, &nodes)
if err != nil {
log.Fatal("JSON decode error:", err)
return
}
//構建樹
var exampleTree Tree
exampleTree.BuildTree(nodes)
bs, _ := json.Marshal(exampleTree.List)
log.Println("tree:", string(bs))
//獲取節點1的所有子節點
n := GetAllNode(exampleTree.List[1])
log.Println("n:", n)
}
數組轉樹