1.golang实现
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() {
nodes := []Node{
{Id: 1, Pid: 0, Name: "类别1"},
{Id: 2, Pid: 0, Name: "类别2"},
{Id: 3, Pid: 0, Name: "类别3"},
{Id: 4, Pid: 1, Name: "类别1-1"},
{Id: 5, Pid: 1, Name: "类别1-2"},
{Id: 6, Pid: 1, Name: "类别1-3"},
{Id: 7, Pid: 6, Name: "类别1-3-1"},
}
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 {
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)
}
}
}
2.java实现
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, "类别1"));
nodes.add(new Node(2, 0, "类别2"));
nodes.add(new Node(3, 0, "类别3"));
nodes.add(new Node(4, 1, "类别1-1"));
nodes.add(new Node(5, 1, "类别1-2"));
nodes.add(new Node(6, 1, "类别1-3"));
nodes.add(new Node(7, 6, "类别1-3-1"));
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) {
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() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public List<Node> getChildren() {
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}
}
3.结果
