Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 嘿,我在c+中有一个分段错误+; #包括 #包括 #包括 使用名称空间std; typedef结构节点*treePointer; 类型定义结构节点{ int数据; 树冬左子,右子; }; 类堆栈{ 私人: int top; 公众: 向量st; 堆栈():顶部(-1) { } 无效推送(treePointer ptr){ 圣普什图(ptr); top++; } treePointer pop(){ if(st.empty()) 抛出_范围内的_(“Stack::pop():空堆栈”); 树间温度=圣背(); 顶部--; 圣波普背部(); 返回温度; } }; 无效iterInorder(treePointer ptr) { 斯塔克街; 对于(;;){ 对于(;ptr;ptr=ptr->leftChild) 圣普什(ptr); ptr=圣波普(); 如果(!ptr)中断; cout数据; ptr=ptr->rightChild; } } int main() { 节点a1、a2、a3、a4、a5、a6、a7; 树间ptr; a1.data=1,a2.data=2,a3.data=3,a4.data=4,a5.data=5; a6.数据=6,a7.数据=7; a1.leftChild=&a2;a1.rightChild=&a7; a2.leftChild=&a3;a2.rightChild=&a6; a3.rightChild=&a4; a4.leftChild=&a5; iterInorder(&a1); }_C++_Algorithm_Data Structures_Tree_Traversal - Fatal编程技术网

C++ 嘿,我在c+中有一个分段错误+; #包括 #包括 #包括 使用名称空间std; typedef结构节点*treePointer; 类型定义结构节点{ int数据; 树冬左子,右子; }; 类堆栈{ 私人: int top; 公众: 向量st; 堆栈():顶部(-1) { } 无效推送(treePointer ptr){ 圣普什图(ptr); top++; } treePointer pop(){ if(st.empty()) 抛出_范围内的_(“Stack::pop():空堆栈”); 树间温度=圣背(); 顶部--; 圣波普背部(); 返回温度; } }; 无效iterInorder(treePointer ptr) { 斯塔克街; 对于(;;){ 对于(;ptr;ptr=ptr->leftChild) 圣普什(ptr); ptr=圣波普(); 如果(!ptr)中断; cout数据; ptr=ptr->rightChild; } } int main() { 节点a1、a2、a3、a4、a5、a6、a7; 树间ptr; a1.data=1,a2.data=2,a3.data=3,a4.data=4,a5.data=5; a6.数据=6,a7.数据=7; a1.leftChild=&a2;a1.rightChild=&a7; a2.leftChild=&a3;a2.rightChild=&a6; a3.rightChild=&a4; a4.leftChild=&a5; iterInorder(&a1); }

C++ 嘿,我在c+中有一个分段错误+; #包括 #包括 #包括 使用名称空间std; typedef结构节点*treePointer; 类型定义结构节点{ int数据; 树冬左子,右子; }; 类堆栈{ 私人: int top; 公众: 向量st; 堆栈():顶部(-1) { } 无效推送(treePointer ptr){ 圣普什图(ptr); top++; } treePointer pop(){ if(st.empty()) 抛出_范围内的_(“Stack::pop():空堆栈”); 树间温度=圣背(); 顶部--; 圣波普背部(); 返回温度; } }; 无效iterInorder(treePointer ptr) { 斯塔克街; 对于(;;){ 对于(;ptr;ptr=ptr->leftChild) 圣普什(ptr); ptr=圣波普(); 如果(!ptr)中断; cout数据; ptr=ptr->rightChild; } } int main() { 节点a1、a2、a3、a4、a5、a6、a7; 树间ptr; a1.data=1,a2.data=2,a3.data=3,a4.data=4,a5.data=5; a6.数据=6,a7.数据=7; a1.leftChild=&a2;a1.rightChild=&a7; a2.leftChild=&a3;a2.rightChild=&a6; a3.rightChild=&a4; a4.leftChild=&a5; iterInorder(&a1); },c++,algorithm,data-structures,tree,traversal,C++,Algorithm,Data Structures,Tree,Traversal,我一直在用迭代的方法按顺序遍历树。 但当我调用iterInorder(&a1)时,分割错误就出现了! 在main函数中,“cout即使没有std::cout data,我在Coliru上也有一个segfault。因此,您的问题来自其他部分。但是当我注释所有iterInorder函数时,没有更多的错误。因此,您在iterInorder内的调用中有一个segfault 当只执行以下代码时,我得到了一个segfault #include<iostream> #include<vect

我一直在用迭代的方法按顺序遍历树。 但当我调用iterInorder(&a1)时,分割错误就出现了!
在main函数中,“cout即使没有
std::cout data
,我在Coliru上也有一个segfault。因此,您的问题来自其他部分。但是当我注释所有
iterInorder
函数时,没有更多的错误。因此,您在
iterInorder
内的调用中有一个segfault

当只执行以下代码时,我得到了一个segfault

#include<iostream>
#include<vector>
#include<stdexcept>

using namespace std;

typedef struct node* treePointer;
typedef struct node{
    int data;
    treePointer leftChild, rightChild;
};

class stack{
    private:
        int top;
    public:
        vector<treePointer> st;

        stack() : top(-1)
        {

        }

        void push(treePointer ptr){
            st.push_back(ptr);
            top++;
        }

        treePointer pop(){
            if(st.empty())
                throw out_of_range("Stack<>::pop(): empty stack");
            treePointer temp = st.back();
            top--;
            st.pop_back();
            return temp;
        }
};

 void iterInorder(treePointer ptr)
    {
        stack st;

        for( ; ;) {
            for( ; ptr ; ptr = ptr->leftChild)
                st.push(ptr);

            ptr = st.pop();
            if(!ptr) break;
            cout << ptr->data;
            ptr = ptr->rightChild;
        }
    }

    int main()
    {
        node a1, a2, a3, a4, a5, a6, a7;
        treePointer ptr;
        a1.data = 1, a2.data = 2, a3.data = 3, a4.data = 4, a5.data = 5;
        a6.data = 6, a7.data = 7;
        a1.leftChild = &a2; a1.rightChild = &a7;
        a2.leftChild = &a3; a2.rightChild = &a6;
        a3.rightChild = &a4;
        a4.leftChild = &a5;


        iterInorder(&a1);
    }
如果我打印所有内存地址,这样我就知道哪一个会失败

因此在
main
中,它给出:


std::不欢迎使用堆栈溢出。请花点时间阅读并参考您可以在此处询问的内容和方式。解决此类问题的正确工具是您的调试器。在询问堆栈溢出之前,您应该逐行检查代码。如需更多帮助,请阅读。至少,您应该[编辑]您的问题需要包含一个重现您的问题的示例,以及您在调试器中所做的观察。
a3
没有初始化它的
leftChild
。因此,当沿着树下的指针进行操作时,我们不知道您的结果。错误做法*因为您很可能在头文件中编写堆栈类e、 因此,包含
使用名称空间std;
的文件也将“使用std名称空间”。
for( ; ptr ; ptr = ptr->leftChild)
{
    st.push(ptr);
}
a3.rightChild = &a4;