简体   繁体   English

用于树创建的Javascript递归

[英]Javascript recursion for tree creation

i am trying to create funcion for creating decision tree with state of game in every node in game (doesnt matter what game). 我正在尝试创建功能,以在游戏中的每个节点中创建具有游戏状态的决策树(与哪个游戏无关)。 I wrote recursive function (DFS) like this: 我写了这样的递归函数(DFS):

function makeTree(anchor,count,player){
    var subTree=null;
    var nodes=[];
    if(player)var newPlayer=false;
    else var newPlayer=true;
    for (var i = 0; i <= 9; i++) {
        for (var j = 0; j <= 9; j++) {
            if(anchor["state"][i][j]==0){
                var newState=anchor["state"];
                if(player)newState[i][j]=1;
                else newState[i][j]=2;
                var node={name:i+"_"+j, contents:[],state:newState, value:null, player:newPlayer};
                if(count>0){
                    var newCount=count-1;
                    subTree=makeTree(node,newCount,newPlayer);
                    node["contents"]=subTree;
                }
                nodes.push(node);
            }else{
                continue;
            }
        }
    }
    return nodes;
}

And with call: 并致电:

var tree={};
var hrac=true;
var plocha=[[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]];
var state=plocha;
    tree={name: "root",
          contents:[], 
          state:state, 
          value:null, 
          player: hrac};
    tree["contents"]=makeTree(tree,3,hrac);

But the function change variables in different scope, so the output tree will be like this: 但是函数会在不同范围内更改变量,因此输出树将如下所示:

"root" - node - node - node
                     - node
                     - node
                     - node
                     - node

I cant figure out what is going on with variable newState in that function, because after finish the recursion the original variable plocha has the value of the latest node["state"] . 我无法弄清楚该函数中变量newState情况,因为完成递归后,原始变量plocha具有最新的node["state"] Any suggestions what to do? 有什么建议怎么办?

EDIT: Thanks to Bergi i realize that i need to do deep copy of array insted of make reference to it, so i make funcion for copy of array and now this works. 编辑:感谢Bergi,我意识到我需要对要引用的数组进行数组的深层复制,因此我对数组的副本进行了功能化,现在可以使用了。 Thank you Bergi! 谢谢贝尔吉!

Your state property is an array, which is mutable. 您的state属性是一个可变的数组。 On every assignment, you change the one multidimensional array that is the state of all nodes. 在每次分配时,您都将更改一个多维数组,它是所有节点的state You'll want to make every newState a new array, instead of passing your plocha reference recursively through all functions: 您将要使每个newState一个新数组,而不是通过所有函数递归传递plocha引用:

 …
 var newState = anchor["state"].slice(); // create copy of the outer array
 newState[i] = newState[i].slice(); // copy of the row to be modified
 newState[i][j] = player ? 1 : 2;
 …

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

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