Use recursion to delete all child nodes of the tree structure (Java and MySQL Implementation)

1. Business scenario

It has the following tree structure: + - 0 + - 1 + - 2 + - 4 + - 5 + - 3

If you delete a parent node, its child nodes and the child nodes of its child nodes, and so on, you need to delete them all.

2. Java implementation

A map is used to store data in the tree structure. The ID is the key of the map and the PID is the value of the tree structure.

3. SQL implementation

Using stored procedures, all child nodes are stored in a temporary table.

After the stored procedure is executed, a temporary table will be generated. The ID is the child node ID to be deleted, nlevel is the node depth, and Scort is the sorting field.

Create a table and insert data:

Create and call stored procedures:

The following are the child nodes to be deleted:

The above article uses recursive deletion of all child nodes of the tree structure (Java and MySQL Implementation) is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>