当前位置:网站首页>Go recursive infinite classification
Go recursive infinite classification
2022-06-12 03:02:00 【Give me a bottle of ice cream】
Recursive infinite classification
package main
import (
"encoding/json"
"io/ioutil"
"log"
)
type list struct {
ID int
Name string
TopObj *list
Pid int
Child []list
}
func handel(tree *list, v list){
if v.Pid == 0 {
v.TopObj = &list{
Name : tree.Name,
}
tree.Child = append(tree.Child, v)
}else{
if len(tree.Child) > 0 {
for key, value := range tree.Child {
if value.ID == v.Pid {
v.TopObj = &list{
Name: value.Name,
}
tree.Child[key].Child = append(tree.Child[key].Child, v)
}else{
handel(&tree.Child[key], v)
}
}
}
}
}
func main() {
a := []list{
{
ID:1,
Name:"A",
Pid:0,
},
{
ID:10,
Name:"Q",
Pid:0,
},
{
ID:2,
Name:"b",
Pid:1,
},
{
ID:3,
Name:"c",
Pid:0,
},
{
ID:4,
Name:"d",
Pid:3,
},
{
ID:11,
Name:"aa",
Pid:0,
},
{
ID:5,
Name:"E",
Pid:4,
},
{
ID:12,
Name:"BB",
Pid:11,
},
{
ID:13,
Name:"CC",
Pid:12,
},
{
ID:14,
Name:"DD",
Pid:13,
},
}
root := &list{
ID: 0,
Name: "/",
}
for _,v := range a {
handel(root, v)
}
jsonStr, err :=json.MarshalIndent(root," ","\t")
if err != nil {
log.Fatalln(err)
}
if err := ioutil.WriteFile("./tree.json", jsonStr, 0644); err != nil {
log.Fatalln(err)
}
}Generate a tree view
tree.json
{
"ID": 0,
"Name": "/",
"TopObj": null,
"Pid": 0,
"Child": [
{
"ID": 1,
"Name": "A",
"TopObj": {
"ID": 0,
"Name": "/",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 0,
"Child": [
{
"ID": 2,
"Name": "b",
"TopObj": {
"ID": 0,
"Name": "A",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 1,
"Child": null
}
]
},
{
"ID": 10,
"Name": "Q",
"TopObj": {
"ID": 0,
"Name": "/",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 0,
"Child": null
},
{
"ID": 3,
"Name": "c",
"TopObj": {
"ID": 0,
"Name": "/",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 0,
"Child": [
{
"ID": 4,
"Name": "d",
"TopObj": {
"ID": 0,
"Name": "c",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 3,
"Child": [
{
"ID": 5,
"Name": "E",
"TopObj": {
"ID": 0,
"Name": "d",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 4,
"Child": null
}
]
}
]
},
{
"ID": 11,
"Name": "aa",
"TopObj": {
"ID": 0,
"Name": "/",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 0,
"Child": [
{
"ID": 12,
"Name": "BB",
"TopObj": {
"ID": 0,
"Name": "aa",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 11,
"Child": [
{
"ID": 13,
"Name": "CC",
"TopObj": {
"ID": 0,
"Name": "BB",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 12,
"Child": [
{
"ID": 14,
"Name": "DD",
"TopObj": {
"ID": 0,
"Name": "CC",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 13,
"Child": null
}
]
}
]
}
]
}
]
}边栏推荐
- min25筛
- Unique paths for leetcode topic resolution
- A single quarter of educational technology revenue of 230million: a year-on-year decrease of 51% and a sharp narrowing of net loss
- Intel case
- Start ticwatch2
- oracle之用户和表空间
- RPC 入门
- Wechat applet project example - renju for two
- 利用ssh公钥传输文件
- Maya foreground rendering plug-in Mel scripting tool
猜你喜欢

Wechat applet project example - I have a paintbrush (painting)

cupp字典生成工具(同类工具还有crunch)

Intel case

Machine learning - dimensionality reduction (data compression, data visualization)

Graduation design of fire hydrant monitoring system --- thesis (add the most comprehensive hardware circuit design - > driver design - > Alibaba cloud Internet of things construction - > Android App D

tcp 三次握手与四次挥手

errno: -4091, syscall: ‘listen‘, address: ‘::‘, port: 8000

微信小程序项目实例——体质计算器

分數大小的比較

ssh公钥登录失败报错:sign_and_send_pubkey: no mutual signature supported
随机推荐
安科瑞抗晃电产品在河北某化工项目的应用
Selection (043) - which of the following values are false?
2020-12-17
Function templatesfunction templates
Requirements and business model innovation - Requirements 7- user requirements acquisition based on use case / scenario model
【点云压缩】Sparse Tensor-based Point Cloud Attribute Compression
Inverted string - two solutions
Cupp dictionary generation tool (similar tools include crunch)
Research Report on Chinese psoriasis drug market evaluation and investment direction (2022 Edition)
MySQL partition table create delete modify view
【点云压缩】Variational Image Compression with A Scale Hyperprior
Hypergraph tilted data is merged into root node and transferred to 3dfiles
Audio and video technology under the trend of full true Internet | Q recommendation
Cvpr2022 | IFS RCNN: an incremental small sample instance splitter
[digital signal processing] correlation function (power signal | cross correlation function of power signal | autocorrelation function of power signal)
Selection (046) - what is the output of the following code?
How to make div 100% page (not screen) height- How to make a div 100% of page (not screen) height?
Wechat applet project example - Fitness calculator
如何防止商場電氣火灾的發生?
Introduction to architecture - who moved my cake