tree node weighted score

A tree has unlimited levels and can have different depths. As long as the weight and score of the leaf nodes are set, the score of each node can be obtained at one time. Of course, the weight of each node must also be set in advance:

 

/**

* Recursively calculate the score of the current node = the weighted score of the child nodes

* @param curr_folder_id

* @return

*/

public double calNodeScore(String curr_folder_id){

 

Dao dao = new DaoImp();

double gs = 0;//The weighted result of the current node, if the current node includes leaf and non-leaf nodes

try{

String pfkSQL = "select folder_id,isleaf,weight,score from estimate_card where status=1 and par_folder_id="+curr_folder_id;

List ll = dao.getResultList(pfkSQL);

 

for(int ii = 0;ii<ll.size();ii++){//Child nodes of the current node, including possible leaf nodes and non-leaf nodes

String child_folder_id = (String)((Map)ll.get(ii)).get("FOLDER_ID");

String isleaf = (String)((Map)ll.get(ii)).get("ISLEAF");

String weight = (String)((Map)ll.get(ii)).get("WEIGHT");

if("0".equals(isleaf)){

//The calculation result of the non-leaf child node of the current node

double ret = calNodeScore(child_folder_id);

//Also weighted with the results of sibling nodes

gs += (ret*Double.valueOf(weight));

}else{

//The leaf child node of the current node

double s = Double.valueOf((String)((Map)ll.get(ii)).get("SCORE"));

double w = Double.valueOf((String)((Map)ll.get(ii)).get("WEIGHT"));

gs += s*w;

}

}

//Update the weighted score of the current node

String update = "update estimate_card set score="+gs+" where folder_id="+curr_folder_id;

dao.update(update);

 

}catch(Exception e){

e.printStackTrace ();

}finally{

dao.closeconn();

}

return gs;

}

 

In addition, the algorithm requires the understanding of the actual business model, as well as the reference, and the actual rough logic.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988949&siteId=291194637