Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-09

TreeView and Nonrecursion

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bihar Anwar

A Newbie question :-) Is it possible to fill TreeView (tree is naturally recursive) with a nonrecursive/iterative thinking paradigm? Are there some good examples regarding this matter in the Internet?

I post this question in FPC mailing list because my question actually about filling a "tree" in general.

Thanks in advance.

Reply by email

Vannus
to Bihar Anwar

i probably shouldn't open my mouth, as i don't quite understand the question... however FRED from the game Freespace let you design missions using a treeview.

It involved following a tree like
|- when 'Enemy Cargo ship' is 'attacked'
| \- then 'enemy support ships' do 'jump in'
|- when 'Enemy Cargo ship' is 'destroyed'
| \- then 'mission successful'
|- etc...

or do you mean the other way around where the thinking paradigm fills in the treeview?

- V

Bihar Anwar wrote 18 lines
A Newbie question :-) Is it possible to fill TreeView (tree is naturally recursive) with a nonrecursive/iterative thinking paradigm? Are there some good examples regarding this matter in the Internet? I post this question in FPC mailing list because my question actually about filling a "tree" in general. Thanks in advance. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Bihar Anwar
to Vannus
Vannus wrote
i probably shouldn't open my mouth, as i don't quite understand the question... however FRED from the game Freespace let you design missions using a

treeview.

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

Reply by email

José Mejuto
to Bihar Anwar

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é

Reply by email

Juha Manninen (gmail
to José Mejuto
José Mejuto wrote
You must know at which node a new node must be inserted...

If your input data contains a string which always identifies the parent node then you can map the string -> "parent node" and find it later for adding a child node.

Pseudo code again:

var
  ParentNode, Node: TNode;
  Map: TStringHashMap;
  ParentId, Id: string;
...
  //  get data from somewhere
  ParentId := ...
  Id := ...
  ParentNode := Map[ParentId];
  // This should make Node a root node if ParentNode = nil.
  Node := Tree.AddNode(ParentNode, Id);  // Pseudo syntax.
  Map[Id] := Node;   // Use as parent for following nodes.

If you don't have such ID then you must use recursion.

Juha

Reply by email

Flávio Etrusco
to Bihar Anwar: treeview. Just to make my question clear, for example, I can fill a TreeView control with…
Bihar Anwar wrote 14 lines
On 2 September 2010 3:53:34 AM, Vannus wrote: > i probably shouldn't open my mouth, as i don't quite understand the > question... > however FRED from the game Freespace let you design missions using a > treeview. Just to make my question clear, for example, I can fill a TreeView control with particular Registry keys by enumerating registry keys recursively and put them in the TreeView control; then for performance reason, I attempt to use a nonrecursive/iterative approach to enumerate registry keys, but how can I fill the TreeView since a "tree" is naturally recursive? Is recursion is the only way to achive it?

When reading from registry and populating a treeview, a recursive function is the least of your worries regarding "performance" (or memory, if you're careful).

-Flávio

Reply by email

Bihar Anwar
to Juha Manninen (gmail: If your input data contains a string which always identifies the parent node then you can…
Juha Manninen wrote 9 lines
If your input data contains a string which always identifies the parent node then you can map the string -> "parent node" and find it later for adding a child node. Pseudo code again: ... ... If you don't have such ID then you must use recursion.

Thanks José and Juha, now I see the light. :-)

Flávio Etrusco wrote
When reading from registry and populating a treeview, a recursive function is the least of your worries regarding "performance" (or memory, if you're careful).

I'm a crash-phobia :-). Stack sizes are different among the OS platforms. A superdeep recursion will trigger a stack overflow exception.

Reply by email