c++ - 在构建二叉树时尝试创建指向父节点的指针

标签 c++ recursion binary-tree

我是一名狂热者,对 C++ 还很陌生,我正在努力寻找实现这棵树的最佳方式。

我有很多问题,但主要是我想找出在构建树期间存储指向父节点的指针的最佳方法;但是当我尝试在 preOrder 成员函数中访问 root->parent->data 值时,出现了一个Bus Error。我可以愉快地访问地址 root->parent,这输出没有错误。

谁能提出我在这里做的根本错误的事情?我认为这可能是一个范围问题,但可能有更好的构建树的方法?

class FibTree {

    class Node {
    public:
        int data;
        Node const* left;
        Node const* right;
        Node const* parent;
        Node (void);
    };
    Node const* root; // 'root' pointer to constant Node

public:
    FibTree (int);
    Node const* getRoot(void);
    void preOrder(Node const* root);

};

// Tree constructor
FibTree::FibTree(int n) {
    this->root = buildTree( n );
};

FibTree::Node const* FibTree::getRoot(void) {
    return this->root;
}

private:
    static Node* buildTree( int n, Node* parent = NULL );
};

void FibTree::preOrder(Node const* root) {
    if (root == NULL)
        return;
    // *** This prints the address of the parent node correctly
    cout << root->data << "[" << root->parent << "]" << ",";
    // *** This produces a 'Bus Error'
    cout << root->data << "[" << root->parent->data << "]" << ",";
    preOrder(root->left);
    preOrder(root->right);
}

FibTree::Node* FibTree::buildTree( int n, Node* parent ) {
    // *** Is there a scope issue with 'thisNode' here?
    Node* thisNode = new Node();
    thisNode->parent = parent;
    if (n < 2) {
        thisNode->left = NULL;
        thisNode->right = NULL;
        thisNode->data = n;
        return thisNode;
    } else {
        thisNode->left = buildTree( n - 1 , thisNode );
        thisNode->right = buildTree( n - 2, thisNode );
        thisNode->data = thisNode->left->data + thisNode->right->data;
        return thisNode;
    }
}

// Node constructor
FibTree::Node::Node(void) {
    this->data;
    this->left;
    this->right;
    this->parent;
};

int main () {
    FibTree f(n);
    f.preOrder(f.getRoot());
    return 0;
}

此外,是否有一种标准方法可以避免将 root 节点传递给遍历函数,并让成员函数遍历创建的 root 节点。因为我不觉得 f.preOrder(f.getRoot()) 很优雅。

非常感谢, 亚历克斯

最佳答案

据我所知,问题是以下因素的组合:

buildTree 函数中的这一行,

thisNode->parent = parent;

这个函数,

// Tree constructor
FibTree::FibTree(int n) {
    this->root = buildTree( n );
};

和您的 preOrder 函数。

父指针没有被传递给树构造函数中的 buildTree,因此默认为 NULL 指针。所以 root->parent 将为 NULL(有道理,因为它是根)。

然后从根开始调用 preOrder 函数。 Root 有一个为 NULL 的 parent,但您永远不会检查它(您会检查 root 本身是否为 NULL)。

您应该改为执行以下操作:

void FibTree::preOrder(Node const* root) {
    if (root == NULL)
        return;

    if(root->parent == NULL) {
      cout << root->data << " [I am the root] " << endl;
    }
    else {
      cout << root->data << "[" << root->parent->data << "]" << ",";
    }
    preOrder(root->left);
    preOrder(root->right);
}

关于c++ - 在构建二叉树时尝试创建指向父节点的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15727555/

相关文章:

java - 编写一个递归方法,返回 'A' 在传递的字符串中出现的次数

c - 在 C 中处理长递归产生式时如何防止堆栈溢出?

c++ - 二叉树遍历为什么需要检查pre->right != current

c++ - 如何使用 Visual Studio 6 C++ 检测字符串中的换行符

c++ - 如何将 dd-mmm-yyyy 和 now() 转换为天?

c++ - 将 Windows.h 与 GLFW.h 连接起来

c++ - 使用递归(无循环)C++ 对数组进行冒泡排序

c++ - 如何使用 std :from_chars 在 C++17 中从字符串转换为 int/float

c++ - 错误 "System.AccessViolationException"显示切割后的二叉树 Visual C++

java - Java中的二叉树不使用add方法