简体   繁体   English

分割故障有序遍历

[英]segmentation fault inorder traversal

i keep getting segmentation fault (core dump) because of the following lines : 由于以下几行,我不断遇到分段错误(核心转储):

void inOrder(){
        if(this->isNull())
            return ;
        this->left->inOrder();
        cout<<this->key<<' ';
        this->right->inOrder();
}

for the same input sometimes this function displays what is supposed to but in most cases just segfault. 对于相同的输入,有时此功能会显示应有的内容,但在大多数情况下,仅显示段错误。 im pretty sure its not because of isNull() function: 我很确定不是因为isNull()函数:

bool isNull(){
        return this->null;
    }

where null is a private bool initialized with true by pseudoconstructor the whole code could be seen here: http://pastebin.com/RiiwqY3K Thank you :). 其中null是由伪构造函数初始化为true的私有布尔,可以在此处查看整个代码: http : //pastebin.com/RiiwqY3K谢谢:)。

Consider a case where there is a node (let it be the root node) which does not have a left child, so, 考虑存在一个没有left子节点的节点(让它成为根节点)的情况,因此,

root->left = NULL

So, when you call this->left->inOrder() you are calling the function on a null pointer (dereferencing a null pointer), which is a segmentation fault! 因此,当您调用this->left->inOrder()您是在空指针(取消引用空指针)上调用函数,这是分段错误!

So, you do not exactly need a isNull function to check, you just need to add if conditions before you call inOrder something like this :: 所以,你不完全需要isNull函数来检查,你只需要添加if你之前打电话条件inOrder像这样::

if(this->left != NULL)
    this->left->inOrder();

Similarly for right subtree. 对于right子树也是如此。 I am assuming that you are explicitly handling the case in which the root of the tree is NULL, inOrder is called on it from main , so you also put an if condition in the main (or anywhere) before calling this function. 我假设您正在显式处理树的根为NULL的情况, inOrder是从main调用inOrder ,因此在调用此函数之前,还要在main (或任何位置)中放置if条件。

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

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