tree.go
2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package domain
import (
"fmt"
"strconv"
)
const (
StructTree = "tree"
StructList = "list"
)
type TreeNode interface {
PID() string
ID() string
}
type Tree struct {
Node TreeNode `json:"node"`
Nodes []*Tree `json:"nodes"`
}
func NewTrees(nodes []TreeNode) *Tree {
var tree = &Tree{
Node: nil,
Nodes: make([]*Tree, 0),
}
for i := range nodes {
match := traverse(tree, nodes[i])
if !match {
tree.Nodes = append(tree.Nodes, NewTree(nodes[i]))
}
}
return tree
}
func NewTree(node TreeNode) *Tree {
return &Tree{
Node: node,
Nodes: make([]*Tree, 0),
}
}
// AllChildNodes 返回node下所有子节点,包含本身
func (tree *Tree) AllChildNodes(node TreeNode) []TreeNode {
treeNode := tree.find(node)
if treeNode == nil {
return []TreeNode{}
}
return tree.allChild(treeNode)
}
// find 查询node所在的tree,并且返回
func (tree *Tree) find(node TreeNode) *Tree {
var stack []*Tree
stack = append(stack, tree)
var find *Tree
for {
if len(stack) == 0 {
break
}
pop := stack[0]
stack = stack[1:]
stack = append(stack, pop.Nodes...)
if pop == nil || pop.Node == nil {
continue
}
if pop.Node.ID() == node.ID() {
find = pop
break
}
}
return find
}
// allChild 返回treeNode下所有子节点
func (tree *Tree) allChild(treeNode *Tree) []TreeNode {
var stack []*Tree
stack = append(stack, treeNode)
var res []TreeNode
for {
if len(stack) == 0 {
break
}
pop := stack[0]
stack = stack[1:]
stack = append(stack, pop.Nodes...)
res = append(res, pop.Node)
}
return res
}
// traverse 遍历节点
//
// tree 当前树
// node 判断的节点
func traverse(tree *Tree, node TreeNode) bool {
list := tree.Nodes
var match bool = false
for i := range list {
id, pid := list[i].Node.ID(), node.PID() //list[i].Node.PID() == node.ID()
if pid == id {
list[i].Nodes = append(list[i].Nodes, NewTree(node))
return true
}
if match || traverse(list[i], node) {
match = true
break
}
}
return match
}
// Int64String 1 -> "1" 1->1
type Int64String int64
func (t Int64String) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf(`"%d"`, t)
return []byte(stamp), nil
}
func (t *Int64String) UnmarshalJSON(data []byte) error {
v, err := strconv.ParseInt(string(data), 10, 64)
*t = Int64String(v)
return err
}
// Int64String 1 -> "1" "1"->1
type StringInt64 int64
func (t StringInt64) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf(`"%d"`, t)
return []byte(stamp), nil
}
func (t *StringInt64) UnmarshalJSON(data []byte) error {
if len(data) < 2 {
*t = 0
return fmt.Errorf("字符数字格式有误:" + string(data))
}
data = data[1 : len(data)-1]
v, err := strconv.ParseInt(string(data), 10, 64)
*t = StringInt64(v)
return err
}