The tree structure is very common, the most typical is the menu in the management system, for example, under user management, there are new users, deleted users, etc.
The storage structure in the database table generally has an id and some business fields must have a parent id to store the upper-level id
In this way, a cascading relationship is established.I will not query the database here, just create a few records.
Menu entity class
public class Menu { private Integer id; private String name; private String url; private Integer fatherId; public Menu(Integer id, String name, String url, Integer fatherId) { this.id = id; this.name = name; this.url = url; this.fatherId = fatherId; } @Override public String toString() { return "Menu{" + "id=" + id + ", name='" + name + '\'' + ", url='" + url + '\'' + ", fatherId=" + fatherId + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Integer getFatherId() { return fatherId; } public void setFatherId(Integer fatherId) { this.fatherId = fatherId; } }
In creating a new class, it is the same as this menu entity class, but there is one more child node attribute, which stores all child nodes.
public class MenuVo { private Integer id; private String name; private String url; private Integer fatherId; private List<MenuVo> childNode; @Override public String toString() { return "MenuVo{" + "id=" + id + ", name='" + name + '\'' + ", url='" + url + '\'' + ", fatherId=" + fatherId + ", childNode=" + childNode + '}'; } public MenuVo() { } public MenuVo(Integer id, String name, String url, Integer fatherId, List<MenuVo> childNode) { this.id = id; this.name = name; this.url = url; this.fatherId = fatherId; this.childNode = childNode; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Integer getFatherId() { return fatherId; } public void setFatherId(Integer fatherId) { this.fatherId = fatherId; } public List<MenuVo> getChildNode() { return childNode; } public void setChildNode(List<MenuVo> childNode) { this.childNode = childNode; } }
Next, post the test code
public class Test { public static void main(String[] args) { List<Menu> sqlData=new ArrayList<>(); sqlData.add(new Menu(1,"1","xxx",-1)); sqlData.add(new Menu(2,"1-1","xxx",1)); sqlData.add(new Menu(3,"1-1-1","xxx",2)); sqlData.add(new Menu(4,"2","xxx",-1)); sqlData.add(new Menu(5,"2-1","xxx",4)); sqlData.add(new Menu(6,"1-2","xxx",1)); // store the non-root node List <MenuVo> tempList = new ArrayList <> (); // store the final result List <MenuVo> resultList = new ArrayList <> (); // traverse the database to query the data collection, if Parent id ==-1 means the root node is added to the final result, otherwise the non-root node is added to the temporary node for (Menu menu: sqlData) { if (menu.getFatherId () ==-1 ) { resultList.add ( new MenuVo (menu.getId (), menu.getName (), menu.getUrl (), menu.getFatherId (), new ArrayList <> ())); } else { tempList.add ( newMenuVo (menu.getId (), menu.getName (), menu.getUrl (), menu.getFatherId (), new ArrayList <> ())); } } // traverse all root nodes, through the root node and non Root node collection, find all the child nodes of this root node for (MenuVo menuVo: resultList) { getChildNode (tempList, menuVo); } resultList.forEach (System.out :: println); } public static void getChildNode (List <MenuVo> tempList, MenuVo fatherNode) { for (MenuVo menuVo: tempList) { // If the parent id of the node is the id of the parent node passed in, then it is its child node if (menuVo.getFatherId () == fatherNode.getId ()) { // Add to the child node array fatherNode.getChildNode (). Add (menuVo); // Recursive call to continue to find if the child node has child nodes getChildNode (tempList, menuVo); } } } }
The general idea is to distinguish between the root node and the non-root node first, then traverse the root node, and according to the id of the root node, find the parent id in the non-root node
The node with the root node id is added to the set of child nodes of the root node, but since the child node may also have child nodes, this tree has
We don't know how many levels, so we need to recurse when searching for child nodes, using each child node as a parent node to continue to find his child nodes