Java recursive implementation of the JSON tree structure, with JS implementation tree structure: Child parent Node __js

Source: Internet
Author: User

Objective

A procedure or function has a method of calling itself directly or indirectly in its definition or description, it usually transforms a large and complex problem layer into a small scale problem similar to the original problem, and the recursive strategy can describe the multiple computations needed in the process of solving problems with only a small number of programs. Greatly reduces the amount of code in the program. The ability of recursion is to define an infinite set of objects with limited statements. In general, recursion requires boundary conditions, recursive forward segments, and recursive return segments. When the boundary condition is not satisfied, recursion advances, and when the boundary condition is satisfied, recursion returns.
Disadvantages of recursion:

Recursive algorithm is a relatively common algorithm for solving problems, such as ordinary circulation, low operating efficiency. Therefore, you should try to avoid using recursion unless you have a better algorithm or a particular situation where recursion is more appropriate. In the process of recursive call, the system opens up stacks for each layer's return point and local amount. Too many recursive times cause stack overflow and so on. ① Baidu Entry-recursion


1. Realize the effect

2. Implementation analysis

When we don't know how many children a node has, we have to loop through the query, where we use recursive implementations. When using recursion, pay special attention to the need to have an export, to avoid the death cycle.

A simple example that implements the factorial of N:

Public long factorial (long N) {//Seek n!
   if (n==0) return 1;
   else return n*factorial (n-1);//Use recursive n!=n* (n-1)! until 0
}


We can see clearly that 1. There is a judgment condition if (n==0) to be exported, 2. The method itself calls itself.

Code implementation of 3.java federated query database

First, the result of a database query is this:

Emon_no is the device itself, Parent_no on behalf of his father, if Parent_no no father, then is-1, represents the first level node.

The effect we want to achieve is:

We first query the first layer, that is, the device without the parent node, get their number:

You can then recursively query the child nodes in Java and encapsulate them in the map format.

The child's query is obtained by the father's number:

The first-level node is passed in to start the query until the child is not in the level of the node.

Public List Getinfoemonchild (list<eempinfoemon> eempinfoemonlist) {
        list<map> listmap = new ArrayList ( );
        Eempinfoemonmapper eempinfoemonmapper = Ctx.getbean (eempinfoemonmapper.class);
        for (Eempinfoemon eempinfoemon:eempinfoemonlist) {
            list<eempinfoemon> infoemonlist = Eempinfoemonmapper.resultquality1 (Eempinfoemon.getemonno ())//The number of the parent class into the query to change the father of the sub-class
            Map tempmap = new HashMap ();
            Tempmap.put ("id", Eempinfoemon.getemonno ());
            Tempmap.put ("Name", Eempinfoemon.getemonname ());
            if (Infoemonlist.size () >0) {//If the device has children, continue to do the query until the device has no children, that is, the last node
                tempmap.put ("Children", Getinfoemonchild ( infoemonlist));
            }
            Listmap.add (Tempmap);
        }
        return listmap;
    }


Finally, the Gson Tojson (LISTMAP) is converted to JOSN and returned to the front desk.

Front desk also do recursive processing, JS code is as follows:

Data returns the JSON data in the background var tablestr;
            function Reorgdata (data) {var orgdata = {Name:data.name, id:data.id
            }; if (Data.children && data.children.length>0) {//is used here is the function of nested multiple objects of $.extend ().

                Nesting multiple objects is a bit like an array-merging operation.
                $.extend (orgdata,{children:[]});

for (var i=0 i<data.children.length; i++) {var subdata = Reorgdata (Data.children[i]); <span style= "color: #33CC00;"  
                            > OPTION,TABLESTR is a global variable, and TABLESTR = Tablestr + "<tr>" is used in a successful callback. + "<td>" + data.children[i].id + "</td>" + "<td>" + data.id + "</ Td> "+" <td style= ' width:5em;
                            ></td> "+" <td> "+ data.children[i].name +" </td> " + "<td style= ' width:10em; ' ><label class= ' F-orange ' > Normal &Lt;/label><div class= ' uinn1 ui-btn-icon-right ui-icon-carat-r uw-app1 Next ' ></div></td></tr

> ";
                Putting the Subdata object into the Orgdata object Orgdata.children.push (Subdata);
                    }}///The success callback function is like this: success:function (data) {var dataarr=data.data;
                    Tablestr= "";
                        if (dataarr.length>0) {$ ("#hint"). Hide ();
                          for (Var i=0;i<dataarr.length;i++) {var dommain = document.getElementById (' main ' +i);
                          var datas = []; tablestr=tablestr+ "<tr><td>" +dataarr[i].id+ "</td><td></td>+ <td style= ' width : 5em; ' ></td> <td> "+dataarr[i].name+" </td><td style= ' width:10em; ><label class= ' f-orange ' > Normal </label><div class= ' uinn1 ' ui-btn-icon-right ui-icon-carat-r uw-app1 Next ' ></div></td>&Lt;/tr> "Datas.push (Reorgdata (dataarr[i)));
                    }}else{$ ("#hint"). Show ();
                    ///write the TABLESTR on the page $ ("#tb"). HTML (TABLESTR);                  
      CreateTable (); }


Finally, the results of the first picture are achieved:


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.