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; }
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?
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?
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; }
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?
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?
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. :)
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?
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."
Don't see what you're looking for? Try a search.
|