<p>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). </p>

<p>So for these records (id-parent_id pairs):</p>

<pre><code>0-7 0-4 4-9 4-14 4-16 9-6
</code></pre>

<p>I have this tree:</p>

<pre><code>0
├ 7
└ 4
  ├ 9
  | └ 6     
  ├ 14
  └ 16
</code></pre>

<p>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.</p>

<p>I'm confused... does this fits into the classical tree problems? or is it a graph one?</p>

<p>How can I compose this structure and solve this problem using php?</p>

[{'answer_id': 79067, 'author': 'Blorgbeard', 'author_id': 369, 'author_profile': 'https://Stackoverflow.com/users/369', 'pm_score': 2, 'selected': True, 'text': "<p>This is the perfect chance to use recursion!</p>\n\n<p>Pseudo-code:</p>\n\n<pre><code>nodeList = {}\nenumerateNodes(rootNode, nodeList);\n\nfunction enumerateNodes(node, nodeList) {\n   nodeList += node;\n   foreach ( childnode in node.children ) {\n       enumerateNodes(childnode, nodeList);\n   }\n}\n</code></pre>\n\n<p>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 <em>think</em> it should be easy...</p>\n"}, {'answer_id': 79100, 'author': 'Brettski', 'author_id': 5836, 'author_profile': 'https://Stackoverflow.com/users/5836', 'pm_score': 2, 'selected': False, 'text': '<p>Adjacent list models are very difficult to deal with.  The company I am with now uses them for hierarchies and it causes great headaches.  I have successfully used Celko\'s nested set models for prior employers and they work great for creating, maintaining and using hierarchies (trees).  </p>\n\n<p>I found this link which describes them: <a href="http://www.intelligententerprise.com/001020/celko.jhtml" rel="nofollow noreferrer">http://www.intelligententerprise.com/001020/celko.jhtml</a></p>\n\n<p>But I would also recommend the book "SQL for Smarties: Advanced SQL Programming" written by Joe Celko and covers nested sets.  </p>\n\n<p><a href="https://rads.stackoverflow.com/amzn/click/com/0123693799" rel="nofollow noreferrer" rel="nofollow noreferrer">Joe Celko\'s SQL for Smarties: Advanced SQL Programming</a></p>\n\n<p><a href="https://rads.stackoverflow.com/amzn/click/com/1558609202" rel="nofollow noreferrer" rel="nofollow noreferrer">Joe Celko\'s Trees and Hierarchies in SQL for Smarties</a></p>\n'}, {'answer_id': 79112, 'author': 'Sridhar Iyer', 'author_id': 13820, 'author_profile': 'https://Stackoverflow.com/users/13820', 'pm_score': 0, 'selected': False, 'text': '<p>This is a graph problem. Check out <a href="http://en.wikipedia.org/wiki/Breadth-first_search" rel="nofollow noreferrer">BFS(breadth first search)</a> and <a href="http://en.wikipedia.org/wiki/Depth-first_search" rel="nofollow noreferrer">DFS(depth first search).</a>. You can google out those terms and find hundreds of implementations on the web.</p>\n'}, {'answer_id': 79232, 'author': 'hoyhoy', 'author_id': 3499, 'author_profile': 'https://Stackoverflow.com/users/3499', 'pm_score': 0, 'selected': False, 'text': '<p>This is trivial with a nested set implementation.  See here for more details:</p>\n\n<p><a href="http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/" rel="nofollow noreferrer">http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/</a></p>\n\n<p>Otherwise, write something like this:</p>\n\n<pre><code>def get_subtree(node)\n  if children.size &gt; 0\n    return children.collect { |n| get_subtree(n) }\n  else\n    return node\n  end\nend\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/79041', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/861/']