The design and implementation of the simple and common permission system (III): Maintain and utilize the depth level of the node to construct the tree iteratively

Source: Internet
Author: User

If you add a level attribute to the node's properties, that is, the depth of the tree, the construction tree will be very easy. The premise is to maintain level when adding and modifying nodes.

    root node is 1, the next class is 2, and so on.

  the method of constructing a tree, there are 2 main:

Sort by level, root node is on, child node is next

public static list<map<string, object>> Buildtree (list<treenode> List) {list<map<string, object>> rootlist = new arraylist<map<string, object>> (); Map<string, map<string, object>> rootmap = new hashmap<string, map<string, Object>> (); for ( TreeNode node:list) {integer ACL = node.getacl (); Integer parentacl = Node.getparentacl (); map<string, object> newNode = CreateNode (ACL); Rootmap.put (ACL + "", newNode); if (Parentacl.equals (-1)) { Rootlist.add (NewNode);} else {map<string, object> Fathernode = rootmap.get (Parentacl + ""); AddChild (Fathernode, NewNode);}} return rootlist;} private static void AddChild (Map<string, object> father,map<string, object> Child) {//if father=null, The IF (father = = null) {System.out.println ("Error,father is null") must not be correctly sorted by level, and return; Object o = father.get ("Children"); List<map<string, object>> childs;if (o = = null) {childs = new arraylist<map<string, object>> (); Father.put ("Children", Childs);} else {childs = (list<map<string, object>>) o;} Childs.add (child);}

Just a few simple points to say:
1. In ascending order, the top node is at the front.
2. Place the parent nodes of each node first in the map. When you turn to a child node, put it in the children of the parent node.
When comparing 3.integer, use the Queals method instead of = =.

The 4.Map key is of a differentiated type.

Map.put (1, "a");

Map.get ("1");

The value that is put in is probably different from the value taken out.

The key of the map is not the same, the integer 1 and the string "1" are different.
The parent node in 5.addChild cannot be null. If it does, it must not be sorted correctly by level.

This way of building a tree through level, the advantage is that the structure is very simple, the downside is that you have to maintain level, trouble.

The design and implementation of the simple and common permission system (III): Maintain and utilize the depth level of the node to construct the tree iteratively

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.