這個(gè)程序的思路是自己輸入數(shù)字,在輸入的同時(shí),已經(jīng)幫你左右順序排好了,即左子樹的數(shù)字比右子樹小,是個(gè)順序二叉樹,以輸入0為結(jié)素,而后一中序遍歷輸出,但不知道為什么,在屏幕上打引的卻是左子樹最小的數(shù)字,而且一直輸出,請(qǐng)看下面程序
#include
#include
typedef struct no
{
int key;
struct no *left,*right;
}node,*PNOD;
void inster(PNOD *p,int k)
{
PNOD ppre,pre,temp;
ppre=*p;
printf("current number ppre % d",ppre->key);
if(ppre==NULL)
{
ppre=(node *)malloc(sizeof(node));
ppre->key=k;
ppre->left=NULL;
ppre->right=NULL;
*p=ppre;
return;
}
while(ppre)
{
if(k < ppre->key)
{
temp=ppre;
ppre=ppre->left;
printf("left\n");
}
else if(k == ppre->key)
{
printf("has ...\n");
return;
}
else if(k > ppre->key)
{
temp=ppre;
ppre=ppre->right;
printf("right\n");
}
/*printf("aaaaaaaaaaaaaaaaaa");*/
}
pre=(node *)malloc(sizeof(node));
pre->key=k;
pre->left=NULL;
pre->right=NULL;
#include
#include
typedef struct no
{
int key;
struct no *left,*right;
}node,*PNOD;
void inster(PNOD *p,int k)
{
PNOD ppre,pre,temp;
ppre=*p;
printf("current number ppre % d",ppre->key);
if(ppre==NULL)
{
ppre=(node *)malloc(sizeof(node));
ppre->key=k;
ppre->left=NULL;
ppre->right=NULL;
*p=ppre;
return;
}
while(ppre)
{
if(k < ppre->key)
{
temp=ppre;
ppre=ppre->left;
printf("left\n");
}
else if(k == ppre->key)
{
printf("has ...\n");
return;
}
else if(k > ppre->key)
{
temp=ppre;
ppre=ppre->right;
printf("right\n");
}
/*printf("aaaaaaaaaaaaaaaaaa");*/
}
pre=(node *)malloc(sizeof(node));
pre->key=k;
pre->left=NULL;
pre->right=NULL;