Miles K. wrote:
The recursive function does work given my data set. The mysql table
that I am drawing from already contains the parent/child relationships.
Table :
[ INDEX ] [ PARENT ] [ DATA ]
1 0 food >> ROOT Category
2 1 cheeses >> Sub-Category
3 2 cheddar >> Item 1
4 2 brie >> Item 2
5 0 beverages >> ROOT Category
and so forth.
Does this clear it up at all?
Hi Miles,
After what you have said now it does look that recursion is the best
strategy to solve this algorithm. However you are growing it on me
gradually. First you didn’t even consider multiple root records, and
from your last post it is clear that you have not only multiple root
records but multiple levels of indentation, i.e. not only
two-dimensional Gtk::TreeStore structure but a multi-dimensional one.
And so I come back to my very first objection, which is your condition
that when looked at superficially seemed to only determine the root
nodes, but is actually the controlling mechanism for the recursion. You
should have given more precise definition of what was only a
hypothetical pseudo-code like condition “if t.to_i == parent_id”.
The second issue you have here is the problem which I have already
covered extensively, and would show up only when you actually run a GUI
application, where you would either get root and children nodes properly
indented, or you would only get a two-dimensional layout!
Third minor problem which, indeed, can be programmatically accounted
for, is with your list notation. Your [ INDEX ] [ PARENT ] [ DATA ]
should actually look something like the following:
[ INDEX ] [ PARENT ] [ DATA ]
0, 0, “food” >> ROOT Category
1, 1, “cheeses” >> Sub-Category
2, 1, “cheddar” >> Item 1
3, 1, “brie” >> Item 1
4, 4, “beverages” >> ROOT Category
5, 5, “non-alcohol” >> Sub-Category
6, 5, “pepsi” >> Item 1
7, 5, “coke” >> Item 1
8, 4, “alcoholic” >> Sub-Category
9, 8, “wine” >> Item 1
10, 8, “brandy” >> Item 1
I am really interested to see how you actually handled the problematic
recursion terminating condition.
Cheers, Igor