简体   繁体   English

用Java语言进行简单递归的问题

[英]Problem with simple Recursion in Javascript

Here is the code snippet, this goes into an infinite loop - with the "too much recursion error". 这是代码段,这会陷入无限循环-带有“太多的递归错误”。

SW = {
    WData : {
        wf : {

            roots : [852,1517,1523,1540],
            leaves : [],
            features : {
                852:  { "cf":"855,1848"},
                1517: { "cf":"1929,1930"},
                1523: { "cf":""},
                1540: { "cf":"1546,1549"},
                855:  { "cf":"" },
                1848: { "cf":""},
                1929: { "cf":""},
                1930: { "cf":""},
                1546: { "cf":"1600"},
                1549: { "cf":""},
                1600: { "cf":""}                
            }
        }
    },

    init: function init(){                
        this.buildTree();
        //console.log(this.WData.wf.leaves);
    },

    buildTree : function(){
        this.getLeaves(this.WData.wf.roots);
    },

    getLeaves: function(roots){
        for(var i in roots){
            var root = this.WData.wf.roots[i];

            if( this.WData.wf.features[ root ].cf === ""){

                this.WData.wf.leaves.push( root );
                return false;
            }
            else{
                this.getLeaves( this.WData.wf.features[root].cf.split(',').map(Number) );
            }
        }
        return false;
    }
}


SW.init();

Not able to understand what is the problem here. 无法理解这里的问题。 I have a feeling i am making quite a simple mistake. 我感觉自己犯了一个简单的错误。

http://jsfiddle.net/eWGG8/4/ http://jsfiddle.net/eWGG8/4/

You have a couple of problems: 您有几个问题:

  1. At the start of your loop, you should be using roots rather than this.WData.wf.roots , eg. 在你的循环的开始,你应该用roots而不是this.WData.wf.roots ,例如。 like this: 像这样:

     var root = roots[i]; 
  2. You are trying to push new items onto a string rather than an array, so in your structure, change leaves to the following: 您正在尝试将新项目推送到字符串而不是数组上,因此在您的结构中,将叶子更改为以下内容:

     leaves : ["asdf"], 
  3. This then results in an error when you try to assign it to your results div (in the jsFiddle), so use join to create a string 当您尝试将其分配给结果div(在jsFiddle中)时,这将导致错误,因此请使用join创建一个字符串

     $('#result').append( SW.WData.wf.leaves.join(",") ); 

Your termination condition is in this line: 您的终止条件在此行中:

for(var i in roots){

so why not follow it up with something like this console.log(i) to see if it is doing what you think. 因此,为什么不继续使用类似console.log(i)来查看它是否在按照您的想法进行。 This type of technique is often faster than a debugger. 这种技术通常比调试器快。

You keep referencing the original tree in your recursion - you likely want to split on the node that you passed in recursively: 您在递归中一直引用原始树-您可能想在递归传入的节点上拆分:

getLeaves: function(roots){
for(var i in roots){
    var root = roots[i];

    if( !(root.cf)){

        this.WData.wf.leaves.push( root );
        return false;
    }
    else{
        this.getLeaves( root.cf.split(',').map(Number) );
    }
}
return false;

From what I can tell, you need to change the values of the cf properties in the features entries to reflect that you already parsed them. 据我所知,您需要更改features条目中cf属性的值以反映您已经对其进行了解析。 If you don't, this branch of your code 如果不这样做,则此代码分支

else{
    this.getLeaves( this.WData.wf.features[root].cf.split(',').map(Number) );
}

will infinitely add values to the leaves Array , if I'm correct. 如果我是对的,将无限地向leaves Array添加值。

So, try adding something like: 因此,尝试添加如下内容:

 else {

    var cf = this.WData.wf.features[root].cf ; // save the current cf value

    //... change this.WData.wf.features[root].cf to the desired value

        this.getLeaves( cf.split(',').map(Number) ) ;


 }

By the way, Array.map is a browser implementation specific function (for Gecko browsers, eg Firefox). 顺便说一下, Array.map是浏览器实现特定的功能(对于Gecko浏览器,例如Firefox)。 In other words your code won't work where that is not implemented. 换句话说,您的代码在未实现的地方将无法正常工作。

Also, in my opinion, you would do good to add comments and fold out your code to make it more readily understandable. 另外,我认为,最好添加注释并折叠代码以使其更易于理解。 The way it is written now it is terse but hard to grasp its essential purpose. 现在的书写方式虽然简洁但难以把握其基本目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM