You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
570 B
28 lines
570 B
2 years ago
|
package pkg
|
||
|
|
||
|
type Node[T any] struct {
|
||
|
Data T
|
||
|
Children []Node[T]
|
||
|
}
|
||
|
|
||
|
// 适用 A00001B00001 层级结构
|
||
|
// parent 父级字符串
|
||
|
// length A00001 一个节点长度
|
||
|
// pathFunc 返回T的code A00001
|
||
|
func Tree[T any](data []T, parent string, length int, pathFunc func(d T) string) (res []Node[T]) {
|
||
|
for _, v := range data {
|
||
|
path := pathFunc(v)
|
||
|
if len(path) < length {
|
||
|
return nil
|
||
|
}
|
||
|
p := path[:len(path)-length]
|
||
|
if p == parent {
|
||
|
res = append(res, Node[T]{
|
||
|
Data: v,
|
||
|
Children: Tree[T](data, path, length, pathFunc),
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
return
|
||
|
}
|