Using recursive algorithm combined with database parsing into Java tree structure of code parsing

  • 2020-10-23 20:05:47
  • OfStack

1. Prepare table structure and corresponding table data

Table structure:


create table TB_TREE
(
CID NUMBER not null,
CNAME VARCHAR2(50),
PID NUMBER // The parent node 
)

b. Table Data:


insert into tb_tree (CID, CNAME, PID) values (1, ' China ', 0);
insert into tb_tree (CID, CNAME, PID) values (2, ' The Beijing municipal ', 1);
insert into tb_tree (CID, CNAME, PID) values (3, ' Guangdong province, ', 1);
insert into tb_tree (CID, CNAME, PID) values (4, ' Shanghai ', 1);
insert into tb_tree (CID, CNAME, PID) values (5, ' guangzhou ', 3);
insert into tb_tree (CID, CNAME, PID) values (6, ' shenzhen ', 3);
insert into tb_tree (CID, CNAME, PID) values (7, ' zhuhai ', 5);
insert into tb_tree (CID, CNAME, PID) values (8, ' The tianhe district ', 5);
insert into tb_tree (CID, CNAME, PID) values (9, ' Futian district ', 6);
insert into tb_tree (CID, CNAME, PID) values (10, ' Nanshan district ', 6);
insert into tb_tree (CID, CNAME, PID) values (11, ' Miyun county ', 2);
insert into tb_tree (CID, CNAME, PID) values (12, ' In the pudong new area ', 4);

2. TreeNode object, corresponding to tb_tree


public class TreeNode implements Serializable {
private Integer cid;
private String cname;
private Integer pid;
private List nodes = new ArrayList();
public TreeNode() {
}
//getter , setter omit 
}

3. Test data


public class TreeNodeTest {
@Test
public void loadTree() throws Exception{
System.out.println(JsonUtils.javaToJson(recursiveTree(1)));
}
/**
*  The recursive algorithm resolves into a tree structure 
*
* @param cid
* @return
* @author jiqinlin
*/
public TreeNode recursiveTree(int cid) {
// According to the cid Get the node object (SELECT * FROM tb_tree t WHERE t.cid=?)
TreeNode node = personService.getreeNode(cid);
// The query cid All of the child nodes below (SELECT * FROM tb_tree t WHERE t.pid=?)
List childTreeNodes = personService.queryTreeNode(cid); 
// Traversal of child nodes 
for(TreeNode child : childTreeNodes){
TreeNode n = recursiveTree(child.getCid()); // recursive 
node.getNodes().add(n);
}
return node;
}
}

The output json format is as follows:


{
  "cid": 1,
  "nodes": [
    {
      "cid": 2,
      "nodes": [
        {
          "cid": 11,
          "nodes": [
          ],
          "cname": " Miyun county ",
          "pid": 2
        }
      ],
      "cname": " The Beijing municipal ",
      "pid": 1
    },
    {
      "cid": 3,
      "nodes": [
        {
          "cid": 5,
          "nodes": [
            {
              "cid": 7,
              "nodes": [
              ],
              "cname": " zhuhai ",
              "pid": 5
            },
            {
              "cid": 8,
              "nodes": [
              ],
              "cname": " The tianhe district ",
              "pid": 5
            }
          ],
          "cname": " guangzhou ",
          "pid": 3
        },
        {
          "cid": 6,
          "nodes": [
            {
              "cid": 9,
              "nodes": [
              ],
              "cname": " Futian district ",
              "pid": 6
            },
            {
              "cid": 10,
              "nodes": [
              ],
              "cname": " Nanshan district ",
              "pid": 6
            }
          ],
          "cname": " shenzhen ",
          "pid": 3
        }
      ],
      "cname": " Guangdong province, ",
      "pid": 1
    },
    {
      "cid": 4,
      "nodes": [
        {
          "cid": 12,
          "nodes": [
          ],
          "cname": " In the pudong new area ",
          "pid": 4
        }
      ],
      "cname": " Shanghai ",
      "pid": 1
    }
  ],
  "cname": " China ",
  "pid": 0
}

conclusion


Related articles: