in reply to walking a tree
Trees are generally represented in one of two fashions in a database, each with its benefits/drawbacks. The first is the way you're doing it - a table that has data, an id, and a parent_id. To find all children, you ask SELECT id FROM table WHERE parent_id = ?. If you need ordering (such as a binary tree), add in an order column. This is a really good fashion for mutations, but it sucks for queries, unless you're on Oracle or Postgres and have the CONNECT BY extension (and even then it still sucks).
The other method (nested sets) is a bit harder to explain. Take a look at http://dev.mysql.com/tech-resources/articles/hierarchical-data.html for a good article on the topic. Less efficient on mutations, but much more efficient in terms of queries, including traversals.
|
|---|