golang list to tree

// 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)

}

数组转树

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值