gpt4 book ai didi

c - 在二叉树中使用指针

转载 作者:行者123 更新时间:2023-11-30 14:24:39 25 4
gpt4 key购买 nike

Possible Duplicate:
Binary tree - Dereferencing pointers

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

<小时/>
#include <stdio.h>
#include <stdlib.h>

struct treenode
{
int data;
struct treenode *lchild;
struct treenode *rchild;
};

void insertnode(struct treenode **n,int d)
{
struct treenode *p=&n;
if(p==NULL)
{
// means the tree is empty
p=(struct treenode *)malloc(sizeof(struct treenode));
p->data=d;
p->lchild=NULL;
p->rchild=NULL;
}

else
{
// strat comparing the new data from root
if( d<p->data )
insertnode(((p->lchild)),d);

else
insertnode(((p->rchild)),d);
}
}

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

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

}

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

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

}

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

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

}

int main(void)
{
struct treenode *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 enetred an invalid choice. Please try again");
}
}

return 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;
}

关于c - 在二叉树中使用指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11501480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com