当前位置:网站首页>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
}
]
}
]
}
]
}
]
}边栏推荐
- Kubernetes' learning path. Is there any "easy mode" Q recommendation for container hybrid cloud
- Selection (045) - what is the output of the following code?
- Audio and video technology under the trend of full true Internet | Q recommendation
- Inverted string - two solutions
- xml
- Application of ard3m motor protector in coal industry
- Demand and business model innovation - demand 9- prototype
- 大整数的加与乘;
- 【高代码文件格式API】道宁为您提供文件格式API集——Aspose,只需几行代码即可创建转换和操作100多种文件格式
- AI interview bag | Netease mutual entertainment AI Lab artificial intelligence research engineers share on both sides
猜你喜欢

One article to show you how to understand the harmonyos application on the shelves

In 2022, don't you know the difference between arrow function and ordinary function?

Cupp dictionary generation tool (similar tools include crunch)

Sparse tensor based point cloud attribute compression

Demand and business model innovation - demand 9- prototype

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

Inverted string - two solutions

Solutions to errors in ROM opening by MAME

微信小程序项目实例——双人五子棋

The road of global evolution of vivo global mall -- multilingual solution
随机推荐
Interpreting 2021 of middleware: after being reshaped by cloud nativity, it is more difficult to select models
安科瑞抗晃电产品在河北某化工项目的应用
One article to show you how to understand the harmonyos application on the shelves
WPS table learning notes - highlight duplicate values
golang的gin框架,各种接收参数的方式和各种绑定的区别?
Introduction to program environment and preprocessing C language (advanced level)
[digital signal processing] correlation function (periodic signal | autocorrelation function of periodic signal)
【点云压缩】Variational Image Compression with A Scale Hyperprior
微信小程序项目实例——双人五子棋
Oracle sequence
Research Report on Chinese psoriasis drug market evaluation and investment direction (2022 Edition)
In 2022, don't you know the difference between arrow function and ordinary function?
Apache simple honeypot
Inverted string - two solutions
Min25 sieve
The road of global evolution of vivo global mall -- multilingual solution
Application of acrelcloud-6000 secure power cloud platform in a commercial plaza
Cvpr2022 | IFS RCNN: an incremental small sample instance splitter
min25筛
2020-12-06