TIWAdvTreeView Examples

I know recursion can be confusing, but in this case I don't know of any alternative.

I would recommend using a TAdvTreeNode object instead of an index, that will make it easier to add child nodes.
Below is the modified sample code. I hope this can help you.

ParentNode: TAdvTreeNode;
ParentID: integer;

  ParentID := 1;
  ParentNode := nil;

  N := TIWAdvTreeView1.Items.GetFirstNode;

  while (N <> nil) do
  begin
    if (ParentID = N.Tag) then
      ParentNode := N;

    RetrieveChildNodes(N, ParentID);
    N := N.getNextSibling;
  end;

  if (ParentNode <> nil) then
  begin
//ParentNode.AddChild
  end
  else
  begin
      //Parent not found
  end;

procedure RetrieveChildNodes(Node: TAdvTreeNode;
  ParentID: integer);
var
  ExN: TAdvTreeNode;
begin
  if not Assigned(Node) then
    Exit;

  ExN := Node.getFirstChild;
  if Assigned(ExN) then
  begin
    while (ExN <> nil) do
    begin
      if (ParentID = ExN.Tag) then
        ParentNode := ExN;

      RetrieveChildNodes(ExN, ParentID);
      ExN := ExN.getNextSibling;
    end;
  end;
end;