-1

I want to iterate all nodes of tree view

so when I have huge number of nodes at that time stack overflow happened so how to handle this situation as I have to iterate all nodes.

var datas= getdatas();

    function treenode() {
        var item = datas.pop();

        if (item) {
        //some operation 
            treenode();
        }
    };
  treenode();
Sanjay Kanani
  • 198
  • 13
  • 2
    Where is the exit condition? – Arup Rakshit Aug 26 '18 at 04:22
  • 2
    it already there if( item ) – Sanjay Kanani Aug 26 '18 at 04:23
  • That is not an exit/base condition... You need to add a base/exit condition there – Arup Rakshit Aug 26 '18 at 04:30
  • 1
    If all you are doing is popping from an array and if that is not null, you are popping from the array again, then recursion is not appropriate, you can just do this with a for loop. Just loop over datas.length and break if you get a null entry. – Chris Cousins Aug 26 '18 at 04:30
  • @ChrisCousins ,Yes that will work if it is a simple array but it is tree node inside tree-node structure and I have just written down overall scenario not entire code as it's big function , I have to solve this with recursive only – Sanjay Kanani Aug 26 '18 at 04:33
  • If your datas is tree-like then please show an example of what datas looks like. If indeed a recursive function is needed, to be sure any and all recursive functions *can be* converted to iterative functions if needed (for instance if depth is huge). – Chris Cousins Aug 26 '18 at 04:37
  • Posting questions with code like `datas = getdatas()` doesn't allow us to help you when `getdatas` remains undefined in the rest of your post. voting to close this question until you post real code with concrete data. – Mulan Aug 27 '18 at 20:19

1 Answers1

0

Hey Guys I have found solution with settimeout function

Now I am not getting stack overflow error by moving

simple calling call stack to event based calling.

var datas= getdatas();

    function treenode() {
        var item = datas.pop();

        if (item) {
        //some operation 
         setTimeout(treenode,0);
        }
    };
  treenode()
Sanjay Kanani
  • 198
  • 13