[fpc-pascal] TreeView and Nonrecursion

José Mejuto joshyfun at gmail.com
Wed Sep 1 23:47:23 CEST 2010


Hello FPC-Pascal,

Wednesday, September 1, 2010, 11:20:57 PM, you wrote:

BA> Just to make my question clear, for example, I can fill a TreeView control with
BA> particular Registry keys by enumerating registry keys recursively and put them
BA> in the TreeView control; then for performance reason, I attempt to use a
BA> nonrecursive/iterative approach to enumerate registry keys, but how can I fill
BA> the TreeView since a "tree" is naturally recursive? Is recursion is the only way
BA> to achive it?

You must know at which node a new node must be inserted, but you can
write something like (pseudo code):

procedure FillConditional(var ParentNode: TNode);
var
  j: integer;
  l: TList;
  n: TNode;
begin
  l:=TList.Create;
  n:=nil;
  for j:=0 to xxxx do begin //xxx is some kind of condition
    n:=TNode.Create;
    n.position:=j;
    Tree.AddNode(ParentNode,n);
    if j=10 then begin
      l.add(n); //Remember the node at position 10
    end;
  end;
  for j:=0 to n.Count-1 do begin
    n:=l.Items[j];
    FillConditional(n);
  end;
  l.free;
end;

It is recursive, but first you add all sibliings and them you add each
new brach linearly.

-- 
Best regards,
 José




More information about the fpc-pascal mailing list