Development of the project. When you have a menu with a tree structure, you need to count the total number of records under each menu. It is generally assumed that the depth of the tree structure is 2 (only the two-level menu), and there is no problem with the For loop statistics. However, assuming that the depth of the tree menu is unlimited, it cannot be achieved by two for loops, only with recursive statistics, using a global map to record the value of each statistic. Simple implementations such as the following:
element with hierarchy source class, omit Getter/setter:
public class Source {private source parent;private string Id;private string name;private list<source> children;}
Statistical methods. Just need the root element of the hierarchy, you can complete the statistics of the individual menu sub-elements:
Import Java.util.list;import Java.util.map;public class Sourcecountutil {/** * tree structure The statistical value of a node = total number of children + child's total number of children * @param root * @param result * @return */public static int docount (Source root,map<string,object> result) {int count = 0; list<source> list = Root.getchildren (); if (List==null | | List.size () ==0) {return count;} for (Source child:list) {///counts the number of child nodes of the current element count++;//the total number of children of the child node int cur_cnt=docount (Child,result); Result.put ( String.valueof (Child.getid ()), cur_cnt); count + = cur_cnt;} Returns the number of statistics for the current node before returning Result.put (String.valueof (Root.getid ()), count); return count;}}
Each time you use recursion to count the number of child nodes in a menu, you deposit it into map. The result of the last output stores the number of child elements of the entire menu.
Dtree.js implementation of the page menu display is also completed by recursion. Combined with the above statistics, we can get the following menu statistic effect:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd29qaxvzagl3bzk0nxlvdq==/font/5a6l5l2t/fontsize/400/fill/ I0jbqkfcma==/dissolve/70/gravity/center ">
Project development-Number statistics in tree hierarchies