java - Iteration with a queue vs method recursion in a tree -


i familiar pros , cons of iteration , recursion in general. have particular question regarding queues vs recursion. have tree, each node additionally indexed in map via key. if trim parts of tree, need unindex them, need visit each , every node. far called trim method recursively, - in java - causes quite number of stack frames created. alternative using queue linkedlist.

the question is: which more costly? creation , linking of new nodes in linkedlist, or recursive calls? or there other data structures queues should favour on linkedlist?

further information might useful: every child node stored in array in parent node , there no reference parent node. problem space real time ai, performance quite crucial , extend favourable on elegant solution possible.

problem solved recursion:

private void trimrecursive(node node) {     // perform operations on node     if(node.children!=null){         (int = 0; < node.children.length; i++) {             trimrecursive(node.children[i]);         }     } } 

problem solved iteration:

private void trimiterative(node node) {     // perform operations on node     queue<node> queue = new linkedlist<>();     queue.add(node);     while(!queue.isempty()){         if(node.children!=null){             (int = 0; < node.children.length; i++) {                 queue.add(node.children[i]);             }         }     } } 

thanks in advance!


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -