简体   繁体   English

在C ++中递归填充N元树

[英]Recursively Populate N-ary Tree in c++

I'm attempting to create the game tree of the tic-tac-toe board game. 我正在尝试创建井字棋盘游戏的游戏树。 I've written some basic methods, but I'm having trouble recursively populating the elements of the tree. 我已经写了一些基本的方法,但是在递归地填充树的元素时遇到了麻烦。

I'm using a Node struct to define the Nodes of the tree. 我正在使用Node结构来定义树的Nodes。 Each node has an array of children. 每个节点都有一个子级数组。

struct node {
  string data;
  int height;
  node * child[9];
};

Each Node stores the content of a game board as a string. 每个节点将游戏板的内容存储为字符串。 * is used to display blanks. *用于显示空白。

So, * * * * * * * * * would be a blank board. 因此, * * * * * * * * *将是一个空白板。

I have a Tree class that implements the tree. 我有一个实现该树的Tree类。

class Tree {
public:
  Tree();
  Tree(string data);
  ~Tree();

  void insert(string data, node * leaf);
  node * get_root();
  void populate(node * n);
  void generate_tree(node * n);
  int number_of_blanks(string);

private:
  void destroy_tree(node * leaf);

  node * root;
  node * temp;
  int count;
};

Tree::Tree(string data) {
  root = new node;
  root->data = data;
  root->height = 0;
  temp = root;
  count = 0;
}

Here is my method for inserting nodes. 这是我插入节点的方法。 It inserts a new node to the first NULL child. 它将新节点插入第一个NULL子级。

void Tree::insert(string data, node * leaf) {
  int i;
  //checks for first NULL child
  for(i = 0; i < 9; i++) {
    if(leaf->child[i] == NULL) {
      //first NULL child is inserted and all its children set to NULL
      leaf->child[i] = new node;
      leaf->child[i]->data = data;
      leaf->child[i]->height = leaf->height + 1;
      break;
    }
  }
}

This code works how I've intended it to, however I'm sure it's not the best method. 这段代码可以按照我的预期工作,但是我确定这不是最好的方法。

Where I'm having the most trouble is recursively populating the tree. 我最麻烦的地方是递归地填充树。 My recursion either ends early, or is an endless loop. 我的递归要么提前结束,要么是一个无休止的循环。 I'm not sure how to approach this problem, as I've never used recursion with a void method. 我不确定如何解决此问题,因为我从未使用过void方法进行递归。

void Tree::generate_tree(node * leaf) {
  int i;
  string data;
  string player;
  int length = number_of_blanks(leaf->data);

  if(leaf->height % 2 == 0)
    player = "X";
  else
    player = "O";


  if(leaf->data.find_last_of('*',8) == string::npos) {
    cout << "This is a leaf!!!!!!!!! " << leaf->data << endl;
    return;
  }


  for(i = 0; i < length; i++) {
    if(leaf->height >=9 )
      return;
    data = leaf->data.replace(count,1,player);
    insert(data,leaf);
    cout << "New Node: " << data << " Height: " << leaf->child[i]->height << endl;
    count++;
    generate_tree(leaf->child[i]);
    count = 0;
  }
}

Any tips, or specific suggestions would be greatly appreciated. 任何提示或具体建议,将不胜感激。 Thank you. 谢谢。

I'd recommend giving node a constructor and to initialize members before the code block that makes up the constructor. 我建议给node一个构造函数,并在组成该构造函数的代码块之前初始化成员。 For example: 例如:

 node(string s, int h) : data(s), height(h) { 
     for (int i=0;i < 9; ++i) 
        child[i] = NULL; 
 }

Similarly for the constructor for Tree : 对于Tree的构造函数类似:

Tree::Tree(std::string data) : root(new node(data,0)), count(0) {}

This makes other parts of the code much simpler. 这使代码的其他部分更加简单。 For example, your insert code would now look like this: 例如,您的insert代码现在如下所示:

void Tree::insert(std::string data, node * leaf) {
  //checks for first NULL child
  for(int i = 0; i < 9; i++) {
    if(leaf->child[i] == NULL) {
      //first NULL child is inserted and all its children set to NULL
      leaf->child[i] = new node(data, leaf->height+1);
      break;
    }
  }
}

I haven't had the time to analyze the rest, but this may make it easier for you to solve. 我没有时间分析其余部分,但这可能使您更容易解决。

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

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