Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Count tree root nodes (https://www.delphipraxis.net/181569-count-tree-root-nodes.html)

WojTec 26. Aug 2014 10:01

Delphi-Version: XE5

Count tree root nodes
 
Today I need to know how many root nodes (I mean without parent node) has my tree. I wrote this:

Delphi-Quellcode:
function CountTreeNodes(ATree: TTreeView; ALevel: Word): Integer;
var
  I: integer;
begin
  Result := 0;

  for I := 0 to ATree.Items.Count - 1 do
  begin
    if ALevel = ATree.Items[I].Level then
      Inc(Result)
    ;
  end;
end;

Count := CountTreeNodes(Tree, 0);
This is working, but is quite lame IMO - function will be slow and sloow and slooow if many nodes on tree. Is possible to do it in pro mode?

Uwe Raabe 26. Aug 2014 10:26

AW: Count tree root nodes
 
This should be slightly faster:

Delphi-Quellcode:
function CountRootNodes(ATree: TTreeView): Integer;
var
  node: TTreeNode;
begin
  Result := 0;

  node := ATree.Items.GetFirstNode;
  while node <> nil do begin
    Inc(Result);
    node := node.GetNextSibling;
  end;
end;

Sir Rufo 26. Aug 2014 10:43

AW: Count tree root nodes
 
You are right that this is very slow and the documentation also stated this Delphi-Referenz durchsuchenTTreeView.Items
http://docwiki.embarcadero.com/Libraries/XE6/en/Vcl.ComCtrls.TTreeView.Items

Note: Accessing tree view items by index can be time-intensive, particularly when the tree view contains many items. For optimal performance, try to design your application so that it has as few dependencies on the tree view's item index as possible.

But there are some methods to walk through the tree:

Get the very first node by calling
Delphi-Quellcode:
MyNode := MyTreeView.Items[0];
.
Walk through the tree until you get the desired level with Delphi-Referenz durchsuchenTTreeNode.Next
Delphi-Quellcode:
while Assigned( MyNode ) and ( MyNode.Level <> ALevel ) do
  MyNode := MyNode.GetNext;
Once you got the node at the desired level you can step through all nodes on this level with Delphi-Referenz durchsuchenTTreeNode.GetNextSibling
Delphi-Quellcode:
while Assigned( MyNode ) do
  MyNode := MyNode.GetNextSibling;

jaenicke 26. Aug 2014 11:03

AW: Count tree root nodes
 
If performance is an issue for you, why don't you use the virtual trees? They are faster by orders of magnitude...
http://www.jam-software.com/virtual-treeview/

And for your problem you have
Delphi-Quellcode:
VirtualStringTree1.ChildCount[CurrentNode]
.
This property only fetches the appropriate value, because this value is already known, so there is no need for counting or anything. Faster is impossible. ;-)

Sir Rufo 26. Aug 2014 11:31

AW: Count tree root nodes
 
Zitat:

Zitat von jaenicke (Beitrag 1269835)
And for your problem you have
Delphi-Quellcode:
VirtualStringTree1.ChildCount[CurrentNode]
.
This property only fetches the appropriate value, because this value is already known, so there is no need for counting or anything. Faster is impossible. ;-)

It is fast, because it did not count all wanted nodes ;)
Code:
+-n1
  +-n2
  +-n3
+-n4
  +-n5
  +-n6
Counting all nodes at level 1 should return 4 and not 2 ;)

WojTec 26. Aug 2014 13:36

Re: Count tree root nodes
 
I used @Uwe Raabe and @Sir Rufo method, also thanks for explanation :)


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:20 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz