Java で N 分木を実装しています。各ノードは、できるだけ多くのノードを持つことができます。問題は、ツリーを構築しようとしたときに発生します。特定の高さのツリーを再帰的に作成し、ノードのリストに基づいて子ノードを割り当てる関数があります。関数を呼び出すと、ルート ノードにはデータが含まれていません。完了すると null を返します。私はN-aryツリーを実装したことがないので、実装についてある程度確信があります。それに関するポインタは本当に感謝しています!
//Creates a tree
void createTree(int height) throws InvalidMoveException,
Modified_Tree.InvalidMoveException {
root = new ListNode();
root = populateTree(moves.root,height,true,0);
}
//Function called by Create tree to populate the tree
//It takes in a ListNode, an int height that determines the height of the tree,
//and a boolean, which is used
//To know whether the node is a black piece/max or a white piece/min
public ListNode populateTree(ListNode root, int height, boolean black, int score)
{
ListNode node = root;
List<ListNode> nodes = new List<ListNode>(10);
//Sets the size of List in node to the int taken
node.setChildNumber(nodes.size());
//return if reached the pre-maximum height
if(height == 1){
for(int i = 0; i < nodes.size(); i++){
//Add the child to the last node
node.addChild(nodes.get(i));
}
return node;
}
else{
for(int j =0; j < node.getChildNumber(); j++){
node = populateTree(node.getChildAt(j),height-1,!black,score);
}
}
return node;
}
どんな助けでも本当に感謝しています!