6 Replies Latest reply on Apr 16, 2009 2:13 PM by mickey3

    tree data structure

    mickey3

      I was wondering why there is no class for a tree? There's array,  there's a list why is there no class to represent a tree?

        • 1. Re: tree data structure
          _Natasha_ Level 4

          Tree by default can use array as a dataProvider.

           

          Objects and arrays can easiely represent tree structure.

          For example:

          <mx:Array id="testData">

               <mx:Object label="test11">

                    <mx:children>

                         <mx:Object label="test221"/>

                    </mx:children>

               </mx:Object>

               <mx:Object label="test12"/>

               <mx:Object label="test13"/>

               <mx:Object label="test14"/>

          </mx:Array>

           

          So you can realize what you want by using dinamic objects or associative arrays if you don't want to create special data structure.

          1 person found this helpful
          • 2. Re: tree data structure
            Miggl Level 1

            I am currently working on the same thing, but have yet to figure out how to programmatically add children objects to a parent object (AS3). Could you give a pointer there?

             

            Thanks!

            -Mike

            • 3. Re: tree data structure
              _Natasha_ Level 4

              It's the same for AS.

               

              If child is only one:

              var parentObject:Object = new Object();

              parentObject.label = "Parent Object";

              var childObj:Object = new Object();

              childObj.label = "Child Object";

              parentObject.children = childObj;

               

               

              For many children:

              var parentObject:Object = new Object();

              parentObject.label = "Parent Object";

              var childObj1:Object = new Object();

              childObj1.label = "Child Object";

              var childObj2:Object = new Object();

              childObj2.label = "Child Object";

              parentObject.children = new Array();

              parentObject.children.push(childObj1);

              parentObject.children.push(childObj2);

               

              For storing child objects you can use any property, but Tree and AdvancedDataGrid by default use property with name "children" for it. If you use another property, you showld create your own dataDescriptor.

              1 person found this helpful
              • 4. Re: tree data structure
                Miggl Level 1

                Thanks Natasha!

                 

                To OP: I hope I didn't hijack your query, but I felt this was related. Hope this helps you as well.

                 

                Cheers!

                -Mike

                • 5. Re: tree data structure
                  mickey3 Level 1

                  Hi

                   

                  THanks os much guys, yes this is truly helping. Just one more thing - I know this might be too asking now but could you post a code how you would recursively search for a given item/object in the whole tree?

                   

                  Thanks.

                  • 6. Re: tree data structure
                    mickey3 Level 1

                    Ok nevermind, i have solved it, here's the function if anyone cares to know:

                     

                            public function findPage( page:Page, id:String ) : Page
                            {
                                if(page.id == id)
                                {
                                    return page;
                                }
                                else
                                {
                                    for( var i:int = 0; i < page.children.length; i++ )
                                    {
                                        var temp:Page = findPage(page.children[i], id);
                                        if(temp != null)
                                           return temp;                    
                                    }   
                                }            
                               
                                return null;    
                            }