Home > Backend Development > PHP Tutorial > 请问一个树节点的删除算法,请大家过来看看吧

请问一个树节点的删除算法,请大家过来看看吧

WBOY
Release: 2016-06-13 13:42:47
Original
1072 people have browsed it

请教一个树节点的删除算法,请大家过来看看吧
求一个php的算法实现,实现如下:
将一个形如表1的树以表2的形式存储在一张数据库表中,现在希望删除一个子树(例如想删除Red子树)该怎么做呢?
Food
|--Fruit
|----Green
|------Pear
|----Red
|------Cherry
|------Apple
|----Yellow
|------Banana
|--Meat
|----Mutton
|----Beef
|----Pork
(表1)
ID                 parent             title
1                                             Food
2                   Food                   Fruit
3                   Fruit                 Green
4                   Green                 Pear
5                   Fruit                 Red
6                   Red                     Cherry
7                   Fruit                 Yellow
8                   Yellow               Banana
9                   Food                   Meat
10                 Meat                   Mutton
11                 Meat                   Beef
12                 Meat                   Pork
13                 Red                     Apple
(表2)

------解决方案--------------------
递归

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template