
Tree algorithm problem
I have coded a simple recursive function to display a tree based on a recordset containing records with a unique id, a parent id and an item string.<BR>Fairly standard stuff so far, however, when I write out the tree, the algorithm recursively 'unwinds' the tree and writes each node out in the correct order.<BR>Now, the problem I'm having is not to do with this algorithm, since it's a simple tree one that anyone should know, it's to do with the displaying of the graphics that accompany the tree to represent the structure.<BR>Exactly like this ASP forum, each node of the tree is drawn on a seperate row, preceded by a set of spaces and images based on the current node's location (depth in tree, which parent it has etc).<BR><BR>Example of the specific problem I'm having:<BR><BR>root<BR><BR>Message1<BR> Message2<BR> x Message3<BR> x Message5<BR> Message4<BR> x Message7<BR> x Message8<BR> Message9<BR>Message6<BR><BR>At the points x I know there should be a vertical line drawn to preserve the structure, but I'm having difficulty finding a demonstration algorithm online that shows either the required test conditions or the way of preserving the current top level (or multiple 'top levels' if the threads are n levels deep).<BR><BR>Any help would be greatly appreciated.

RE: Tree algorithm problem
If your problem is only to display the images, pass annother parameter into the recursive function, the currentDepth in the tree, by knowing how deep you are in, you can calculate what images.

No, that's not enough
Consider:<BR><BR>+ Message1<BR> + Message2<BR>  + Message3<BR>  Message4<BR> Message5<BR> + Message6<BR> Message7<BR><BR>Look at Messages 3 and 4 and compare them to Messages 6 and 7.<BR><BR>They all have the same depth of recursionthe same indentation if you willbut 3 and 4 need an extra vertical line in their indentation that should *not* be there for Messages 6 and 7.<BR><BR>I think that the "parent" in each case has to figure out ahead of time if the vertical line will be needed by its children and, if so, pass it into them.<BR><BR>This can be most easily accomplished by passing the entire indent string along form parent to child, with the parent adding to it before sending it to the next level of recursion.<BR><BR>Assuming you are using GetRows() to get the data into array form (or doing it from an array in the browser...same difference, except more efficient), this isn't too hard...you just have to do a "look ahead" to see if there are any more children of this particular parent. Actually, the best thing to do might be to sort the rough array into the final form (as above) before going ahead with the drawing. But you don't have to.<BR><BR>

RE: No, that's not enough
I did the same thing you are trying to do  I ran into the same problem. The way I fixed it was like this.<BR><BR>start by building a "parent path" string and each time you call your recursion, sent this string along. For example a path may look like this<BR>1<BR>11<BR>111<BR>112<BR>113<BR>12<BR>13<B R>131<BR>1321<BR><BR>etc... Each time the recursion is called it will append an interator to the supplied parent path. You can then use this path to determine how deep the node is in the tree. If this is not clear then I could possibly send you some email with a sample  I pull my data from Commerce Server to build a hiermenu but the sample should suffice to demonstrate the idea.<BR><BR>Man I hope that not too confusing. (^_^)<BR>
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

