C++ 获得';sigbrt';这里有错误吗?

C++ 获得';sigbrt';这里有错误吗?,c++,pointers,binary-tree,tree-traversal,C++,Pointers,Binary Tree,Tree Traversal,问题是: “连接同一级别的节点 给定一个二叉树,连接处于同一级别的节点。将为相同的节点提供一个加法nextRight指针 最初,所有nextRight指针都指向垃圾值。您的函数应将这些指针设置为指向每个节点的下一个右侧。” 我的策略: 执行级别顺序遍历 将所有元素存储在向量中 若要表示级别的更改,请将nullptr添加到矢量 迭代向量并使当前节点nextRight指针字段包含第i+1个元素的值 我的输出: 运行时错误: 运行时错误中止信号来自中止(3)(SIGABRT) 我的代码(仅适用

问题是:

“连接同一级别的节点 给定一个二叉树,连接处于同一级别的节点。将为相同的节点提供一个加法nextRight指针

最初,所有nextRight指针都指向垃圾值。您的函数应将这些指针设置为指向每个节点的下一个右侧。”

我的策略:

  • 执行级别顺序遍历
  • 将所有元素存储在向量中
  • 若要表示级别的更改,请将nullptr添加到矢量
  • 迭代向量并使当前节点nextRight指针字段包含第i+1个元素的值
  • 我的输出:
    运行时错误:
    运行时错误中止信号来自中止(3)(SIGABRT)

    我的代码(仅适用于函数):

    /*结构节点
    {
    int数据;
    结构节点*左;
    结构节点*右;
    结构节点*nextRight;
    }; */
    空连接(节点*根)
    {
    //所有节点地址的向量
    //对于每套nxtrt下一个ele
    //在级别顺序中,在每个级别后添加一个nullptr作为分隔符
    queueq;
    如果(根)
    {
    q、 推(根);
    q、 推(空压);
    }
    向量;
    节点*curr;
    而(!q.empty())
    {
    curr=q.front();
    q、 pop();
    v、 推回(当前);
    if(curr==nullptr)
    {
    q、 推动(电流);
    继续;
    }
    如果(当前->左)
    {
    q、 按(当前->左);
    }
    如果(当前->右侧)
    {
    q、 推送(当前->右侧);
    }
    }
    对于(int i=0;i<(int)(v.size()-1);+i)
    {
    v[i]->nextRight=v[i+1];//使每个节点指向右侧节点
    }
    }
    

    我试图找出此错误的来源,但未能找到。

    产生这种输出的原因是什么?在实际使用
    connect
    和-g的地方构建一些可执行文件,例如
    $g++node.cpp-g
    ,然后使用
    gdb
    运行它:
    $gdb a.out
    。获得异常后,键入
    bt full
    ()。这将显示错误的确切位置。尝试准备一些最小的示例(尽管我相信这不是您的用例):

    #包括
    使用名称空间std;
    结构体类型
    {
    int数据;
    结构节点*左;
    结构节点*右;
    结构节点*nextRight;
    };
    空连接(节点*根)
    {
    //所有节点地址的向量
    //对于每套nxtrt下一个ele
    //在级别顺序中,在每个级别后添加一个nullptr作为分隔符
    queueq;
    如果(根)
    {
    q、 推(根);
    q、 推(空压);
    }
    向量;
    节点*curr;
    而(!q.empty())
    {
    curr=q.front();
    q、 pop();
    v、 推回(当前);
    if(curr==nullptr)
    {
    q、 推动(电流);
    继续;
    }
    如果(当前->左)
    {
    q、 按(当前->左);
    }
    如果(当前->右侧)
    {
    q、 推送(当前->右侧);
    }
    }
    对于(int i=0;i<(int)(v.size()-1);+i)
    {
    v[i]->nextRight=v[i+1];//使每个节点指向右侧节点
    }
    }
    int main(){
    节点t;
    t、 数据=5;
    连接(&t);
    返回0;
    }
    
    我得到的错误是:

    Reading symbols from a.out...done.
    (gdb) r
    Starting program: /home/mpiotrowski/KeyTestSpyro/PiecewiseQuadratic/a.out 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
    36         if(curr->left)
    (gdb) bt
    #0  0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
    #1  0x0000555555554c79 in main () at test2.cpp:55
    (gdb) bt full
    #0  0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
            q = std::queue wrapping: std::deque with 2 elements = {0x20ce2d8d48550020, 0x0}
            v = std::vector of length 5, capacity 8 = {0x7fffffffdd50, 0x0, 0x555555556c70 <__libc_csu_init>, 0x0, 0x20ce258d4c544155}
            curr = 0x20ce258d4c544155
    #1  0x0000555555554c79 in main () at test2.cpp:55
            t = {data = 5, left = 0x0, right = 0x555555556c70 <__libc_csu_init>, nextRight = 0x555555554910 <_start>}
    
    从a.out读取符号…完成。
    (gdb)r
    启动程序:/home/mpiotrowski/KeyTestSpyro/PiecewiseQuadratic/a.out
    程序接收信号SIGSEGV,分段故障。
    测试2处的0x0000555554B17 in connect(根=0x7fffffffdd50)。cpp:36
    36如果(当前->左)
    (gdb)英国电信
    #测试2处的0 0x0000555554B17 in connect(根=0x7fffffffdd50)。cpp:36
    #test2处的main()中有1 0x00005554C79。cpp:55
    (gdb)英国电信全部
    #测试2处的0 0x0000555554B17 in connect(根=0x7fffffffdd50)。cpp:36
    q=std::队列包装:std::带有2个元素的deque={0x20CE2D48D4855020,0x0}
    v=std::长度为5的向量,容量为8={0x7FFFFFDD50,0x0,0x556C70,0x0,0x20ce258d4c544155}
    电流=0x20ce258d4c544155
    #test2处的main()中有1 0x00005554C79。cpp:55
    t={data=5,left=0x0,right=0x556C70,nextRight=0x554910}
    
    因此,根据
    36 if(curr->left)
    我们可以判断
    curr
    为空,并且在某些情况下您正在尝试取消引用它


    重要只是一个提示:如果您对调试器感到不舒服,您可以始终在每一行中添加
    print
    ,并查看缺少哪些打印。您将能够识别可能存在错误的行。

    在实际使用
    连接-g的地方构建一些可执行文件,例如
    $g++node.cpp-g
    ,然后使用
    gdb
    $gdb a.out
    运行它。获得异常后,键入
    bt full
    ()。这将显示错误的确切位置。尝试准备一些最小的示例(尽管我相信这不是您的用例):

    #包括
    使用名称空间std;
    结构体类型
    {
    int数据;
    结构节点*左;
    结构节点*右;
    结构节点*nextRight;
    };
    空连接(节点*根)
    {
    //所有节点地址的向量
    //对于每套nxtrt下一个ele
    //在级别顺序中,在每个级别后添加一个nullptr作为分隔符
    queueq;
    如果(根)
    {
    q、 推(根);
    q、 推(空压);
    }
    向量;
    节点*curr;
    而(!q.empty())
    {
    curr=q.front();
    q、 pop();
    v、 推回(当前);
    if(curr==nullptr)
    {
    q、 推动(电流);
    继续;
    }
    如果(当前->左)
    {
    q、 按(当前->左);
    }
    如果(当前->右侧)
    {
    q、 推送(当前->右侧);
    }
    }
    对于(int i=0;i<(int)(v.size()-1);+i)
    {
    v[i]->nextRight=v[i+1];//使每个节点指向右侧节点
    }
    }
    int main(){
    节点t;
    t、 数据=5;
    连接(&t);
    返回0;
    }
    
    我得到的错误是:

    Reading symbols from a.out...done.
    (gdb) r
    Starting program: /home/mpiotrowski/KeyTestSpyro/PiecewiseQuadratic/a.out 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
    36         if(curr->left)
    (gdb) bt
    #0  0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
    #1  0x0000555555554c79 in main () at test2.cpp:55
    (gdb) bt full
    #0  0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
            q = std::queue wrapping: std::deque with 2 elements = {0x20ce2d8d48550020, 0x0}
            v = std::vector of length 5, capacity 8 = {0x7fffffffdd50, 0x0, 0x555555556c70 <__libc_csu_init>, 0x0, 0x20ce258d4c544155}
            curr = 0x20ce258d4c544155
    #1  0x0000555555554c79 in main () at test2.cpp:55
            t = {data = 5, left = 0x0, right = 0x555555556c70 <__libc_csu_init>, nextRight = 0x555555554910 <_start>}
    
    从a.out读取符号…完成。
    (gdb)r
    启动程序:/home/mpiotrowski/KeyTestSpyro/PiecewiseQuadratic/a.out
    程序接收信号SIGSEGV,分段故障。
    测试2处的0x0000555554B17 in connect(根=0x7fffffffdd50)。cpp:36
    36如果(当前->左)
    (gdb)英国电信
    #0
    
    Reading symbols from a.out...done.
    (gdb) r
    Starting program: /home/mpiotrowski/KeyTestSpyro/PiecewiseQuadratic/a.out 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
    36         if(curr->left)
    (gdb) bt
    #0  0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
    #1  0x0000555555554c79 in main () at test2.cpp:55
    (gdb) bt full
    #0  0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
            q = std::queue wrapping: std::deque with 2 elements = {0x20ce2d8d48550020, 0x0}
            v = std::vector of length 5, capacity 8 = {0x7fffffffdd50, 0x0, 0x555555556c70 <__libc_csu_init>, 0x0, 0x20ce258d4c544155}
            curr = 0x20ce258d4c544155
    #1  0x0000555555554c79 in main () at test2.cpp:55
            t = {data = 5, left = 0x0, right = 0x555555556c70 <__libc_csu_init>, nextRight = 0x555555554910 <_start>}
    
    if(curr == nullptr)
       {
           q.push(curr);  // remove this line
           continue;
       }
    
    q.push(curr->left);
    
    // where curr pushed as curr->left previous cycle
    if(curr->left) 
    
    struct Node
    {
        int data;
        struct Node* left = nullptr;
        struct Node* right = nullptr;
        struct Node* nextRight = nullptr;
    };