Business scenarios:
When an institution querys department information, it hopes to return a nested format of a tree-like structure;
Solution:
Nested into corresponding structures through recursion and pointers;
I borrowed the code from my predecessors, but in the end, the recursive pointer call was debugged for a long time before I came out. Here is a complete sample code.
package main import ( "fmt" "encoding/json" ) type dept struct { DeptId string `json:"deptId"` FrameDeptStr string `json:"frameDeptStr"` Child []*dept `json:"child"` } func main() { depts := make([]dept,0) var a dept = "1" = "" depts = append(depts,a) ="3" = "1" depts = append(depts,a) ="4" = "1" depts = append(depts,a) ="5" = "13" depts = append(depts,a) ="6" = "13" depts = append(depts,a) (depts) deptRoots := make([]dept,0) for _,v := range depts{ if == ""{ deptRoots= append(deptRoots,v) } } pdepts := make([]*dept,0) for i,_ := range depts{ var a *dept a = &depts[i] pdepts = append(pdepts,a) } //Acquired the department on the root ("The department on the root has:",deptRoots) var node *dept node = &depts[0] makeTree(pdepts,node) ("the result we got is",pdepts) data, _ := (node) ("%s", data) } func has(v1 dept,vs []*dept) bool { var has bool has = false for _,v2 := range vs { v3 := *v2 if + == { has = true break } } return has } func makeTree(vs []*dept,node *dept) { ("the node value in maketree is:",*node) childs := findChild(node,vs) (" the child we got is :",childs) for _,child := range childs{ ("in the childs's for loop, the child's address here is:",&child) = append(,child) ("in the child's for loop, after append the child is:",child) if has(*child,vs) { ("i am in if has") ("the child in if has is:",*child) ("the child in if has 's address is:",child) makeTree(vs,child) } } } func findChild(v *dept,vs []*dept)(ret []*dept) { for _,v2 := range vs{ if + == { ret= append(ret,v2) } } return }
Code Notes:
To determine the relationship between departments through frame_dept_str (a.frame_dept_str= a's parent's frame_dept_str + a's parent's dept_id).
Supplement: Three ways to traverse the tree structure of golang
Look at the code ~
package main import "log" type node struct { Item string Left *node Right *node } type bst struct { root *node } /* m kl h i a b c d e f //Precise traversal (root left and right): m k h a b i c d l j e f //In-order traversal (left root right): a h b k c i d m l e j f //Post-order traversal (left and right root): a b h c d i k e f j l m */ func (tree *bst) buildTree() { m := &node{Item: "m"} = m k := &node{Item: "k"} l := &node{Item: "l"} = k = l h := &node{Item: "h"} i := &node{Item: "i"} = h = i a := &node{Item: "a"} b := &node{Item: "b"} = a = b c := &node{Item: "c"} d := &node{Item: "d"} = c = d j := &node{Item: "j"} = j e := &node{Item: "e"} f := &node{Item: "f"} = e = f } //Precise traversalfunc (tree *bst) inOrder() { var inner func(n *node) inner = func(n *node) { if n == nil { return } () inner() inner() } inner() } //Medium orderfunc (tree *bst) midOrder() { var inner func(n *node) inner = func(n *node) { if n == nil { return } inner() () inner() } inner() } //Post sequencefunc (tree *bst) lastOrder() { var inner func(n *node) inner = func(n *node) { if n == nil { return } inner() inner() () } inner() } func main() { tree := &bst{} () // () () }
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.