Skip to main content
TeeTree provides multiple flexible methods to create tree structures and add nodes. This guide covers the essential techniques for building your tree hierarchies.

Understanding Tree Structure

A TeeTree component maintains a network-hierarchy of nodes, where:
  • Root nodes have no parent
  • Child nodes belong to a parent node
  • Brother nodes share the same parent
  • Each node is a full component with properties, methods, and events

Adding Root Nodes

Root nodes are the top-level nodes in your tree without any parent.
// Add a root node with text
MyRoot := Tree1.Add('This is a Root node');
The Add method without a parent parameter automatically creates a root node.

Adding Child Nodes

Child nodes are attached to a parent node and form the tree hierarchy.
1

Using Parent Node

The most direct way is to call AddChild on the parent node:
MyRoot := Tree1.Add('Parent Node');
MyRoot.AddChild('This is a child node');
2

Using Tree Component

Pass the parent as the second parameter:
Tree1.Add('Child node text', MyRoot);
3

Using Shapes Collection

Use the Shapes collection’s AddChild method:
Tree1.Shapes.AddChild(MyRoot, 'Child text');

Child Node Variations

// Standard child addition
ChildNode := ParentNode.AddChild('Child text');

Adding Brother Nodes

Brother nodes (siblings) share the same parent as an existing node.
var Node: TTreeNodeShape;
begin
  // Add a brother to the selected node
  with Tree1.Selected.First do
    if Assigned(Parent) then
      Node := AddBrother('Brother num: ' + IntToStr(Parent.Count + 1))
    else
      Node := Tree1.AddRoot('Root num: ' + IntToStr(Tree1.Roots.Count + 1));
end;
Before calling AddBrother, check if the node has a parent. Root nodes have no parent, so you should create a new root instead.

10 Methods to Add Nodes

Here’s a comprehensive example from the TeeTree demo showing different addition techniques:
procedure TAddMethodsForm.FormCreate(Sender: TObject);
var 
  MyNode: TTreeNodeShape;
  AnotherNode: TTreeNodeShape;
begin
  // 1. Simple way to add a root node
  Tree1.Add('1.Simple');

  // 2. Preserve the resulting node
  MyNode := Tree1.Add('2.MyNode');

  // 3. Add a child node to MyNode
  Tree1.Add('3.Child', MyNode);

  // 4. Specify X and Y positions with no parent
  Tree1.Add(200, 100, '4.At 100,100', nil);

  // 5. Using MyNode's Add method
  MyNode.Add('5.Child');

  // 6. Adding a brother
  MyNode.AddBrother('6.Brother');

  // 7. Using Shapes.AddChild
  Tree1.Shapes.AddChild(MyNode, '7.AddChild');

  // 8. Using default index property
  AnotherNode := Tree1[0].Add('8.Add Child');

  // 9. Using the node Parent property
  AnotherNode.Parent.Add('9.Another Brother');

  // 10. Chain nodes in one line
  Tree1.Add('10.Root').Add('Child').Add('Sub-Child');
end;

Expanding Nodes

After creating child nodes, expand the parent to make them visible:
MyRoot.Expanded := True;

// Or toggle expansion
MyRoot.Toggle;

Interactive Node Addition

Example of adding nodes interactively based on user selection:
procedure ButtonAddChildClick(Sender: TObject);
var Node: TTreeNodeShape;
begin
  // Add child to selected node
  with Tree1.Selected.First do
  begin
    Node := AddChild('Child num: ' + IntToStr(Count + 1));
    Expanded := True;  // Show the new child
  end;
  
  // Select the new node
  Tree1.Selected.Clear;
  Node.Selected := True;
end;

Inserting Nodes at Specific Positions

// Insert at specific index in children list
NewNode := ParentNode.Insert(2, 'Inserted at position 2');

Best Practices

When adding many nodes, set capacity for better performance:
// Set before adding nodes (in unit initialization)
TreeListCapacity := 1000;
TreeShapeListCapacity := 100;
  • Nodes are automatically freed when their parent is destroyed
  • Root nodes are freed when the Tree component is destroyed
  • Use Clear to remove all nodes: Tree1.Shapes.Clear
  • Group related nodes under common parents
  • Use meaningful text for node identification
  • Consider using Data or TagObject properties to associate custom data

Common Patterns

Building from Data

// Build tree from array
for i := 0 to High(MyArray) do
  Tree1.Add(MyArray[i].Name);

Building Hierarchical Structure

var Category, Item: TTreeNodeShape;
begin
  Category := Tree1.Add('Electronics');
  Category.AddChild('Computers');
  Category.AddChild('Phones');
  Category.AddChild('Tablets');
  Category.Expanded := True;
end;

Next Steps

Build docs developers (and LLMs) love