<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" backgroundColor="#ffffff" creationComplete="initializeTree()">
<mx:Script>
<![CDATA[
        
  public var dpXML:XML;

  public function initializeTree():void
  {
    dpXML = <root>
        <node label="First Folder" isBranch="true"/>
        <node label="Second Folder" isBranch="true"/>
        <node label="Third Folder" isBranch="true"/>
      </root>;
            
    myTree.showRoot = false;
    myTree.labelField = "@label";
    myTree.dataProvider = dpXML;
  }
  public function addChildrenToTree(event:Object):void
  {
    // the selected node

    var selectedNode:Object =event.node;
            
    // the label of the selected node

    var _label:String = String( selectedNode.@label );
            
    // If the selected node does not include the word "Sub", 

    // then let's give it 2 children: a branch and a leaf.

           
    if ( _label.search( "Sub" ) == -1 )
    {
      var myXML:XML = <root>
          <node label="Sub Folder" isBranch="true" />
          <node label="Sub Leaf" isBranch="false" />
        </root>;

      // add myXML to the selected node

      // myXML.node basically ignores the root node. 

      // <root> is just there to form valid XML


      selectedNode.setChildren(myXML.node);
                
      // Otherwise let's just give it a leaf

    } else {
      var myXML:XML = <node label="Sub Leaf 2" isBranch="false" />;
      selectedNode.setChildren(myXML);
    } 
    // re-apply the data-provider

    myTree.dataProvider = dpXML;
  }
]]>
</mx:Script>

<mx:Tree id="myTree" nodeOpen="addChildrenToTree(event)" color="#ffffff" width="500" height="454" borderThickness="0" alternatingRowColors="['#606060', '#505050']" />

</mx:Application>