二叉树遍历有误
#include "stdio.h"#include "stdlib.h"
struct node
{
int num;
int count;
struct node *pleft;
struct node *pright;
};
struct node *pnode; //定义全局指针 指向二叉树根节点
struct node *creatnode(int i) //创建二叉树节点的函数
{
struct node *q;
q=(struct node*)malloc(sizeof(struct node));
q->count=1;
q->num=i;
q->pleft=NULL; q->pright=NULL;
return q;
}
struct node *charu(struct node *p,int k) //插入二叉树的节点
{
if (k==p->num)
{
p->count++;
return NULL;
}
if (k>p->num)
{
if (p->pright==NULL)
{
p->pright=creatnode(k);
return p->pright;
}
else
return charu(p->pright,k);
}
if (k<p->num)
{
if (p->pleft==NULL)
{
p->pleft=creatnode(k);
return p->pleft;
}
else
return charu(p->pleft,k);
}
}
void bianli(struct node *p)
{
if (p->pleft==NULL)
{
printf("%d ",p->num);
}
else
{ bianli(p->pleft);
}
if (p->pright!=NULL)
{
bianli(p->pright);
}
}
void main()
{
int b;
pnode=creatnode(8);//创建根节点
for (b=1;b<=10;b++)
{
charu(pnode,b);
}
printf("%d\n",pnode->num);
bianli(pnode);
getchar();
getchar();
}
按理说输出应该是1 2 3 4 5 6 7 8 9 10
但是输出的是 1 2 3 4 5 6 7 9 10 没有8 这是为什么啊
[local]1[/local]