Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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++中的简单二叉树代码,但是在运行代码时,这些值被替换。p> #include<iostream> using namespace std; class bst { private: struct node { int data; node *left; node *right; }; public: node *start = NULL; node* create_node(node*p,int a) { if (p == NULL) { node *n1 = new node; n1->data = a; n1->left = NULL; n1->right = NULL; return n1; } else { if (a > p->data) { p->right = create_node(p->right, a); } else if(a < p->data) { p->left = create_node(p->left, a); } } } }; int main() { bst l1; int a[10] = { 12, 4, 3, 9, 6, 5, 10, 13, 34, 23 }; l1.start=l1.create_node(l1.start,a[0]); for (int i = 0; i < 10; i++) { l1.create_node(l1.start, a[i]); } return 0; } #包括 使用名称空间std; bst级 { 私人: 结构节点 { int数据; 节点*左; 节点*右; }; 公众: node*start=NULL; node*create_节点(node*p,int a) { if(p==NULL) { node*n1=新节点; n1->data=a; n1->left=NULL; n1->right=NULL; 返回n1; } 其他的 { 如果(a>p->数据) { p->right=创建_节点(p->right,a); } else if(adata) { p->left=创建_节点(p->left,a); } } } }; int main() { bst l1; inta[10]={12,4,3,9,6,5,10,13,34,23}; l1.start=l1.create_节点(l1.start,a[0]); 对于(int i=0;i_C++ - Fatal编程技术网

二叉树实现-分支上的数字被替换 以下是我试图实现的C++中的简单二叉树代码,但是在运行代码时,这些值被替换。p> #include<iostream> using namespace std; class bst { private: struct node { int data; node *left; node *right; }; public: node *start = NULL; node* create_node(node*p,int a) { if (p == NULL) { node *n1 = new node; n1->data = a; n1->left = NULL; n1->right = NULL; return n1; } else { if (a > p->data) { p->right = create_node(p->right, a); } else if(a < p->data) { p->left = create_node(p->left, a); } } } }; int main() { bst l1; int a[10] = { 12, 4, 3, 9, 6, 5, 10, 13, 34, 23 }; l1.start=l1.create_node(l1.start,a[0]); for (int i = 0; i < 10; i++) { l1.create_node(l1.start, a[i]); } return 0; } #包括 使用名称空间std; bst级 { 私人: 结构节点 { int数据; 节点*左; 节点*右; }; 公众: node*start=NULL; node*create_节点(node*p,int a) { if(p==NULL) { node*n1=新节点; n1->data=a; n1->left=NULL; n1->right=NULL; 返回n1; } 其他的 { 如果(a>p->数据) { p->right=创建_节点(p->right,a); } else if(adata) { p->left=创建_节点(p->left,a); } } } }; int main() { bst l1; inta[10]={12,4,3,9,6,5,10,13,34,23}; l1.start=l1.create_节点(l1.start,a[0]); 对于(int i=0;i

二叉树实现-分支上的数字被替换 以下是我试图实现的C++中的简单二叉树代码,但是在运行代码时,这些值被替换。p> #include<iostream> using namespace std; class bst { private: struct node { int data; node *left; node *right; }; public: node *start = NULL; node* create_node(node*p,int a) { if (p == NULL) { node *n1 = new node; n1->data = a; n1->left = NULL; n1->right = NULL; return n1; } else { if (a > p->data) { p->right = create_node(p->right, a); } else if(a < p->data) { p->left = create_node(p->left, a); } } } }; int main() { bst l1; int a[10] = { 12, 4, 3, 9, 6, 5, 10, 13, 34, 23 }; l1.start=l1.create_node(l1.start,a[0]); for (int i = 0; i < 10; i++) { l1.create_node(l1.start, a[i]); } return 0; } #包括 使用名称空间std; bst级 { 私人: 结构节点 { int数据; 节点*左; 节点*右; }; 公众: node*start=NULL; node*create_节点(node*p,int a) { if(p==NULL) { node*n1=新节点; n1->data=a; n1->left=NULL; n1->right=NULL; 返回n1; } 其他的 { 如果(a>p->数据) { p->right=创建_节点(p->right,a); } else if(adata) { p->left=创建_节点(p->left,a); } } } }; int main() { bst l1; inta[10]={12,4,3,9,6,5,10,13,34,23}; l1.start=l1.create_节点(l1.start,a[0]); 对于(int i=0;i,c++,C++,谁能指出我的错误在哪里。 当我进行顺序遍历时,我的输出是101223。 当我用断点遍历代码时,我看到每个分支下的数字都被替换了。我的索引函数是: void printInorder(node* node) { if (node == NULL) return; printInorder(node->left); printf("%d ", node->data); printIno

谁能指出我的错误在哪里。 当我进行顺序遍历时,我的输出是101223。
当我用断点遍历代码时,我看到每个分支下的数字都被替换了。

我的索引函数是:

void printInorder(node* node)
    {
        if (node == NULL)
            return;


        printInorder(node->left);

        printf("%d ", node->data);

        printInorder(node->right);
    }
我发现这个问题,使它起作用的变化与瓦拉布所建议的相似

if (a > p->data)
            {
                if (p->right == NULL)
                {
                    p->right = create_node(p->right, a);
                }
                else
                {
                    create_node(p->right, a);
                }
            }

编译器是否没有就create_节点缺少返回值向您发出警告/错误?您的
create_节点
再次返回什么?仔细看代码。如果编译器没有向您发出类似于:
main.cpp:44:5:Control可能会到达非无效函数结尾的警告,则您需要提高警告级别。如果它给了你这个警告,在完全理解它们的意思后修复它(以及任何其他警告)。我不认为这个代码是错误的。我强烈怀疑你的顺序函数是错误的。我继续测试代码,结果一切正常。你能分享你的订单代码吗?