Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/davidberneda/GenericTree
Delphi implementation of a generic Tree structure
https://github.com/davidberneda/GenericTree
Last synced: about 1 month ago
JSON representation
Delphi implementation of a generic Tree structure
- Host: GitHub
- URL: https://github.com/davidberneda/GenericTree
- Owner: davidberneda
- License: unlicense
- Created: 2015-06-15T10:50:33.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-12-30T18:07:25.000Z (over 7 years ago)
- Last Synced: 2024-01-09T01:14:55.480Z (5 months ago)
- Language: Pascal
- Size: 136 KB
- Stars: 53
- Watchers: 20
- Forks: 15
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Lists
- awesome-pascal - GenericTree
- awesome-delphi - GenericTree
- awesome-delphi - GenericTree
README
# GenericTree
Delphi implementation of a generic Tree structure
## Basic usage:
```delphi
var Root : TNode;Root := TNode.Create;
try
Root.Add('Hello').Add('World !');
finally
Root.Free;
end;
```The generic type can be pre-declared for easier typing:
```delphi
type
TFloatTree = TNode;var Tree1 : TFloatTree;
```## Features:
Adding nodes using the Add method, returns the new created node:
```delphi
var Node : TNode;
Node := Root.Add('abc');
```"Data" property is your own custom data at each node:
```delphi
var Node : TNode;
When : TDateTime;
Node := Root.Add(Now);
When := Node.Data;
Node.Data := Tomorrow;
```"Count" returns the number of child nodes for a given node:
```delphi
var t : Integer;
t:=Node.Count;
```"Empty" returns True when the number of children nodes is zero:
```delphi
var b : Boolean;
b:=Node.Empty;
```Destroying a node removes it from its parent:
```delphi
Node.Free;
```Nodes can be accessed using the default array property:
```delphi
Node := Root[3];
```"Clear" removes and destroys all children nodes of a given node (recursively):
```delphi
Node.Clear;
```"Index" returns the position of a node in its parent children array, or -1 if the node is a "root" node.
```delphi
var t : Integer;
t:=Node.Index;
```"Parent" property returns the node that is the parent of a given node, or nil if the node is a "root" node.
```delphi
var tmp : TNode;
tmp:=Node.Parent;
```A node can be "detached" from its parent (without destroying it), setting the Parent property to nil:
```delphi
Node.Parent:=nil;
```A node can also be removed and destroyed using its Parent Delete method:
```delphi
Root.Delete(3); // removes and destroys the 4th child of Root
```Traversing nodes (recursively or not) using the ForEach method:
```delphi
var Total:Integer;
Total:=0;
Root.ForEach(procedure(const Item:TNode) begin Inc(Total); end);
```A loop of all children nodes can be done using a traditional loop:
```delphi
var t : Integer;
for t:=0 to Node.Count-1 do Node[t].Data:='hello';
```And also using a "for N" :
```delphi
var N : TNode;
for N in Node.Items do N.Data:='hello';
```The "Level" property returns the depth of a node, (the number of parent->parent->...), being zero for root nodes that have no parent.
```delphi
var t : Integer;
t:=Node.Level;
```Children nodes can be exchanged positions:
```delphi
Node.Exchange( 5, 9 ); // <-- swap children nodes at 5 and 9 positions
```Sorting nodes using a custom "compare" function:
```delphi
Node.Sort(function(const A,B:TNode):Integer
begin
if CaseSensitive then
result:=CompareStr(A.Data,B.Data)
else
result:=CompareText(A.Data,B.Data);
end);
```