// 构建一个简单的多叉树
// a
// / \
// b1 b2
// / | \ \
// c1 c2 c3 c4
// / \ / \
// d1 d2 d3 d4
需求:
删除掉所有conditions中operator=“set”的condition
//解决方案遍历
插入到链表中全部进行遍历:a b1 b2 c1 c2 c3 c4 d1 d2 d3 d4
type Condition struct {
operator string
data string
}
type Rule struct {
Id int32
name string
conditions []*Condition
child []*Rule
}
var rule Rule
consumeList := list.List{}
consumeList.PushBack(&rule) // 将顶级菜单的id入队列
for consumeList.Len() > 0 {
frontData := consumeList.Front()
curRule, ok := frontData.Value.(*Rule)
if !ok {
fmt.Println(status.Error(codes.Unknown, "规则队列解析异常"))
break
}
consumeList.Remove(frontData)
ConditionNew := make([]*Condition, 0)
for _, v := range curRule.conditions {
if v.operator == "set" {
continue
}
ConditionNew = append(ConditionNew, v)
}
curRule.conditions = ConditionNew //将去掉“set”的conditons重新赋值给当前的rule的conditions属性
for _, v := range curRule.child {
consumeList.PushBack(v)
}
}
遍历多叉树的所有数据
于 2025-03-31 16:33:24 首次发布