Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recursion 需要JS中递归的帮助吗_Recursion - Fatal编程技术网

Recursion 需要JS中递归的帮助吗

Recursion 需要JS中递归的帮助吗,recursion,Recursion,我很难理解递归。我可以做简单的递归,但这对我来说并不容易。我的目标是加速这个搜索算法。我猜递归会有所帮助。在一个简单的43节点树上,它需要15秒,子节点就是这样。下面是我的展开递归fomr代码的工作原理 var nodeList = new Array(); var removeList = new Array(); var count = 0; var foundInThisNodeTree; var find = fu

我很难理解递归。我可以做简单的递归,但这对我来说并不容易。我的目标是加速这个搜索算法。我猜递归会有所帮助。在一个简单的43节点树上,它需要15秒,子节点就是这样。下面是我的展开递归fomr代码的工作原理

var nodeList = new Array();
         var removeList = new Array();
         var count = 0;
         var foundInThisNodeTree;

         var find = function ( condition )
         {
         }

         while ( this.treeIDMap.igTree( "nodeByPath", count ).data() )
         {
             var foundInThisNodeTree = false;
             var n;
             n = this.treeIDMap.igTree( "nodeFromElement", this.treeIDMap.igTree( "nodeByPath", count ) )
             if ( n.data.ITEM.indexOf( filter ) > -1 ) { foundInThisNodeTree = true; }
             else {//look deeper
                 var i = 0;
                 while ( this.treeIDMap.igTree( "nodeByPath", count + "_" + i ).data() )
                 {
                     n = this.treeIDMap.igTree( "nodeFromElement", this.treeIDMap.igTree( "nodeByPath", count + "_" + i ) );
                     if ( n.data.ITEM.indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                     else {//look deeper
                         var j = 0;
                         while ( this.treeIDMap.igTree( "nodeByPath", count + "_" + i + "_" + j ).data() )
                         {
                             n = this.treeIDMap.igTree( "nodeFromElement", this.treeIDMap.igTree( "nodeByPath", count + "_" + i + "_" + j ) );
                             if ( n.data.ITEM.indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                             else {//look deeper
                                 var k = 0;
                                 while ( this.treeIDMap.igTree( "nodeByPath", count + "_" + i + "_" + j + "_" + k ).data() )
                                 {
                                     n = this.treeIDMap.igTree( "nodeFromElement", this.treeIDMap.igTree( "nodeByPath", count + "_" + i + "_" + j + "_" + k ) );
                                     if ( n.data.ITEM.indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                                     k++;
                                 }
                             }

                             j++;
                         }
                     }
                     i++;
                 }
             }
             if ( !foundInThisNodeTree ) this.treeIDMap.igTree("removeAt", ""+count )
             else count++;
         }
米尔科·埃尔曼建议的第二次修订

var nodeList = new Array();
         var removeList = new Array();
         var count = 0;
         var foundInThisNodeTree;
         filter = filter.toLowerCase();
         while ( this.treeIDMap.igTree( "nodeByPath", count ).data() )
         {
             var foundInThisNodeTree = false;
             var n;
             n = this.treeIDMap.igTree( "nodeFromElement", this.treeIDMap.igTree( "nodeByPath", count ) )
             if ( n.data.ITEM.toLowerCase().indexOf( filter ) > -1 ) { foundInThisNodeTree = true; }
             else {//look deeper
                 var i = 0;
                 n = this.treeIDMap.igTree( "childrenByPath", count );
                 while ( n[i] )
                 {
                     if ( n[i].data.ITEM.indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                         var j = 0;
                         n = this.treeIDMap.igTree( "childrenByPath", count + "_" + i );
                         while ( n[j]  )
                         {
                             if ( n[j].data.ITEM.indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                                 var k = 0;
                                 n = this.treeIDMap.igTree( "childrenByPath", count + "_" + i + "_" + j);
                                 while ( n[k] )
                                 {
                                     if ( n[k].data.ITEM.indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                                     k++;
                                 }
                             j++;
                         }

                     i++;
                 }
             }
             if ( !foundInThisNodeTree ) this.treeIDMap.igTree("removeAt", ""+count )
             else count++;
         }
 var count = 0;
 var foundInThisNodeTree;
 filter = filter.toLowerCase();
 while ( this.treeIDMap.igTree( "nodeByPath", count ).data() )
 {
     var foundInThisNodeTree = false;
     var n;
     n = this.treeIDMap.igTree( "nodeFromElement", this.treeIDMap.igTree( "nodeByPath", count ) )
     if ( n.data.ITEM.toLowerCase().indexOf( filter ) > -1 ) { foundInThisNodeTree = true; }
     if ( n.data.branch )//look at all childer under the root node
     {      
         var i = 0;
         n = n.data.branch;
         while ( n[i] )//look at all childer under the root node
         {      
            if ( n[i].ITEM.toLowerCase().indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
            while ( n[i].branch )//look deeper
            {
                var j = 0;
                n = n[i].branch;
                if ( n[j].ITEM.toLowerCase().indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                while ( n[j].branch )//look deeper
                {
                    var k = 0;
                    n = n[j].branch;
                    if ( n[k].ITEM.toLowerCase().indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                    k++;
                }

                j++;
            }

             i++;
         }
     }
     if ( !foundInThisNodeTree ) this.treeIDMap.igTree("removeAt", ""+count )
     else count++;
 }
***使用我的可分支树获取数据无需调用树***

var nodeList = new Array();
         var removeList = new Array();
         var count = 0;
         var foundInThisNodeTree;
         filter = filter.toLowerCase();
         while ( this.treeIDMap.igTree( "nodeByPath", count ).data() )
         {
             var foundInThisNodeTree = false;
             var n;
             n = this.treeIDMap.igTree( "nodeFromElement", this.treeIDMap.igTree( "nodeByPath", count ) )
             if ( n.data.ITEM.toLowerCase().indexOf( filter ) > -1 ) { foundInThisNodeTree = true; }
             else {//look deeper
                 var i = 0;
                 n = this.treeIDMap.igTree( "childrenByPath", count );
                 while ( n[i] )
                 {
                     if ( n[i].data.ITEM.indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                         var j = 0;
                         n = this.treeIDMap.igTree( "childrenByPath", count + "_" + i );
                         while ( n[j]  )
                         {
                             if ( n[j].data.ITEM.indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                                 var k = 0;
                                 n = this.treeIDMap.igTree( "childrenByPath", count + "_" + i + "_" + j);
                                 while ( n[k] )
                                 {
                                     if ( n[k].data.ITEM.indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                                     k++;
                                 }
                             j++;
                         }

                     i++;
                 }
             }
             if ( !foundInThisNodeTree ) this.treeIDMap.igTree("removeAt", ""+count )
             else count++;
         }
 var count = 0;
 var foundInThisNodeTree;
 filter = filter.toLowerCase();
 while ( this.treeIDMap.igTree( "nodeByPath", count ).data() )
 {
     var foundInThisNodeTree = false;
     var n;
     n = this.treeIDMap.igTree( "nodeFromElement", this.treeIDMap.igTree( "nodeByPath", count ) )
     if ( n.data.ITEM.toLowerCase().indexOf( filter ) > -1 ) { foundInThisNodeTree = true; }
     if ( n.data.branch )//look at all childer under the root node
     {      
         var i = 0;
         n = n.data.branch;
         while ( n[i] )//look at all childer under the root node
         {      
            if ( n[i].ITEM.toLowerCase().indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
            while ( n[i].branch )//look deeper
            {
                var j = 0;
                n = n[i].branch;
                if ( n[j].ITEM.toLowerCase().indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                while ( n[j].branch )//look deeper
                {
                    var k = 0;
                    n = n[j].branch;
                    if ( n[k].ITEM.toLowerCase().indexOf( filter ) > -1 ) { foundInThisNodeTree = true; break; }
                    k++;
                }

                j++;
            }

             i++;
         }
     }
     if ( !foundInThisNodeTree ) this.treeIDMap.igTree("removeAt", ""+count )
     else count++;
 }
不要总是使用“nodeByPath”,而应该使用“childrenByPath”

这将最小化igTree上的搜索调用


PS:使用而不是替换;)

您并不是在递归地执行此操作。对于层次结构中的每个级别,您都在重复代码。您需要的是一个helper函数,它将当前节点路径作为参数,并递归地为其每个子节点调用相同的方法,并将其id添加到当前节点的路径中。递归意味着代码应该适用于任意深度的树。在我看来,你的代码只能在设定的深度下工作

对于速度问题,可能有两个问题。我并没有仔细阅读你的代码,所以我把它留给你去弄清楚哪一个更可能

  • 您可能正在重新访问节点。如果是这样,显然这会影响性能

  • 您正在使用的框架在查找节点时可能会比较慢。一个解决方案可能是找到替代方法来调用框架,该框架适用于您正在做的事情。例如,框架内部可能有一个层次表示,但在传递完整路径时必须重新构建或解析它。寻找采用源和相对路径的方法。如果这不是问题所在,那么框架可能会很慢,您最好读取所有节点并构建自己的内存树来代替它


  • 好的,我找到了一种使用数据提供程序并使用普通Json搜索的方法。不过,如果有人能加快速度,我将不胜感激。我刚从15秒到1秒。这个有我需要的递归

       findInObject = function( obj, prop, val )
     {
         if ( obj !== null && obj.hasOwnProperty( prop ) && obj[prop].toLowerCase().indexOf(val) > -1 )
         {
             return obj;
         } else
         {
             for ( var s in obj )
             {
                 if ( obj.hasOwnProperty( s ) && typeof obj[s] == 'object' && obj[s] !== null )
                 {
                     var result = findInObject( obj[s], prop, val );
                     if ( result !== null )
                     {
                         return result;
                     }
                 }
             }
         }
         return null;
     }
    
     for ( var i = 0; i < this.treeData.length; i++)
     {
         if ( findInObject( this.treeData[i], "ITEM", filter ) ) foundNodes.push( this.treeData[i] )//does the node have a match?
     }
    
     this.treeIDMap.igTree( { dataSource: foundNodes } );
     this.treeIDMap.igTree( "dataBind" );
    
    findInObject=函数(obj、prop、val)
    {
    如果(obj!==null&&obj.hasOwnProperty(prop)&&obj[prop].toLowerCase().indexOf(val)>-1)
    {
    返回obj;
    }否则
    {
    用于(obj中的var s)
    {
    如果(对象的属性和类型为'object'&&obj[s]!==null)
    {
    var结果=findInObject(对象、属性、值);
    如果(结果!==null)
    {
    返回结果;
    }
    }
    }
    }
    返回null;
    }
    for(var i=0;i

    })

    这真的属于代码审查StackExchange。我尝试了一下(参见上面添加的代码),它看起来确实更好。还是一样的速度。我甚至找到了一种方法,只给树打一个电话,但还是一样慢?是的,我解释过了?我说我不知道如何递归地做,我的例子就是它的展开版本。“您正在使用的框架在查找节点时可能会比较慢”是的,这几乎是大部分。