AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Count tree root nodes

Ein Thema von WojTec · begonnen am 26. Aug 2014 · letzter Beitrag vom 26. Aug 2014
Antwort Antwort
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

Count tree root nodes

  Alt 26. Aug 2014, 10:01
Delphi-Version: XE5
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?
  Mit Zitat antworten Zitat
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.009 Beiträge
 
Delphi 12 Athens
 
#2

AW: Count tree root nodes

  Alt 26. Aug 2014, 10:26
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;
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#3

AW: Count tree root nodes

  Alt 26. Aug 2014, 10:43
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 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;
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.346 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: Count tree root nodes

  Alt 26. Aug 2014, 11:03
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 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.
Sebastian Jänicke
Alle eigenen Projekte sind eingestellt, ebenso meine Homepage, Downloadlinks usw. im Forum bleiben aktiv!
  Mit Zitat antworten Zitat
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#5

AW: Count tree root nodes

  Alt 26. Aug 2014, 11:31
And for your problem you have 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
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#6

Re: Count tree root nodes

  Alt 26. Aug 2014, 13:36
I used @Uwe Raabe and @Sir Rufo method, also thanks for explanation
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:42 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