你可以尝试这样的事情(运行GroovyConsole
)
//the list of nodes
def nodes = ['A','B','C','D']
//the map of children for each node(children are in lists)
def children = [A:['B', 'C'],B:['C'],C:['D']]
//the map for the rankings
def ranking = [:]
//define closure first so it can be called recursively
def calculate
calculate = {
if(children.containsKey(it)){
//if key is in children map it has children -> at least rank 1
int rank = 1
//get children ranks and put it con collection
def childrenRanks = children[(it)].collect{calculate(it)}
//add max children rank to parent rank and return
return rank + childrenRanks.max()
}else{
//if key is not on children map is leaf so rank 0
return 0
}
}
nodes.each{
ranking[it] = 100 //fixed value
ranking[it] += calculate(it)
}
println ranking