I voted on nodes in your previous post regarding this, so I must have seen it, but it seems I didn't mention Celko's methods of storing such trees using SQL. I heartily recommend Celko's SQL for Smarties, which treats the matter. A smaller book on this specific topic is Trees and Hierarchies in SQL for Smarties, but that is mostly a rehash of his SQL column which are available on the web.

The idea that seems the one most convenient and transparent to me is to store the location of a node in the tree as a string which traces the path to the tree:

create table nodes ( node_id integer not null primary key, parent integer references node_id, message varchar(1024), location varchar(256) -- only 256 levels of replies and only 256 replies to a root node ); -- Let's create a discussion -- root level insert into nodes (node_id, message,location) values (1,'Hello',''); -- we count from 'A' to 'Z' in this simplicistic example insert into nodes (node_id, message,location) values (2,'Re: Hello','A +'); insert into nodes (node_id, message,location) values (3,'Re: Hello','B +'); insert into nodes (node_id, message,location) values (4,'Re: Re: Hello + (1)','AA'); insert into nodes (node_id, message,location) values (5,'Re: Re: Hello + (2)','AB'); insert into nodes (node_id, message,location) values (6,'Re: Re: Hello +!','BA');

If you want all children of the node number 2, you can run a LIKE query against your table and you will get back the list of nodes in one query:

select node_id, location from nodes where location like 'A%' order by location asc

This saves you the issuing of multiple queries against your database at the price of having a set limit on the number of replies to a node and at the cost of an (anchored) LIKE query. A LIKE query might or might not use an index, depending on your database, so you have to check that. I've demonstrated a very simplicistic approach of using alphabetic characters to denote the position in the tree. You can expand that approach by using multiple characters per depth (for example sprintf '%04d' if you want to keep ASCII), but the fixed limit will remain.

I found some articles by Celko on the subject and likely this Google query will turn up more...


In reply to Re: Message Board Quandary, Revisited by Corion
in thread Message Board Quandary, Revisited by stonecolddevin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.