This is a relatively simple infinite-pole classification, suitable for a variety of programming languages.
This principle is a one-time database of all categories read out and then according to the PID to sort.
Relatively simple, good understanding.
public static list<category> cates;private static list<category> Tmpcates = new arraylist<category> ();p Ublic static void main (String[] args) { init (); list<category> clist = tree ( cates , 0 , 0 ); for (category c: CList) { StringBuffer sb = new StringBuffer (); for (int i = 0; i < c.getlevel (); i++) { sb.append ("---"); } system.out.println ( sb.tostring () + c.getname () ); }} Public static list<category> tree ( list<category> cs , integer pid , integer level ) { for (CATEGORY&NBSP;C:&NBSP;CS) { if ( c.getparent_id () == pid ) { c.setlevel ( level ); tmpcates.add (c); tree ( cs , c.getid () , level+1 ); } } return tmpcates;} Public static void init () { cates = new arraylist< Category> (); category c1 = new category (); c1.setid (1); c1.setname ("Guangdong"); c1.setparent_id (0); cates.add (C1); c1 = new category (); c1.setid (2); c1.setname ("Shaanxi"); c1.setparent_id (0); cates.add (C1); c1 = new category (); c1.setid (3); c1.setname ("Sichuan"); c1.setparent_id (0); cates.add (C1); c1 = new category (); c1.setid (5); c1.setname ("Shenzhen"); &NBSP;&NBSP;&NBSP;C1.SETPARENT_ID (1); cates.add (C1); c1 = new category (); c1.setid (6); c1.setname ("Shaanxi"); &NBSP;&NBSP;&NBSP;C1.SETPARENT_ID (2); cates.add (C1); c1 = new categorY (); c1.setid (8); c1.setname ("Chengdu"); C1.SETPARENT_ID (3); cates.add (C1); c1 = new Category (); c1.setid (9); c1.setname ("Baoji"); &NBSP;C1.SETPARENT_ID (2); cates.add (C1);}
The main method is the tree () function
Java Simple infinite-pole classification