tree_test.go
2.8 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
package xcollection
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func prepare() []struct {
Input []TreeNode
Text string
Except []string
Except2 []string
} {
return []struct {
Input []TreeNode
Text string
Except []string
Except2 []string
}{
{
Input: []TreeNode{
&st{Id: 1, Pid: 0},
&st{Id: 2, Pid: 1}, &st{Id: 3, Pid: 1}, &st{Id: 4, Pid: 1},
&st{Id: 5, Pid: 3},
&st{Id: 6, Pid: 5}, &st{Id: 7, Pid: 5}},
Text: `
树形结构:
1
2 3 4
5
6 7
`,
Except: []string{"5", "6", "7"},
Except2: []string{"2", "4", "6", "7"},
},
}
}
func Test_Tree(t *testing.T) {
table := prepare()
for i := range table {
tree := NewTree(table[i].Input)
out := tree.AllChildNode(&st{Id: 5, Pid: 3})
var res []string = treeNodeResults(out)
assert.Equal(t, res, table[i].Except)
out = tree.AllLeafNode(nil) //tree.Root()
res = treeNodeResults(out)
assert.Equal(t, res, table[i].Except2)
root := tree.Root()
assert.Equal(t, root.ID(), "1")
//tree.Add(&st{Id:10,Pid: 7})
//
//out = tree.AllLeafNode(tree.Root())
//res = treeNodeResults(out)
//assert.Equal(t, res, []string{"2", "4", "6", "10"})
out = tree.TreeNodePaths(&st{Id: 7, Pid: 5})
res = treeNodeResults(out)
assert.Equal(t, res, []string{"1", "3", "5", "7"})
}
}
func Test_TreeNodeByDepth(t *testing.T) {
input := []TreeNode{
&st{Id: 1, Pid: 0},
&st{Id: 2, Pid: 1}, &st{Id: 3, Pid: 1}, &st{Id: 4, Pid: 1},
&st{Id: 5, Pid: 3},
&st{Id: 6, Pid: 5}, &st{Id: 7, Pid: 5},
&st{Id: 8, Pid: 6}, &st{Id: 9, Pid: 6}, &st{Id: 10, Pid: 6}, &st{Id: 11, Pid: 7}, &st{Id: 12, Pid: 7},
}
tree := NewTree(input)
/*
树形结构:
1
2 3 4
5
6 7
8 9 10 11 12
*/
var out []TreeNode
var res []string
out = tree.AllChildNodeByDepth(&st{Id: 5, Pid: 3}, 2)
res = treeNodeResults(out)
assert.Equal(t, []string{"6", "7"}, res)
out = tree.AllChildNodeByDepth(tree.Root(), 1)
res = treeNodeResults(out)
assert.Equal(t, []string{"1"}, res)
out = tree.AllChildNodeByDepth(tree.Root(), 2)
res = treeNodeResults(out)
assert.Equal(t, []string{"2", "3", "4"}, res)
out = tree.AllChildNodeByDepth(tree.Root(), 3)
res = treeNodeResults(out)
assert.Equal(t, []string{"5"}, res)
out = tree.AllChildNodeByDepth(tree.Root(), 4)
res = treeNodeResults(out)
assert.Equal(t, []string{"6", "7"}, res)
out = tree.AllChildNodeByDepth(tree.Root(), 5)
res = treeNodeResults(out)
assert.Equal(t, []string{"8", "9", "10", "11", "12"}, res)
}
type st struct {
Id int64
Pid int64
}
func (t *st) PID() int64 {
return t.Id
}
func (t *st) ID() int64 {
return t.Pid
}
func treeNodeResults(nodes []TreeNode) []string {
var res []string
for i := range nodes {
res = append(res, fmt.Sprintf("%d", nodes[i].ID()))
}
return res
}