Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have actually managed to rewrite my original code to make it work individually. It appears to work at this moment in time. This is the procedure I am now using. Thanks Rob, i'll set yours as the answer since it looks like it will work better for mine and i'll look over it to learn what I can but for the purposes of not unnecessarily using others code I will use my own for now.</p> <pre><code> Procedure TForm1.CreateTree; Var iRounds, iCurrentRound, iCurrentNode, iTraverseToNode:integer; sBinary:String; ThisNode, NewNode, NextNode:TNodePtr; begin iRounds:=0; While Power(2,iRounds) &lt; Memo1.Lines.Count do {Calculates numbers of rounds by using whole powers of 2} iRounds:=iRounds+1; If iRounds &gt; 0 then begin for iCurrentRound:=1 to iRounds do begin for iCurrentNode:=0 to power(2,iCurrentRound)-1 do begin NextNode:=MyTree.GetRoot; ThisNode:=NextNode; New(NewNode); NewNode.Data:=''; NewNode.Left:=Nil; NewNode.Right:=Nil; sBinary:=DenToBinStr(iCurrentNode); if sBinary = '' then sBinary:='0'; While length(sBinary)&gt;iCurrentNode+1 do begin sBinary:='0'+sBinary; end; for iTraverseToNode:=1 to length(sBinary)-1 do While NextNode &lt;&gt; nil do begin if sBinary[iTraverseToNode] = '0' then begin ThisNode:=NextNode; NextNode:=NextNode.Left; end else if sBinary[iTraverseToNode] = '1' then begin ThisNode:=NextNode; NextNode:=NextNode.Right; end end; if sBinary[iCurrentNode+1] = '0' then ThisNode^.Left:=NewNode else if sBinary[iCurrentNode+1] = '1' then ThisNode^.Right:=NewNode else Showmessage('TooFar'); break; end; end; end; end; </code></pre> <p>EDIT: 03/03/2010 I found a much better and simpler way of doing this recursively.</p> <pre><code> Procedure RecursiveTree(r:integer; ThisNode: TNodePtr); Var NewNode:TNodePtr; begin If (NOT assigned(ThisNode.Left)) and (r&lt;&gt;0) then begin New(NewNode); NewNode.Left:=Nil; NewNode.Right:=Nil; NewNode.Data:=''; ThisNode.Left:=NewNode; RecursiveTree(r-1,ThisNode.Left); end; If (NOT assigned(ThisNode.Right)) and (r&lt;&gt;0) then begin New(NewNode); NewNode.Left:=Nil; NewNode.Right:=Nil; NewNode.Data:=''; ThisNode.Right:=NewNode; RecursiveTree(r-1,ThisNode.Right); end; end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload