Tree algorithm problem

Results 1 to 4 of 4

Thread: Tree algorithm problem

  1. #1
    Join Date
    Dec 1969

    Default 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 &#039;unwinds&#039; the tree and writes each node out in the correct order.<BR>Now, the problem I&#039;m having is not to do with this algorithm, since it&#039;s a simple tree one that anyone should know, it&#039;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&#039;s location (depth in tree, which parent it has etc).<BR><BR>Example of the specific problem I&#039;m having:<BR><BR>root<BR>&#124<BR>&#124-Message1<BR>&#124 &#124-Message2<BR>&#124 x &#124-Message3<BR>&#124 x &#124-Message5<BR>&#124 &#124-Message4<BR>&#124 x &#124-Message7<BR>&#124 x &#124-Message8<BR>&#124 &#124-Message9<BR>&#124-Message6<BR><BR>At the points x I know there should be a vertical line drawn to preserve the structure, but I&#039;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 &#039;top levels&#039; if the threads are n levels deep).<BR><BR>Any help would be greatly appreciated.

  2. #2

    Default 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.

  3. #3
    Join Date
    Dec 1969

    Default No, that's not enough

    Consider:<BR><BR>+ Message1<BR>&#124 + Message2<BR>&#124 &#124 + Message3<BR>&#124 &#124 Message4<BR>&#124 Message5<BR>&#124 &nbsp; + Message6<BR>&#124 &nbsp; 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 recursion--the same indentation if you will--but 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&#039;t too 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&#039;t have to.<BR><BR>

  4. #4
    Join Date
    Dec 1969

    Default 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 hier-menu 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