繁体   English   中英

尝试在构建二进制树时创建指向父节点的指针

[英]Trying to create a pointer to parent node whilst building binary Tree

我是一个爱好者,对c ++很陌生,我正在努力实现这个树的最佳方式。

我有很多问题,但主要是我试图找出在构建树期间存储指向父节点的指针的最佳方法; 但是当我尝试在preOrder成员函数中访问root->parent->data preOrder root->parent->data值时,我收到了Bus错误 我可以愉快地访问地址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())非常优雅。

非常感谢,Alex

据我所知,问题是:

你的buildTree函数中的这一行,

thisNode->parent = parent;

这个功能,

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

和你的preOrder功能。

父指针未在树构造函数中传递给buildTree,因此默认值为NULL指针。 所以root-> parent将为NULL(有意义,因为它是根)。

然后从root用户开始调用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);
}

我无法重现这个问题,但看起来最可疑的部分是这样的:

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

语句( this->data )绝对没有。 也许你想要(并且应该尝试):

FibTree::Node::Node()
  : data( 0 ),
    left( NULL ),
    right( NULL ),
    parent( NULL )
{
}

解释:您的代码没有初始化数据和指针,因此它们未初始化。 如果您打印指针,通常可以工作,但取消引用它可能会导致您看到的错误。

执行此操作时,您可能会看到parent变为NULL并且您需要检查它:

if( root->parent != NULL ) {
    cout << root->data << "[" << root->parent->data << "]" << ",";
}

暂无
暂无

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

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