【问题标题】:Reading a binary tree from file从文件中读取二叉树
【发布时间】:2013-01-25 04:29:40
【问题描述】:

我一直在处理来自文件的输入,并认为我的逻辑是正确的,但我的节点没有正确链接。我能够正确设置根并且程序能够遍历字符串并正确加载节点,只是不链接它们。谁能帮我梳理一下我的逻辑并找出问题所在?

输入字符串为(A(B(DG)E)(C()F))。

    struct node
    {
     string data;
     node* left;
     node* right;
    };

    void tree::build_tree(string &input, int i, node *n)
    {
     if(i > input.length())
          return *n = NULL;

     if(input[i] == '(')
     {
      string data; string temp;
      int prev_i = i;

     //get_data retrieves the identifier
     data = get_data(input, temp, i+1);

     //get_data_num retrieves the new position in the string
     i = get_data_num(input, temp, i+1);

     if(input[prev_i] == '('&& input[i] == ')')
     {
      i += 1;
      *n = NULL;
     }
     else
     {
      // Allocate a new node and assign the data and 
      // set the pointer to the branches to null
      *n = new node;
     (*n)->data = data;
     (*n)->left = NULL;
     (*n)->right = NULL;

     if(input[i] == ' ')
     {i += 1; }

     //Pass the address of the nodes
     build_tree(input, i, &(*n)->left);
     build_tree(input, i, &(*n)->right);
     }

   }

   else if(isalnum(input[i]) || input[i] == '_' || input[i] == '-')
   {
     string data; string temp;
     int prev_i = i;

     data = get_data(input, temp, i);
     i = get_data_num(input, temp, i);

     if(input[prev_i] == '('&& input[i] == ')')
     {
      i += 1;
      *n = NULL;
     }
     else
     {
      *n = new node;
      (*n)->data = data;
      (*n)->left = NULL;
      (*n)->right = NULL;

      if(input[i] == ' ')
      { i += 1; }

     build_tree(input, i, &((*n)->left));
     build_tree(input, i, &((*n)->right));
   }
  }

   else if(input[i] == ' ')
   {
    i += 1;
   }

    else if(input[i] == ')')
    {
     i += 1;
     *n = NULL;
    }

    else
    {
     cout << "The input tree is not in the correct format!" << endl;
    }
    }

【问题讨论】:

  • 参数i应该是一个参考。否则,两个连续的递归调用(解析左右子节点)将在同一个位置读取!只需在参数列表中尝试int &amp;i,无需其他更改,然后报告结果。
  • @leemes 我试过了,结果还是一样。
  • 请发布您的节点结构。它可能有助于理解问题。
  • @Glenn 好的,我已经发布了。
  • @user2057191 谢谢。那非常有帮助。我在下面提供了一个答案,因为它不适合发表评论。

标签: c++ recursion file-upload tree binary-tree


【解决方案1】:

我认为问题在于您没有设置左右指针的值。您正在传递指针的值。您需要将指针传递给指针(左和右),以设置结构中的值。另一种选择是使用引用而不是指针。

以下是我对您提供的代码所做的修改:

  • 将节点参数的 build_tree 调用更改为指向 一个指针。
  • 相应地更改了值的分配。
  • 更改了对 build_tree 的调用以传递左右的地址(到 获取指向指针的指针)。
  • 删除分配/条件以设置 root_node。所以当你 调用build_tree你需要传入root的地址。这 将设置节点就像后面的所有节点一样,所以它不需要 作为一个特例。
  • 为左和右添加了 NULL 分配,以防没有 分支(可能不需要这样做,但我觉得这是一个好习惯 确保所有项目都有一些初始值)。
void tree::build_tree(string &input, int i, node **n)
{

  if(input[i] == '(')
  {
    string data; string temp;

    //get_data retrieves the identifier
    data = get_data(input, temp, i+1);

    //get_data_num retrieves the new position in the string
    i = get_data_num(input, temp, i+1);

    // Allocate a new node and assign the data and 
    // set the pointer to the branches to null
    *n = new node;
    (*n)->data = data;
    (*n)->left = NULL;
    (*n)->right = NULL;

    if(input[i] == ' ')
    { i += 1; }

    // Pass the address of the nodes 
    build_tree(input, i, &(*n)->left);
    build_tree(input, i, &(*n)->right);
  }

  else if(isalnum(input[i]) || input[i] == '_' || input[i] == '-')
  {
    string data; string temp;
    data = get_data(input, temp, i);
    i = get_data_num(input, temp, i);

    *n = new node;
    (*n)->data = data;
    (*n)->left = NULL;
    (*n)->right = NULL;

    if(input[i+1] == ' ')
    { i += 1; }

    build_tree(input, i, &((*n)->left));
    build_tree(input, i, &((*n)->right));
  }

  else if(input[i] == ' ')
  {
    i += 1;
  }

  else if(input[i] == ')')
  {
    *n = NULL;
  }

  else
  {
   cout << "The input tree is not in the correct format!" << endl;
  }
}

那么对于初始调用,

build_tree(testString,0,&root);

由于未提供 get_data 和 get_data_num,我无法测试所做的更改。

【讨论】:

  • 非常感谢!这解决了我让节点连接的问题。我只需要进行一些小的调整以适应我更新的代码。
  • 我刚刚又测试了一遍,只有左边的节点在设置。有什么想法吗?
  • 我得先看看,然后再回复你。希望当天晚些时候我会有答案。谢谢。
  • 我怀疑在生成左节点而不是右节点的解析和分配中有一些不正确的地方。另一个问题是在 ( 之后需要一个数据值。但是在您的示例中,您有一个 () 表示没有数据值。我想要更多关于树的外观的解释。例如 (A ( B (DGE)(C () F ),这是否意味着顶部节点没有数据值,或者输入格式不正确?我可能有一些可行的方法,但我可以使用附加信息。跨度>
  • 感谢您的帮助。我最终解决了这个问题。这是函数如何在字符串中移动的问题,只需要一些 if 语句来纠正。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多