Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.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_Data Structures - Fatal编程技术网

C 被树枝卡住

C 被树枝卡住,c,data-structures,C,Data Structures,我试图进行树遍历。(按顺序、按顺序和按后顺序) 这是我的密码 #include<stdio.h> #include<stdlib.h> struct node{ int data; struct node* left; struct node* right; }; void Insert(struct node* root,int item) { struct node* parent; struct node* NewNode = (

我试图进行树遍历。(按顺序、按顺序和按后顺序) 这是我的密码

#include<stdio.h>
#include<stdlib.h>


struct node{
  int data;
  struct node* left;
  struct node* right;
};


void Insert(struct node* root,int item)
{
    struct node* parent;
    struct node* NewNode = (struct node*)malloc(sizeof(struct node));
    NewNode->left = NULL;
    NewNode->data = item;
    NewNode->right = NULL;

    if (root == NULL)
        root = NewNode;
    else
    {
        parent = root;
        while (1)
        {
            if (parent->data>item)
            {
                if(parent->left == NULL)
                {
                    parent->left = NewNode;
                    return;
                }
                parent = parent->left;

            }
            if (parent->data<item)
            {
                if(parent->right == NULL)
                {
                    parent->right = NewNode;
                    return;
                }
                parent = parent->right;
            }
        }

    }
}




void pre(struct node *newNode)
{
    if(newNode!=NULL)
    {
        printf("%d ",newNode->data);
        pre(newNode->left);
        pre(newNode->right);
    }
}

void in(struct node *newNode)
{
    if(newNode!=NULL)
    {
        in(newNode->left);
        printf("%d ",newNode->data);
        in(newNode->right);
    }
}


void post(struct node *newNode)
{
    if(newNode!=NULL)
    {
        post(newNode->left);
        post(newNode->right);
        printf("%d ",newNode->data);
    }
}


int main(void)
{
    int num,i;

    printf("\nHow many Numbers you wanna Enter in tree:\t");
    scanf("%d",&num);
    int numArr[num];
    printf("\nEnter the numbers: \n");
    struct node* root = NULL;
    for (i=0;i<num;i++)
    {
        scanf("%d",&numArr[i]);
        Insert(root,numArr[i]);
    }

    printf("\nPre order traversal is:\n");
    pre(root);
    printf("\nIn order traversal is:\n");
    in(root);
    printf("\nPost order traversal is:\n");
    post(root);
}
#包括
#包括
结构节点{
int数据;
结构节点*左;
结构节点*右;
};
空插入(结构节点*根,int项)
{
结构节点*父节点;
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
NewNode->left=NULL;
新建节点->数据=项目;
NewNode->right=NULL;
if(root==NULL)
根=新节点;
其他的
{
父=根;
而(1)
{
如果(父项->数据>项)
{
如果(父项->左==NULL)
{
父节点->左=新节点;
返回;
}
父项=父项->左;
}
如果(父->数据权限==NULL)
{
父->右=新节点;
返回;
}
父=父->右;
}
}
}
}
void pre(结构节点*新节点)
{
if(newNode!=NULL)
{
printf(“%d”,newNode->data);
pre(新建节点->左侧);
pre(新建节点->右侧);
}
}
在(结构节点*新节点)中无效
{
if(newNode!=NULL)
{
在(新建节点->左侧);
printf(“%d”,newNode->data);
在(新建节点->右侧);
}
}
void post(结构节点*newNode)
{
if(newNode!=NULL)
{
post(新建节点->左侧);
post(新建节点->右侧);
printf(“%d”,newNode->data);
}
}
内部主(空)
{
int num,i;
printf(“\n要在树中输入多少个数字:\t”);
scanf(“%d”和&num);
int努马尔[num];
printf(“\n输入数字:\n”);
结构节点*root=NULL;

对于(i=0;i这是正确的答案,感谢大家的帮助

#include<stdio.h>
#include<stdlib.h>


struct node{
  int data;
  struct node* left;
  struct node* right;
};


void Insert(struct node** root,int item)
{
    struct node* parent;
    struct node* NewNode = (struct node*)malloc(sizeof(struct node));
    NewNode->left = NULL;
    NewNode->data = item;
    NewNode->right = NULL;

    if (*root == NULL)
        *root = NewNode;
    else
    {
        parent = *root;
        while (1)
        {
            if (parent->data>item)
            {
                if(parent->left == NULL)
                {
                    parent->left = NewNode;
                    return;
                }
                parent = parent->left;

            }
            if (parent->data<item)
            {
                if(parent->right == NULL)
                {
                    parent->right = NewNode;
                    return;
                }
                parent = parent->right;
            }
        }

    }
}




void pre(struct node *newNode)
{
    if(newNode!=NULL)
    {
        printf("%d ",newNode->data);
        pre(newNode->left);
        pre(newNode->right);
    }
}

void in(struct node *newNode)
{
    if(newNode!=NULL)
    {
        in(newNode->left);
        printf("%d ",newNode->data);
        in(newNode->right);
    }
}


void post(struct node *newNode)
{
    if(newNode!=NULL)
    {
        post(newNode->left);
        post(newNode->right);
        printf("%d ",newNode->data);
    }
}


int main(void)
{
    int num,i;

    printf("\nHow many Numbers you wanna Enter in tree:\t");
    scanf("%d",&num);
    int numArr[num];
    printf("\nEnter the numbers: \n");
    struct node* root = NULL;
    for (i=0;i<num;i++)
    {
        scanf("%d",&numArr[i]);
        Insert(&root,numArr[i]);
    }

    printf("\nPre order traversal is:\n");
    pre(root);
    printf("\nIn order traversal is:\n");
    in(root);
    printf("\nPost order traversal is:\n");
    post(root);
}
#包括
#包括
结构节点{
int数据;
结构节点*左;
结构节点*右;
};
无效插入(结构节点**根,int项)
{
结构节点*父节点;
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
NewNode->left=NULL;
新建节点->数据=项目;
NewNode->right=NULL;
如果(*root==NULL)
*根=新节点;
其他的
{
父=*根;
而(1)
{
如果(父项->数据>项)
{
如果(父项->左==NULL)
{
父节点->左=新节点;
返回;
}
父项=父项->左;
}
如果(父->数据权限==NULL)
{
父->右=新节点;
返回;
}
父=父->右;
}
}
}
}
void pre(结构节点*新节点)
{
if(newNode!=NULL)
{
printf(“%d”,newNode->data);
pre(新建节点->左侧);
pre(新建节点->右侧);
}
}
在(结构节点*新节点)中无效
{
if(newNode!=NULL)
{
在(新建节点->左侧);
printf(“%d”,newNode->data);
在(新建节点->右侧);
}
}
void post(结构节点*newNode)
{
if(newNode!=NULL)
{
post(新建节点->左侧);
post(新建节点->右侧);
printf(“%d”,newNode->data);
}
}
内部主(空)
{
int num,i;
printf(“\n要在树中输入多少个数字:\t”);
scanf(“%d”和&num);
int努马尔[num];
printf(“\n输入数字:\n”);
结构节点*root=NULL;

对于(i=0;i这是正确的答案,感谢大家的帮助

#include<stdio.h>
#include<stdlib.h>


struct node{
  int data;
  struct node* left;
  struct node* right;
};


void Insert(struct node** root,int item)
{
    struct node* parent;
    struct node* NewNode = (struct node*)malloc(sizeof(struct node));
    NewNode->left = NULL;
    NewNode->data = item;
    NewNode->right = NULL;

    if (*root == NULL)
        *root = NewNode;
    else
    {
        parent = *root;
        while (1)
        {
            if (parent->data>item)
            {
                if(parent->left == NULL)
                {
                    parent->left = NewNode;
                    return;
                }
                parent = parent->left;

            }
            if (parent->data<item)
            {
                if(parent->right == NULL)
                {
                    parent->right = NewNode;
                    return;
                }
                parent = parent->right;
            }
        }

    }
}




void pre(struct node *newNode)
{
    if(newNode!=NULL)
    {
        printf("%d ",newNode->data);
        pre(newNode->left);
        pre(newNode->right);
    }
}

void in(struct node *newNode)
{
    if(newNode!=NULL)
    {
        in(newNode->left);
        printf("%d ",newNode->data);
        in(newNode->right);
    }
}


void post(struct node *newNode)
{
    if(newNode!=NULL)
    {
        post(newNode->left);
        post(newNode->right);
        printf("%d ",newNode->data);
    }
}


int main(void)
{
    int num,i;

    printf("\nHow many Numbers you wanna Enter in tree:\t");
    scanf("%d",&num);
    int numArr[num];
    printf("\nEnter the numbers: \n");
    struct node* root = NULL;
    for (i=0;i<num;i++)
    {
        scanf("%d",&numArr[i]);
        Insert(&root,numArr[i]);
    }

    printf("\nPre order traversal is:\n");
    pre(root);
    printf("\nIn order traversal is:\n");
    in(root);
    printf("\nPost order traversal is:\n");
    post(root);
}
#包括
#包括
结构节点{
int数据;
结构节点*左;
结构节点*右;
};
无效插入(结构节点**根,int项)
{
结构节点*父节点;
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
NewNode->left=NULL;
新建节点->数据=项目;
NewNode->right=NULL;
如果(*root==NULL)
*根=新节点;
其他的
{
父=*根;
而(1)
{
如果(父项->数据>项)
{
如果(父项->左==NULL)
{
父节点->左=新节点;
返回;
}
父项=父项->左;
}
如果(父->数据权限==NULL)
{
父->右=新节点;
返回;
}
父=父->右;
}
}
}
}
void pre(结构节点*新节点)
{
if(newNode!=NULL)
{
printf(“%d”,newNode->data);
pre(新建节点->左侧);
pre(新建节点->右侧);
}
}
在(结构节点*新节点)中无效
{
if(newNode!=NULL)
{
在(新建节点->左侧);
printf(“%d”,newNode->data);
在(新建节点->右侧);
}
}
void post(结构节点*newNode)
{
if(newNode!=NULL)
{
post(新建节点->左侧);
post(新建节点->右侧);
printf(“%d”,newNode->data);
}
}
内部主(空)
{
int num,i;
printf(“\n要在树中输入多少个数字:\t”);
scanf(“%d”和&num);
int努马尔[num];
printf(“\n输入数字:\n”);
结构节点*root=NULL;

对于(i=0;如果函数参数本身无法在函数中更改,则需要传递一个指针以进行更改(如
void Insert(struct node*root
-->
void Insert(struct node**root
)。然后调用
Insert(&root,numArr[i]);
if(root==NULL)root=NewNode;否则{parent=root;
-->
如果(*root==NULL)*root=NewNode;否则{parent=*root;
)(或者替换返回值(如
root=Insert(root,numar[i]);
)将根作为指向节点的指针传递,
struct node**p_root
,然后在函数dereference p_root;
*p_root=newNode;
中,您永远不会更新main中的根指针,因此树始终为空。重新设计插入代码的接口。获取指向指针的指针并以这种方式更新,或者返回新的根指针(如果它不改变,还是旧的