Skip to content

Commit

Permalink
feat(tree): node map to node tree
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Nov 22, 2024
1 parent 72de44b commit 2b1387d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
9 changes: 9 additions & 0 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,12 @@ func intoTree[T cmp.Ordered, E NodeTree[T, E]](nodeMaps map[T]E, rows []E, rootP
}
return root
}

// Map implement Node map to NodeTree
func Map[I cmp.Ordered, E Node[I, U], U NodeTree[I, U]](rows []E) []U {
nodes := make([]U, 0, len(rows))
for _, v := range rows {
nodes = append(nodes, v.MapTree())
}
return nodes
}
7 changes: 1 addition & 6 deletions tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ func TestTree(t *testing.T) {
tree1, err := json.MarshalIndent(gotTree1, " ", " ")
require.NoError(t, err)

arrTree := make([]*DeptTree, 0, len(arr))
for _, v := range arr {
vv := *v
arrTree = append(arrTree, vv.MapTree())
}

arrTree := Map(arr)
gotTree2 := IntoTree2(arrTree, 0)
tree2, err := json.MarshalIndent(gotTree2, " ", " ")
require.NoError(t, err)
Expand Down

0 comments on commit 2b1387d

Please sign in to comment.