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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| package tree
import "sort"
type Tree struct { Title string `json:"title"` Data interface{} `json:"data"` Leaf bool `json:"leaf"` Selected bool `json:"checked"` PartialSelected bool `json:"partial_selected"` Children []Tree `json:"children"` }
type INode interface { GetTitle() string GetId() int GetFatherId() int GetData() interface{} IsRoot() bool } type INodes []INode
func (nodes INodes) Len() int { return len(nodes) } func (nodes INodes) Swap(i, j int) { nodes[i], nodes[j] = nodes[j], nodes[i] } func (nodes INodes) Less(i, j int) bool { return nodes[i].GetId() < nodes[j].GetId() }
func GenerateTree(nodes, selectedNodes []INode) (trees []Tree) { trees = []Tree{} var roots, childs []INode for _, v := range nodes { if v.IsRoot() { roots = append(roots, v) } childs = append(childs, v) }
for _, v := range roots { childTree := &Tree{ Title: v.GetTitle(), Data: v.GetData(), } childTree.Selected = nodeSelected(v, selectedNodes, childTree.Children) recursiveTree(childTree, childs, selectedNodes) if !childTree.Selected { childTree.Selected = nodeSelected(v, selectedNodes, childTree.Children) } childTree.PartialSelected = nodePartialSelected(childTree.Children) childTree.Leaf = len(childTree.Children) == 0 trees = append(trees, *childTree) } return }
func recursiveTree(tree *Tree, nodes, selectedNodes []INode) { data := tree.Data.(INode)
for _, v := range nodes { if v.IsRoot() { continue } if data.GetId() == v.GetFatherId() { childTree := &Tree{ Title: v.GetTitle(), Data: v.GetData(), } childTree.Selected = nodeSelected(v, selectedNodes, childTree.Children) || tree.Selected recursiveTree(childTree, nodes, selectedNodes)
if !childTree.Selected { childTree.Selected = nodeSelected(v, selectedNodes, childTree.Children) } childTree.PartialSelected = nodePartialSelected(childTree.Children) childTree.Leaf = len(childTree.Children) == 0 tree.Children = append(tree.Children, *childTree) } } }
func FindRelationNode(nodes, allNodes []INode) (respNodes []INode) { nodeMap := make(map[int]INode) for _, v := range nodes { recursiveFindRelationNode(nodeMap, allNodes, v, 0) }
for _, v := range nodeMap { respNodes = append(respNodes, v) } sort.Sort(INodes(respNodes)) return }
func recursiveFindRelationNode(nodeMap map[int]INode, allNodes []INode, node INode, t int) { nodeMap[node.GetId()] = node for _, v := range allNodes { if _, ok := nodeMap[v.GetId()]; ok { continue } if t == 0 || t == 1 { if node.GetFatherId() == v.GetId() { nodeMap[v.GetId()] = v if v.IsRoot() { continue } recursiveFindRelationNode(nodeMap, allNodes, v, 1) } } if t == 0 || t == 2 { if node.GetId() == v.GetFatherId() { nodeMap[v.GetId()] = v recursiveFindRelationNode(nodeMap, allNodes, v, 2) } } } }
func nodeSelected(node INode, selectedNodes []INode, children []Tree) bool { for _, v := range selectedNodes { if node.GetId() == v.GetId() { return true } }
if len(children) == 0 { return false } selectedNum := 0 for _, v := range children { if v.Selected { selectedNum++ } } if selectedNum == len(children) { return true } return false }
func nodePartialSelected(trees []Tree) bool { selectedNum := 0 for _, v := range trees { if v.Selected { selectedNum++ } } if selectedNum == len(trees) || selectedNum == 0 { return false } return true }
|