1. Golang implementation
package main

import (
	"encoding/json"
	"fmt"
)

type Node struct {
	Name     string `json:"name"`
	Id       int32  `json:"id"`
	Pid      int32  `json:"pid"`
	Children []Node `json:"children"`
}

func main(a) {
	nodes := []Node{
		{Id: 1, Pid: 0, Name: "Class 1"},
		{Id: 2, Pid: 0, Name: "Class 2"},
		{Id: 3, Pid: 0, Name: "Category 3"},
		{Id: 4, Pid: 1, Name: "Category 1-1"},
		{Id: 5, Pid: 1, Name: 1-2 "category"},
		{Id: 6, Pid: 1, Name: "Category 1-3"},
		{Id: 7, Pid: 6, Name: The 1-3-1 "category"},
	}
	tree := CreateTree(nodes)
	bytes, _ := json.Marshal(tree)
	fmt.Println(string(bytes))
}

func CreateTree(nodes []Node) []Node {
	tree := make([]Node, 0)
	var roots, children []Node
	for _, v := range nodes {
		// define pid==0 as the root node
		if v.Pid == 0 {
			roots = append(roots, v)
		} else {
			children = append(children, v)
		}
	}
	for _, v := range roots {
		rootNode := &Node{
			Id:   v.Id,
			Pid:  v.Pid,
			Name: v.Name,
		}
		Recursion(rootNode, children)
		tree = append(tree,*rootNode)
	}
	return tree
}

func Recursion(rootNode *Node, children []Node) {
	id := rootNode.Id
	for _, v := range children {
		if id == v.Pid {
			childNode := &Node{
				Id:   v.Id,
				Pid:  v.Pid,
				Name: v.Name,
			}
			Recursion(childNode, children)
			rootNode.Children = append(rootNode.Children, *childNode)
		}

	}
}
Copy the code
2. Java implementation
package com.test;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;


public class Test {

    public static void main(String[] args) {
        List<Node> nodes = new ArrayList<>();
        nodes.add(new Node(1.0."Class 1"));
        nodes.add(new Node(2.0."Class 2"));
        nodes.add(new Node(3.0."Category 3"));
        nodes.add(new Node(4.1."Category 1-1"));
        nodes.add(new Node(5.1.1-2 "category"));
        nodes.add(new Node(6.1."Category 1-3"));
        nodes.add(new Node(7.6.The 1-3-1 "category"));
        List<Node> tree = createTree(nodes);
        Object json = JSON.toJSON(tree);
        System.out.println(json);
    }

    public static List<Node> createTree(List<Node> nodes) {
        List<Node> tree = new ArrayList<>();
        List<Node> roots = new ArrayList<>();
        List<Node> children = new ArrayList<>();
        for (Node node : nodes) {
            // define pid==0 as the root node
            if (0 == node.getPid()) {
                roots.add(node);
            } else{ children.add(node); }}for (Node node : roots) {
            recursion(node, children);
            tree.add(node);
        }
        return tree;
    }

    public static void recursion(Node rootNode, List<Node> children) {
        int id = rootNode.getId();
        for (Node node : children) {
            if (id == node.getPid()) {
                List<Node> child = rootNode.getChildren();
                if (child == null) {
                    List<Node> list = new ArrayList<>();
                    list.add(node);
                    rootNode.setChildren(list);
                } else{ rootNode.getChildren().add(node); } recursion(node, children); }}}}class Node {
    private String name;
    private int id;
    private int pid;
    private List<Node> children;
    
    public Node(int id, int pid, String name) {
        this.name = name;
        this.id = id;
        this.pid = pid;
    }
    public String getName(a) {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId(a) {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getPid(a) {
        return pid;
    }
    public void setPid(int pid) {
        this.pid = pid;
    }
    public List<Node> getChildren(a) {
        return children;
    }
    public void setChildren(List<Node> children) {
        this.children = children; }}Copy the code
Results 3.