【问题标题】:Tree recursion going infinitely树递归无限进行
【发布时间】:2020-12-22 09:38:20
【问题描述】:

我编写了一个从向量构造二叉树的代码。问题是当我调用inOrder(root) 时,它会进入无限递归,即root 永远不会变成nullptr

#include <iostream>
#include <vector>
using namespace std;

class Node {
public:
    int data;
    Node* left;
    Node* right;
    explicit Node(int element) {
        data = element;
        left = nullptr;
        right= nullptr;
    }
};

Node* construct_tree(Node* root, vector<int> &vec, int i) {
    if(i < vec.size()) {
        Node* new_node = new Node(vec[i]);
        root = new_node;
        root->left = construct_tree(root, vec, 2*i + 1);
        root->right = construct_tree(root, vec, 2*i + 2);
    }
    return root;
}

void inOrder(Node* root) {
    if (root != nullptr) {
        inOrder(root->left);
        int n = root->data;
        cout << n << " ";
        inOrder(root->right);
    }
}

int main() {
    vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    Node *root = nullptr;
    root = construct_tree(root, vec, 0);
    cout << root->data << " ";
    cout << root->left->data;
    inOrder(root);
    return 0;
}

可能的修复?

【问题讨论】:

  • 正如@MikeCAT 在解决方案中显示的那样,错误在construct_tree 例程中。你在设计函数时犯了一个错误。它递归地构建一棵树并将子树显式分配给当前节点的leftright 成员。因此,它应该返回构建的树的根,即new_node(可能是nullptr,如果i 太大),而不是通过参数传递的root

标签: c++ recursion data-structures tree


【解决方案1】:

你是从construct_tree返回root,所以当i &lt; vec.size()为假时,它可能会将root分配给root-&gt;leftroot-&gt;right,创建循环。

在这种情况下,您将不需要参数 root,因为它的值从未在函数 construct_tree 中使用。

试试这个:

#include <iostream>
#include <vector>
using namespace std;

class Node {
public:
    int data;
    Node* left;
    Node* right;
    explicit Node(int element) {
        data = element;
        left = nullptr;
        right= nullptr;
    }
};

Node* construct_tree( vector<int> &vec, int i) {
    if(i < vec.size()) {
        Node* new_node = new Node(vec[i]);
        // stop using root and use new_node directly
        new_node->left = construct_tree(vec, 2*i + 1);
        new_node->right = construct_tree(vec, 2*i + 2);
        return new_node;
    } else {
        // return nullptr when the condition is false
        return nullptr;
    }
}

void inOrder(Node* root) {
    if (root != nullptr) {
        inOrder(root->left);
        int n = root->data;
        cout << n << " ";
        inOrder(root->right);
    }
}

int main() {
    vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    Node *root = nullptr;
    root = construct_tree(vec, 0); // remove the argument root
    cout << root->data << " ";
    cout << root->left->data;
    inOrder(root);
    return 0;
}

【讨论】:

  • Node* insertLevelOrder(int arr[], Node* root, int i, int n) { if (i &lt; n) { Node* temp = newNode(arr[i]); root = temp; root-&gt;left = insertLevelOrder(arr,root-&gt;left, 2 * i + 1, n); root-&gt;right = insertLevelOrder(arr,root-&gt;right, 2 * i + 2, n); } return root; } 这个函数如何处理数组而不是向量?
猜你喜欢
  • 1970-01-01
  • 2017-02-02
  • 2014-08-17
  • 2021-03-05
  • 1970-01-01
  • 2021-11-15
  • 2012-11-25
  • 2012-03-14
  • 1970-01-01
相关资源
最近更新 更多