我试图让每个父节点(每个节点)向我展示它的子树有多少输赢。
这将允许我玩一台计算机,这样计算机就可以通过转到对O具有最多胜利的下一个子树来优化其移动。
现在,我正在尝试重新排列这棵树,以便它可以简单地显示它的子树有多少输赢。
到目前为止,我有一个有序的树,它有9个世代。
1)第一代有8个兄弟节点(每个都有子节点),或者X有8个可能的移动。
2)第二代有7个兄弟节点(每个都有子节点)或O的7个可能的移动。
3)一直持续到上一代没有兄弟姐妹和完整的棋盘。
4)我有986410个可能的(完整的和不完整的)板。
此方法当前打印整个树中所有可能的wins (对于X和O)的数量。然而,它使树节点的数量加倍,而实际上它应该少于原始数量,因为并不是所有节点都是wins。
public void postOrderTraverse(TreeNode T) {
counter++;
if (T == null) {
return;
} else {
postOrderTraverse(T.firstChild);
postOrderTraverse(T.nextSibling);
// checks diagonals, horizontals and verticals for a set of X's or O's
if (winOrProgress(T.board, X) == true || winOrProgress(T.board, O) == true) {
// prints the game board at this node
char[][] gameBoard = T.board;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
char value = gameBoard[i][j];
System.out.print(value);
}
}
T.firstChild = T;
System.out.print(counter);
System.out.println("Win");
}
}
}
发布于 2018-10-18 10:04:58
你把计数器放在调用之后,它会计算所有的节点,甚至是null节点。将其更改为在else语句中。但这只是你问题的一部分。这将正确地计算所有节点,而不仅仅是获胜者。如果您只想计算获胜节点的数量,则需要将其放入该catch中,而不是其他捕获中。
public void postOrderTraverse(TreeNode T) {
if (T == null) {
return;
} else {
counter++; //this is where you need to put counter, not before the actual call that way you are not counting null nodes. which you would be adding at most 2 nodes for every node that doesn't have any children.
postOrderTraverse(T.firstChild);
postOrderTraverse(T.nextSibling);
https://stackoverflow.com/questions/52864353
复制相似问题