遞歸把path字符串構造成遞歸數組


type PathEvaluationRsp_Module struct {
ModuleId int32 `protobuf:"varint,1,opt,name=moduleId,proto3" json:"moduleId"`
ModuleName string `protobuf:"bytes,2,opt,name=moduleName,proto3" json:"moduleName"`
Children []*PathEvaluationRsp_Module `protobuf:"bytes,5,rep,name=children,proto3" json:"children"`
}

//
做單元測試使用 func TestPath(pathMap []string) []*pb.PathEvaluationRsp_Module { var model *pb.PathEvaluationRsp_Module var models []*pb.PathEvaluationRsp_Module //var modeuleId = 1 for _, keys := range pathMap { array := strings.Split(keys, `\`) model = buildPathModule(array, 0, model) //判斷當前節點是否已經存在存在的話用最新的替代之前的 if !replaceModel(model, models) { models = append(models, model) } } return models } //判斷當前節點是否已經存在存在的話不添加不存在的話添加到上級節點的子節點 func replaceModel(model *pb.PathEvaluationRsp_Module, models []*pb.PathEvaluationRsp_Module) bool { for i := 0; i < len(models); i++ { if models[i].ModuleName == model.ModuleName { //跳出遞歸循環 if model.Children == nil { return true } if !replaceModel(model.Children[0], models[i].Children) { model.Children = append(model.Children, models[i].Children...) //這裏應該使用遞歸判斷 models[i] = model } return true } } return false } //第一個參數爲path 的array index 爲當前的 層級 爲index-1 models 爲當前的所有的結構 rsp childModel // 構建子集model 數組 func buildPathModule(array []string, index int, models *pb.PathEvaluationRsp_Module) *pb.PathEvaluationRsp_Module { //var childModel []*pb.PathEvaluationRsp_Module if index > len(array)-1 { return nil } if models == nil { model := &pb.PathEvaluationRsp_Module{ ModuleId: int32(index), ModuleName: array[index], } child := buildPathModule(array, index+1, models) if child != nil { model.Children = append(model.Children, child) } //childModel = append(childModel, model) return model } //判斷一下當前層級的節點是否已經添加 modelExist := checkExisting(index, 0, array[index], []*pb.PathEvaluationRsp_Module{models}) if modelExist == nil { model := &pb.PathEvaluationRsp_Module{ ModuleId: int32(index), ModuleName: array[index], } child := buildPathModule(array, index+1, models) if child != nil { model.Children = append(model.Children, child) } //childModel = append(childModel, model) return model } else { child := buildPathModule(array, index+1, models) if child != nil { //判斷當前節點的child是否含有當前節點 if !checkChildExisting(modelExist, child) { modelExist.Children = append(modelExist.Children, child) } } return modelExist } } //判斷當前節點的child是否含有當前節點 func checkChildExisting(modele, child *pb.PathEvaluationRsp_Module) bool { for i := 0; i < len(modele.Children); i++ { if modele.Children[i].ModuleName == child.ModuleName { return true } } return false } //index 爲層級 //判斷一下當前層級的節點是否已經添加 func checkExisting(index, seed int, pathName string, models []*pb.PathEvaluationRsp_Module) *pb.PathEvaluationRsp_Module { for i := 0; i < len(models); i++ { if seed != index { //不是一層就去下面一層找 modelExisting := checkExisting(index, seed+1, pathName, models[i].Children) if modelExisting != nil { return modelExisting } } else { if pathName == models[i].ModuleName { return models[i] } } } return nil }
//單元測試
func TestGetAnswerMap(t *testing.T) {
    mmodels := TestPath([]string{`AB\CD\D`, `AB\CD`, `AE\C`, `AD\DE\F`, `AD\DE\G`,`AB\CD\E\F`})
    fmt.Println(mmodels)
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章