// Preface
Function parseSrc (dataSrc ){
Var childNodes: Array = dataSrc. childNodes;
Var result: Array = new Array ();
For (var I = 0; I <childNodes. length; I ++ ){
Var element: XML = childNodes;
If (element. nodeType = 1 & element. hasChildNodes ()){
ParseSrc (element );
}
If (element. nodeType = 1 ){
Result. push (element );
Trace (element. nodeName );
Element. removeNode ();
} Else {
Trace (element + ", value:" + element. nodeValue );
Element. removeNode ();
}
}
}
// Middle Order
Function parseSrc (dataSrc: XML ){
Var childNodes: Array = dataSrc. childNodes;
Var result: Array = new Array ();
For (var I = 0; I <childNodes. length; I ++ ){
Var element: XML = childNodes;
Result. push (element. cloneNode (false ));
Trace (element. cloneNode (false ));
If (element. hasChildNodes ()){
ParseSrc (element );
}
}
}
// Non-recursive traversal. Visit the leaf first. Refer to the data structure tutorial. Function preParse (node ){ Var work: Array = new Array (); // work Stack Var result: Array = new Array (); // result Stack For (; node. hasChildNodes () | work. length> 0 ;){ For (; node. hasChildNodes () & node. childNodes [0]. nodeType = 1 ;){ Work. push (node ); Node = node. childNodes [0]; } Trace (node. nodeName ); Result. push (node ); If (work. length> 0 ){ Node. removeNode (); // If there are nodes in the work stack, continue traversal. The node is retrieved from the root. Node = work. pop (); } } }In terms of efficiency, I think recursion is more efficient than iteration. Because the optimization of vm in the stack segment is higher than that of the stack written manually |