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

C 使用带二叉树的指针

C 使用带二叉树的指针,c,pointers,C,Pointers,可能重复: 我只是想写一个简单的二进制搜索树程序,用户可以在其中插入节点,并以顺序、前顺序或后顺序模式查看树中的所有节点。我的代码是 #包括 #包括 树状结构 { int数据; 结构树节点*lchild; 结构树节点*rchild; }; void insertnode(结构树节点**n,int d) { 结构树节点*p=&n; if(p==NULL) { //表示树是空的 p=(struct treenode*)malloc(sizeof(struct treenode)); p->dat

可能重复:

我只是想写一个简单的二进制搜索树程序,用户可以在其中插入节点,并以顺序、前顺序或后顺序模式查看树中的所有节点。我的代码是


#包括
#包括
树状结构
{
int数据;
结构树节点*lchild;
结构树节点*rchild;
};
void insertnode(结构树节点**n,int d)
{
结构树节点*p=&n;
if(p==NULL)
{
//表示树是空的
p=(struct treenode*)malloc(sizeof(struct treenode));
p->data=d;
p->lchild=NULL;
p->rchild=NULL;
}
其他的
{
//strat比较来自根目录的新数据
如果(ddata)
insertnode(((p->lchild)),d);
其他的
insertnode(((p->rchild)),d);
}
}
无效预订单(结构树节点**n)
{
结构树节点*p=&n;
if(p==NULL)
{
printf(“\n列表为空”);
}
其他的
{
printf(“%d”,p->data);
预订单(p->lchild);
预订单(p->rchild);
}
}
作废后订单(结构树节点**n)
{
结构树节点*p=&n;
if(p==NULL)
{
printf(“\n列表为空”);
}
其他的
{
预订单(p->lchild);
预订单(p->rchild);
printf(“%d”,p->data);
}
}
按顺序无效(结构树节点**n)
{
结构树节点*p=&n;
if(p==NULL)
{
printf(“\n列表为空”);
}
其他的
{
预订单(p->lchild);
printf(“%d”,p->data);
预订单(p->rchild);
}
}
内部主(空)
{
struct treenode*root=NULL;
int选择,数据;
而(1)
{
printf(“\n按1以BST方式插入节点:”;
printf(“\n按2以预先排序方式遍历树:”;
printf(“\n按3以后序方式遍历树:”;
printf(“\n按4以按顺序遍历树:”;
printf(“\n按5退出:”;
printf(“\n输入您的选择:”);
scanf(“%d”,选择(&C);
开关(选择)
{
案例1:printf(“\n输入要插入的数据:”);
scanf(“%d”和数据);
insertnode(根目录和数据);
打破
案例2:前序(&根);
打破
案例3:后订单(&root);
打破
案例4:顺序(&根);
打破
案例5:退出(0);
打破
默认值:printf(“\n您输入了一个无效的选项,请重试”);
}
}
返回0;
}

程序运行成功,但在我进入节点后,浏览器检测到错误,程序停止工作。代码中有错误吗?

扰流板:

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

struct treenode {
    struct treenode *lchild;
    struct treenode *rchild;
    int data;
    } *root = NULL;

void insertnode(struct treenode **pp,int d)
{
    for( ;*pp; )
    {
         if (d < (*pp)->data) pp = &(*pp)->lchild;
         else  pp = &(*pp)->rchild;
    }
    *pp = malloc (sizeof **pp);
    (*pp)->data = d;
    (*pp)->lchild = NULL;
    (*pp)->rchild = NULL;
}

void preorder(struct treenode *p)
{
    if(p==NULL)
    {
        printf("\nThe list is empty");
        return;
    }

    printf("%d,",p->data);
    if (p->lchild) preorder(p->lchild);
    if (p->rchild) preorder(p->rchild);
}

void postorder(struct treenode *p)
{
    if(p==NULL)
    {
        printf("\nThe list is empty");
        return;
    }

    if (p->lchild) preorder(p->lchild);
    if (p->rchild) preorder(p->rchild);
    printf("%d,",p->data);
}
void inorder(struct treenode *p)
{
    if(p==NULL)
    {
        printf("\nThe list is empty");
        return;
    }

    if (p->lchild) preorder(p->lchild);
    printf("%d,",p->data);
    if (p->rchild) preorder(p->rchild);
}

int main(void)
{
    root=NULL;
    int choice,data;

    while(1)
    {
         printf("\nPress 1 for inserting a node in BST fashion: ");
         printf("\nPress 2 for traversing the tree in preorder fashion :");
         printf("\nPress 3 for traversing the tree in postorder fashion :");
         printf("\nPress 4 for traversing the tree in inorder fashion :");
         printf("\nPress 5 to exit :");


         printf("\nEnter your choice: ");
         scanf("%d", &choice);

    switch(choice)
    {
        case 1: printf("\nEnter the data to be inserted:");
            scanf("%d",&data);
            insertnode( &root,data);
            break;

        case 2: preorder(root);
            break;

        case 3: postorder(root);
            break;

        case 4: inorder(root);
            break;

        case 5: exit(0);
            break;
        default: printf("\nYou have entered an invalid choice. Please try again");
    }
}

return 0;
}
#包括
#包括
树状结构{
结构树节点*lchild;
结构树节点*rchild;
int数据;
}*root=NULL;
void insertnode(结构树节点**pp,int d)
{
对于(;*pp;)
{
如果(d<(*pp)->数据)pp=&(*pp)->lchild;
else pp=&(*pp)->rchild;
}
*pp=malloc(尺寸**pp);
(*pp)->数据=d;
(*pp)->lchild=NULL;
(*pp)->rchild=NULL;
}
空预订单(结构树节点*p)
{
if(p==NULL)
{
printf(“\n列表为空”);
返回;
}
printf(“%d”,p->data);
if(p->lchild)预订单(p->lchild);
if(p->rchild)预订单(p->rchild);
}
作废后序(结构树节点*p)
{
if(p==NULL)
{
printf(“\n列表为空”);
返回;
}
if(p->lchild)预订单(p->lchild);
if(p->rchild)预订单(p->rchild);
printf(“%d”,p->data);
}
void顺序(结构树节点*p)
{
if(p==NULL)
{
printf(“\n列表为空”);
返回;
}
if(p->lchild)预订单(p->lchild);
printf(“%d”,p->data);
if(p->rchild)预订单(p->rchild);
}
内部主(空)
{
root=NULL;
int选择,数据;
而(1)
{
printf(“\n按1以BST方式插入节点:”;
printf(“\n按2以预先排序方式遍历树:”;
printf(“\n按3以后序方式遍历树:”;
printf(“\n按4以按顺序遍历树:”;
printf(“\n按5退出:”;
printf(“\n输入您的选择:”);
scanf(“%d”,选择(&C);
开关(选择)
{
案例1:printf(“\n输入要插入的数据:”);
scanf(“%d”和数据);
insertnode(根目录和数据);
打破
案例2:前序(根);
打破
案例3:后订单(根);
打破
案例4:顺序(根);
打破
案例5:退出(0);
打破
默认值:printf(“\n您输入的选项无效,请重试”);
}
}
返回0;
}

你为什么不试着知道呢?你说的“探险家”是什么意思?重复:是的。。我知道,但即使经过所有的更正,我的代码也没有运行。我不知道。我刚刚发布了我的答案,但有点不同。你的
和(pp)->lchild等。看起来不对。最好的方法是运行diff-uw original.c newsource.c并查看输出。(记住:这是作业!)我看到你直接使用了**p(双指针)。我首先将它们存储到单个间接指针中的方法有什么问题?您的版本使用了一个额外的变量,以后需要将该变量分配给*p。省略它更简单:变量+赋值更少:=出错的可能性更小。顺便说一句:我只在sizeof中使用**pp;所以**p不是严格意义上的解引用,只是它的大小是确定的,而不是对象本身。我理解,但我更愿意这样做。你能看看我的代码并告诉我语句struct treenode*p=&n;是对的吗?不,它是错的(在xxxorder()函数中)。它应该是
struct treenode*p=*n。它也是useles,您可以在现在使用n的所有地方的这些函数中使用(*n)。(或将函数更改为将指针作为参数,而不是指针的指针)
#include <stdio.h>
#include <stdlib.h>

struct treenode {
    struct treenode *lchild;
    struct treenode *rchild;
    int data;
    } *root = NULL;

void insertnode(struct treenode **pp,int d)
{
    for( ;*pp; )
    {
         if (d < (*pp)->data) pp = &(*pp)->lchild;
         else  pp = &(*pp)->rchild;
    }
    *pp = malloc (sizeof **pp);
    (*pp)->data = d;
    (*pp)->lchild = NULL;
    (*pp)->rchild = NULL;
}

void preorder(struct treenode *p)
{
    if(p==NULL)
    {
        printf("\nThe list is empty");
        return;
    }

    printf("%d,",p->data);
    if (p->lchild) preorder(p->lchild);
    if (p->rchild) preorder(p->rchild);
}

void postorder(struct treenode *p)
{
    if(p==NULL)
    {
        printf("\nThe list is empty");
        return;
    }

    if (p->lchild) preorder(p->lchild);
    if (p->rchild) preorder(p->rchild);
    printf("%d,",p->data);
}
void inorder(struct treenode *p)
{
    if(p==NULL)
    {
        printf("\nThe list is empty");
        return;
    }

    if (p->lchild) preorder(p->lchild);
    printf("%d,",p->data);
    if (p->rchild) preorder(p->rchild);
}

int main(void)
{
    root=NULL;
    int choice,data;

    while(1)
    {
         printf("\nPress 1 for inserting a node in BST fashion: ");
         printf("\nPress 2 for traversing the tree in preorder fashion :");
         printf("\nPress 3 for traversing the tree in postorder fashion :");
         printf("\nPress 4 for traversing the tree in inorder fashion :");
         printf("\nPress 5 to exit :");


         printf("\nEnter your choice: ");
         scanf("%d", &choice);

    switch(choice)
    {
        case 1: printf("\nEnter the data to be inserted:");
            scanf("%d",&data);
            insertnode( &root,data);
            break;

        case 2: preorder(root);
            break;

        case 3: postorder(root);
            break;

        case 4: inorder(root);
            break;

        case 5: exit(0);
            break;
        default: printf("\nYou have entered an invalid choice. Please try again");
    }
}

return 0;
}