Project development-Number statistics in a tree hierarchy

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.