If a node cannot have multiple parents take out the Closed stuff in the code below. That will limit the levels to 65000 you can of course hold the Open list in a cursor, then no limits to the # of levels else leave the closed stuff that will limit the # of nodes to 65000 (in Closed) unless you put the closed list in a cursor, no 65000 # of nodes limit, but slower endifHere's the algorithm (I have not tested it.)
&&Unexplored nodes are put on an Open list Put the start node in the Open list do while there are nodes in Open An action is done on a node in Open The node in Open is moved to the Closed list That node is expanded for each successor if not in Closed list add to the Open list endif endfor enddo
*--------------------------------------------------------------------------- function do_it() create cursor Tree ; ( Parent I default 0, ; Child I default 0 ; ) select Tree Index on bintoc(Parent) tag Parent && optional index on bintoc(Child) tag Child && optional index on bintoc(Parent) + bintoc(-Child) tag Down && yes: -Child *--------------------------------------------------------------------------- #define NODES_MAX 65000 Function Traverse(StartNode) && depth first local ParentCurrent local Nodes_Closed[ NODES_MAX ], Nodes_Open[ NODES_MAX ] local nOpen, nClosed nOpen = 0 nClosed = 0 && put the startnode in open =seek(bintoc(StartNode)) if( Parent == StartNode ) nOpen = nOpen + 1 Nodes_Open[ nOpen ] = recno() endif do while !empty(nOpen) go (Nodes_Open[ nOpen ] ) && take the node from Open Nodes_Open[ nOpen ] = Null && optional nOpen = nOpen - 1 && and put it in Closed nClosed = nClosed + 1 Nodes_Closed[ nClosed ] = recno() && put the action for the node here && traverse ParentCurrent = Parent scan rest while ( Parent == ParentCurrent ) && if the node in not in Closed do case case empty( ascan( Nodes_Closed, recno()) ) && add it to Open nOpen = nOpen + 1 Nodes_Open[ nOpen ] = recno() endcase endscan enddo endfunc *---------------------------------------------------------------------------