PHP realizes infinite level classification

  One of the ways to achieve infinite-level classification is to use recursion. Almost all blogs with infinite-level classification that can be found on the Internet use recursion because its idea and implementation are very simple.

  The essence of recursive implementation of infinite-level classification: the process of traversing the tree is implemented recursively in the data structure, so the focus should be focused on the traversal of the tree.

  In fact, the recursive implementation of infinite levels is divided into parent notation and child notation (randomly made words), and the parental notation is the most used.

  Parent notation: Each node will save the identity of the parent node, through which the parent node can be found, but the identity of its children is not saved, that is, if the family tree is not traversed, a node does not know its children The identity of the node, it is not even known if it has child nodes.

  Here is a tree:

 

  In tabular form it is as follows:

mysql> select * from category;
+----+-----------+------------+-----------+
| id | cate_name | cate_order | parent_id |
+----+-----------+------------+-----------+
| 1 | AAAA | 3 | 0 |
|  2 | BBBB      |          2 |         1 |
|  3 | CCCC      |          4 |         1 |
|  4 | DDDD      |          3 |         1 |
| 5 | EEEE | 2 | 2 |
|  6 | FFFF      |          3 |         2 |
| 7 | YYYY 7 | 3 |
| 8 | HHHH | 4 | 4 |
| 9 | IIII | 3 | 4 |10 | YYYY | 2 | 4 |
+----+-----------+------------+-----------+

  

  The process of using recursion to traverse a tree described by parent notation is roughly as follows:

  1. Find the root, the root node

  2. Go through it

  After finding the child, look for the child's child until it can no longer search down, then return to the previous level, and repeat this process until all children have been traversed once.

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325050504&siteId=291194637