I have a web system which has a classical parent-children menu saved in a database, with fields id as the PK, and parent_id to pointing to the owning menu. (Yes, I know this doesn’t scale very well, but that’s another topic).
So for these records (id-parent_id pairs):
0-7 0-4 4-9 4-14 4-16 9-6
I have this tree:
0 ├ 7 └ 4 ├ 9 | └ 6 ├ 14 └ 16
I’m needing to hide a top node, so I have to make a list of all the childrens of that certain node, i.e. for 4, they will be (9, 6, 14, 16). Order doesn’t matters.
I’m confused… does this fits into the classical tree problems? or is it a graph one?
How can I compose this structure and solve this problem using php?
This is the perfect chance to use recursion!
Pseudo-code:
Edit: Didn’t notice that your tree is in the adjacent list format. I would probably just build that into an actual tree datastructure before I started working with it. Just loop through all pairs (creating nodes the first time you see them) and linking them. I think it should be easy…