Java Recursive traversal tree structure _java

Source: Internet
Author: User

Nonsense not much said, directly to everyone to paste code, the specific code as follows:

Menu tree structure public jsonarray treemenulist (jsonarray menulist, int parentid) {Jsonarray childmenu = new Jsonarray (); 
For (object object:menulist) {Jsonobject Jsonmenu = Jsonobject.fromobject (object); 
int menuid = Jsonmenu.getint ("id"); 
int pid = Jsonmenu.getint ("ParentID"); 
if (parentid = = pid) {Jsonarray C_node = treemenulist (menulist, MENUID); 
Jsonmenu.put ("Childnode", C_node); 
Childmenu.add (Jsonmenu); 
} return Childmenu; 
public static void Main (String args[]) {Menucacheservice menucacheservice = new Menucacheservice (); 
Jsonarray Jsonarray = new Jsonarray (); 
Menu menu1 = new menu (); 
Menu1.setid (1l); 
Menu1.setparentid (0); 
Menu1.setlevel (0); 
Menu menu2 = new menu (); 
Menu2.setid (2l); 
Menu2.setparentid (0); 
Menu2.setlevel (0); 
Menu Menu3 = new menu (); 
Menu3.setid (3l); 
Menu3.setparentid (2); 
Menu3.setlevel (1); 
Menu Menu4 = new menu (); 
Menu4.setid (4l); 
Menu4.setparentid (2); 
Menu4.setlevel (1); 
Menu Menu5 = new menu (); 
Menu5.setid (5l); Menu5.setparentiD (4); 
Menu5.setlevel (2); 
Menu Menu6 = new menu (); 
Menu6.setid (6l); 
Menu6.setparentid (1); 
Menu6.setlevel (1); 
Jsonarray.add (MENU1); 
Jsonarray.add (MENU2); 
Jsonarray.add (MENU3); 
Jsonarray.add (MENU4); 
Jsonarray.add (MENU5); 
Jsonarray.add (MENU6); 
System.out.print (Menucacheservice.treemenulist (jsonarray,0)); }

Ps:java implements recursive traversal of the tree (for implementing the Collapse menu)

1. Core algorithm

Package Com.zf.tag.dao;
Import java.util.ArrayList;
Import java.util.List;
Import com.zf.tag.entity.dept.Dept; public class Deptdao extends basedao{public static list<dept> searchalldept () throws exception{ T
Listallobject=new arraylist<object[]> ();
List<object[]> tmp=new arraylist<object[]> ();
List<dept> listalldept=new arraylist<dept> ();
String sql= "Select Id,dname,up_did from Tbl_dept";
Listallobject=getresult (SQL); For (object[] row:listallobject) {object[] ro = new object[3]; ro[0] = row[0]; ro[1] = row[1]; ro[2] = row[2]; Tmp.add (
Row); 
For (object[] row:tmp) {if (null = = Row[2]) {Dept d = new Dept (); D.setid (Integer.parseint (string.valueof)));
D.setdname (string.valueof (row[1));
Listalldept.add (d);
Fomatedept (TMP, D);
} return listalldept; private static void Fomatedept (list<object[]> tmp, Dept Dept) {for (object[] row:tmp) {if ((null!= row[2)) &A mp;& Integer.parseint (string.valueof (row[2)) = = = Dept.getid ()) {Dept D = New Dept (); D.setid (Integer.parseint (string.valueof (row[0))); D.setdname (String.valueof (
ROW[1]));
list<dept> list = Dept.getchildren ();
if (list = = null) {list = new arraylist<dept> ();} list.add (d);
Dept.setchildren (list);
Fomatedept (TMP, D); }
}
}
}

2. Entity class (sector)

Package com.zf.tag.entity.dept;
Import java.util.List;
public class Dept {
private Integer ID;
Private String dname;
private list<dept> children;
Public Integer GetId () {return
ID;
}
public void SetId (Integer id) {
this.id = ID;
}
Public String Getdname () {return
dname;
}
public void Setdname (String dname) {
this.dname = dname;
}
Public list<dept> GetChildren () {return
children;
}
public void Setchildren (list<dept> children) {
This.children = children;
}
Public String toString () {return
this.getdname ();
}
}

The above is a small series to introduce the Java recursive traversal tree structure related content, I hope to help!

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.