显示树的左视图时出错

时间:2017-08-01 15:54:35

标签: c++ debugging data-structures

我自己初始化树,如下所示: -

        push    offset string "Welcome fo Lost Fortune\n\n" (417B88h) 
        mov     eax,dword ptr [__imp_std::cout (41B354h)] 
        push    eax  
        call    std::operator<<<std::char_traits<char> > (41115Eh) 
        add     esp,8 
        push    offset string "Please enter the following for y"... (417B40h) 
        mov     eax,dword ptr [__imp_std::cout (41B354h)] 
        push    eax  
        call    std::operator<<<std::char_traits<char> > (41115Eh) 
        add     esp,8 
        push    offset string "Enter a number: " (417B2Ch) 
        mov     eax,dword ptr [__imp_std::cout (41B354h)] 
        push    eax  
        call    std::operator<<<std::char_traits<char> > (41115Eh) 
        add     esp,8 

当我运行上面的程序时,对于所有其他函数(由于大代码我已删除)工作正常,但该函数意味着显示左视图和右视图不起作用。它既没有显示任何错误。当我运行程序时,它显示分段错误: - 输出 -

#include<iostream>
#include<queue>
#include<string>
using namespace std;
struct node{
    int data = 0;
    node* left = nullptr;
    node* right = nullptr;
};
void construct(struct node* &tree)
{
    tree->data = 66;
    tree->left = new node;
    tree->right = new node;
    tree->left->left = new node;
    tree->left->left->left = new node;
    tree->left->left->right = new node;
    tree->left->left->left->left = new node;
    tree->left->left->left->right = new node;
    tree->left->left->left->right->right = new node;
    tree->right->right = new node;
    tree->right->left = new node;
    tree->right->left->left = new node;
    tree->right->left->right = new node;
    tree->right->left->right->left = new node;
    tree->left->data = 50;
    tree->left->left->data = 46;
    tree->left->left->left->data = 89;
    tree->left->left->right->data = 37;
    tree->left->left->left->left->data = 53;
    tree->left->left->left->right->data = 81;
    tree->left->left->left->right->right->data = 86;
    tree->right->data = 72;
    tree->right->left->data = 78;
    tree->right->right->data = 71;
    tree->right->left->left->data = 87;
    tree->right->left->right->data = 35;
    tree->right->left->right->left->data = 17;
}
void show_tree_view(struct node* const &tree, string view)
{
    queue<struct node*> box;
    box.push(tree);
    cout << box.front()->data << " ";
    int size = box.size();
    while(!box.empty())
    {
        for(int i = 0; i < size; ++i)
        {
            struct node* current = box.front();
            box.pop();
            if(current->left != nullptr) box.push(current->left);
            if(current->right != nullptr) box.push(current->right);
        }
        size = box.size();
        if(view == "left") cout << box.front()->data << " ";
        if(view == "right") cout << box.back()->data << " ";
    }
    cout << endl;
}
int main()
{
    struct node* tree = new node;
    construct(tree);
    /*int height = calculate_height_by_recursion(tree);
    cout << "height with recursion : " << height << endl;
    height = calculate_height_without_recursion(tree);
    cout << "height without recursion : " << height << endl;*/
    cout << "Left view of tree is : ";
    show_tree_view(tree, "left");
    //cout << "Right view of tree is : ";
    //show_tree_view(tree, "right");
    return 0;
}

我调试了上面的内容,但未能发现错误。建议是必需的。

1 个答案:

答案 0 :(得分:0)

一个错误是您的show_tree_view函数无法检查此处std::queue是否为空:

 if (view == "left") cout << box.front()->data << " ";
 if (view == "right") cout << box.back()->data << " ";

在此代码之前,您在循环中调用box.pop(),因此box可能为空。

我拿了你的代码并使用Visual Studio运行它,它停止时出现错误,指向上面的一组行。实际上,size为0,因此调用box.front()box.back()将导致出现未定义的行为(在您的情况下,发生分段错误)。

因此,您应先检查box.empty(),如果它不是空的,则可以安全地拨打box.front()box.back()