all groups > flash actionscript > december 2005 >
You're in the

flash actionscript

group:

Tree Component....



Tree Component.... celoftis
12/22/2005 9:43:02 PM
flash actionscript: I have a large Tree with a dynamic number of nodes and level.
I want to search through a given (sub) Tree to find a particular node. What's
the easiest way todo this? Really I just stuck on knowing how to iterate over
the nodes in a given tree... if there's an easier way todo this please point me
in the right direction!
Here's my code - questions in comments!!!

function findNode(xmlNode:XMLNode):XMLNode {
for (each node in the root of the Tree) { //don't know how todo this!!! How
do I iterate over the XMLNodes in the Tree?
//Some search criteria... I just
picked nodeName and label for this example
if ((xmlNode.attributes.nodeName == node.attributes.nodeName)
&& (xmlNode.attributes.label ==
node.attributes.label)) {
//found the node...
return xmlNode;
} else {
theNode = findNodeHelper(node);
if (null == theNode) {
//not not found yet...
} else {
return theNode;
}
}
}
return null;
}
function findNodeHelper(myNode:XMLNode):XMLNode {
for (xmlNode in myNode.childNodes) { //Dont' kow how to start this loop!
//Some search criteria... I just
picked nodeName and label for this example
if ((xmlNode.attributes.nodeName == myNode.attributes.nodeName)
&& (xmlNode.attributes.label ==
myNode.attributes.label)) {
//found the node...
return xmlNode;
} else {
theNode = findNodeHelper(myNode);
if (null == theNode) {
//not not found yet...
} else {
return xmlNode;
}
}
}
return null;
}



Re: Tree Component.... krl
12/22/2005 9:49:59 PM
Basically xml nodes are kept in an array. You access them like you would a
multidimensional array. Use indexing to iterate through the tree. .

Are you interested in a quicker search algorithm, ot do you need help with the
code?
Re: Tree Component.... celoftis
12/23/2005 1:57:42 PM
Originally posted by: krl
Basically xml nodes are kept in an array. You access them like you would a
multidimensional array. Use indexing to iterate through the tree. .

Are you interested in a quicker search algorithm, ot do you need help with the
code?

I can manage the code... but I'm always up for learner a
different/faster/better why of doing things. Any suggestions?

Re: Tree Component.... celoftis
12/23/2005 3:22:09 PM
Here's what I finally came up with...


function findNode(myNode:XMLNode):XMLNode {
var tmpNode:XMLNode = myTreeNavMenu.menuTree.dataProvider.firstChild;
for (var aNode:XMLNode = myTreeNavMenu.menuTree.dataProvider.firstChild;
null != aNode; aNode=aNode.nextSibling) {
if ((aNode.nodeName == myNode.nodeName)
&& (aNode.attributes.label == myNode.attributes.label)) {
return aNode; //found the node...
} else {
theNode = findNodeHelper(myNode, aNode);
if (null == theNode) {}
else {
return theNode;
}
}
}
return null;
}
function findNodeHelper(myNode:XMLNode, xmlNode:XMLNode):XMLNode {
for (var aNode:XMLNode = xmlNode.firstChild;
null != aNode; aNode=aNode.nextSibling) {
if ((aNode.nodeName == myNode.nodeName)
&& (aNode.attributes.label == myNode.attributes.label)) {
return aNode; //found the node...
} else {
theNode = findNodeHelper(myNode, aNode);
if (null == theNode) {}
else {
return theNode;
}
}
}
return null;
}

Re: Tree Component.... krl
12/23/2005 3:53:50 PM
Originally posted by: celoftis
I can manage the code... but I'm always up for learner a
different/faster/better why of doing things. Any suggestions?


If you're using your search mechanism a lot then a faster search algorithm
would be adventageous for you.
Here are a few different algorithms that I've seen: binary search tree,
quicksort, radix-sort, red-black trees, AVL trees, and some other ones. What
type of things would you like to learn?
Re: Tree Component.... celoftis
12/23/2005 4:09:35 PM
Originally posted by: krl
Originally posted by: celoftis
I can manage the code... but I'm always up for learner a
different/faster/better why of doing things. Any suggestions?


If you're using your search mechanism a lot then a faster search algorithm
would be adventageous for you.
Here are a few different algorithms that I've seen: binary search tree,
quicksort, radix-sort, red-black trees, AVL trees, and some other ones. What
type of things would you like to learn?

Thanks for the input - I am famaliar with all those menthods (etc) - the
amount of searching will be quite limited for the applicaiton I'm building (and
the number of nodes will typically be small, <500). Therefore, I figured than
an O(n) search would be OK. (Of course if this app needed the capacity for
searching I'd make the effort) The algos you mention are great but I'd probably
spend more time implementing them than it will take me to complete the rest of
the project.

For future reference do you know where to obtain any of these algos implented
in ActionScript?

Re: Tree Component.... krl
12/23/2005 4:33:29 PM
That's cool you know 'em.

I haven't found anywhere with any of them implemented. I've started looking, but haven't found anything yet.

A Java to Actionscript converter would be nice. :)




Re: Tree Component.... celoftis
12/23/2005 4:37:00 PM
Re: Tree Component.... krl
12/23/2005 5:05:18 PM
That would be very intense.

What kind of things would it entail?
Obviosuly someone to know both quite well. Then a lot of man hours to write
all the relations. Phew.

Sorta reminds me on .NET. Does Actionscript have a place somewhere in there?

Re: Tree Component.... celoftis
12/23/2005 5:10:01 PM
Here's what I found on MSDN:

"As with any other rendering technology, migrating to Flash is not an
automated process and will include extensive rewriting of existing code and may
require extensive knowledge of the associated scripting language
(ActionScript). Flash is not a Microsoft supported technology and will
therefore require you to seek support options from the appropriate vendor if
you choose this technology as your migration solution."


AddThis Social Bookmark Button