如何在Java中让递归和for循环一起工作?
我需要递归地创建一个树结构。在树中,每个节点都有不同数量的子节点,因此我认为我需要在 for 循环中递归调用该方法。 for 循环的循环次数与当前节点有子节点的次数相同。
该函数首先创建深度 d 中最左边的子级,然后返回(或应该返回)到前一个深度并创建另一个深度,依此类推。我相信你知道我在这里的意思。所以我试图以这种方式创建整棵树。我设置了一个基本情况,以便如果满足基本情况的条件,则不再递归调用该方法。问题是我的程序以某种方式设法克服了这些条件并继续递归调用该方法,尽管它不应该这样做。
代码如下:
private void makeTree(GameState prevState, Vector moves, Node parentNode, int index, int depthLimit) {
if(prevState.getPossibleMoveCount(index) != 0){
for(int i = 0; i < moves.size(); i++){
Move thisMove = (Move)moves.get(i);
GameState newState = prevState.getNewInstance(thisMove);
Node child = new Node(newState, thisMove);
parentNode.addChild(child);
child.setParent(parentNode);
if((child.getDepth() + 1) < depthLimit){
int newIndex = switchIndex(index);
Vector newMoves = newState.getPossibleMoves(newIndex);
makeTree(newState, newMoves, child, newIndex, depthLimit);
}else{
child.setScore(newState.getMarkCount(index));
}
}
}
}
这里的 Node 类不是 Java 默认的 Node 类,而是属于该接口的类。我知道不应该创建一个与某些默认 Java 类同名的类,但这个接口不是我的。我只需要实施它。 Node 类不会与 Java Node 类冲突。所以我认为这不会造成任何问题。
真正的问题是 if((child.getDepth() + 1) < heightLimit) 似乎对程序没有任何影响。程序每次都会继续递归调用该方法,直到达到深度 61,此时内存耗尽。深度限制设置为 5,但正如我所说,这似乎并不重要。
要点是,当程序处于深度“深度限制-1”时,它应该停止递归调用,并为当前节点的子节点设置分数,然后继续为当前节点执行所有这些操作。恰好依次出现的下一个元素。因为这个方法不返回任何东西(void方法),所以不需要任何返回调用,对吗?
我希望你明白我正在尝试做什么以及出了什么问题。任何帮助表示赞赏。如果您需要更多信息,请直接询问,我会尽力更仔细地解释这一点。
提前致谢。
E:方法中的深度限制参数在第一次调用之前给出,并且在树创建过程中不会改变。
I need create a tree structure recursively. In the tree each node has different amount of children, so I think I need to call the method recursively in a for loop. The for loop loops as many times as the current node has children.
The function first creates the leftmost child in depth d and then returns (or is supposed to return) back to the previous depth and creates another and so on. I believe you know what I mean here. So I'm trying to create the whole tree in this way. I have set a base case so that if the conditions of the base case are met, the method isn't called recursively anymore. The problem is that my program somehow manages to get past those conditions and continues calling the method recursively, though it shouldn't do that.
Here's the code:
private void makeTree(GameState prevState, Vector moves, Node parentNode, int index, int depthLimit) {
if(prevState.getPossibleMoveCount(index) != 0){
for(int i = 0; i < moves.size(); i++){
Move thisMove = (Move)moves.get(i);
GameState newState = prevState.getNewInstance(thisMove);
Node child = new Node(newState, thisMove);
parentNode.addChild(child);
child.setParent(parentNode);
if((child.getDepth() + 1) < depthLimit){
int newIndex = switchIndex(index);
Vector newMoves = newState.getPossibleMoves(newIndex);
makeTree(newState, newMoves, child, newIndex, depthLimit);
}else{
child.setScore(newState.getMarkCount(index));
}
}
}
}
The Node class here isn't the Java default Node class, but instead a class that belongs to this interface. I know one shouldn't create a class with a same name that's already given to some default Java class, but this interface isn't mine. I just have to implement it. The Node class doesn't collide with the Java Node class. So I don't think that causes any problems.
The real problem is that the if((child.getDepth() + 1) < depthLimit) doesn't seem to have any effect on the program. The program continues calling the method recursively every time until it hits the depth 61 at which point memory runs out. The depth limit is set to 5, but like I said, it doesn't seem to matter.
The point is that when the program is in the depth "depthLimit -1" it should stop the recursive call and instead set the scores for the children of the current node and then continue doing all those things for the next element that happens to be in turn. Because this method doesn't return anything (void method), there don't need to be any return calls, right?
I hope you get the idea what I'm trying to do and what goes wrong. Any help is appreciated. And if you need any more information, please just ask and I'll try to explain this more carefully.
Thanks in advance.
E: The depthLimit argument in the method is given before the first call and it doesn't change during the tree creation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题不在于您发布的部分。
我为您的方法中使用的所有类和方法添加了虚拟实现,并且它运行得很好,在级别 6 处停止。
(我对泛型类型进行了一些更改以避免编译器警告。)
以下是深度限制 = 4 的输出:
Your problem is not in the part you posted.
I added dummy implementations for all the classes and methods used in your method, and it runs quite fine, stopping at level 6.
(I changed a bit to generic types to avoid compiler warnings.)
Here is the output for depthLimit=4:
当您在 if 语句内部调用时,
您期望深度来自哪里?看起来您正在将深度传递给 makeTree 例程,但期望它是当前节点的深度(也许是 this.depth ?)
When you call
inside of your if statement where are you expecting depth to come from? It looks like you're passing in depth to the makeTree routine, yet expecting it to be the depth of the current node (this.depth maybe?)