主题:[原创]二叉树遍历问题,供大家参考使用
这是中序,大家想要前序,后序时自己写一下遍历函数就可以了
#define stack_init_size 100
#define stackincrement 10
#define ok 1
#define error 0
#define N 100
#define null 0
typedef int status;
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
typedef struct{
bitree *base;
bitree *top;
int stacksize;
}sqstack;
void initstack(sqstack *s)
{
s->base=(bitree *)malloc(stack_init_size*sizeof(bitree));
if(!s->base) exit(1);
s->top=s->base;
s->stacksize=stack_init_size;
}
push(sqstack *s,bitree e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(bitree *)realloc(s->base,(s->stacksize+stackincrement)*sizeof(bitree));
if(!s->base) exit(1);
s->top=s->base+s->stacksize;
s->stacksize+=stackincrement;
}
*s->top++=e;
return ok;
}
pop(sqstack *s,bitree *e)
{
if(s->top==s->base) return error;
*e=*--s->top;
return ok;
}
bitree gettop(sqstack *s)
{
bitree *e=null;
if(s->top==s->base) return error;
*e=*(s->top-1);
return *e;
}
bitree creatree()
{
int i;
char a[N];
sqstack s;
bitree ht,q,p;
scanf("%s",a);
ht=(bitree)malloc(sizeof(bitnode));
ht->data=a[0];
p=ht;
initstack(&s);
push(&s,ht);
i=1;
while(a[i]!='\0')
{
if(a[i]!='#')
{
q=(bitree)malloc(sizeof(bitnode));
q->data=a[i];
if(p==gettop(&s))
p->lchild=q;
else p->rchild=q;
p=q;
push(&s,p);
i++;
}
else
{
if(p==gettop(&s))
p->lchild=null;
else p->rchild=null;
pop(&s,&p);
i++;
}
}
return ht;
}
stackempty(sqstack *s)
{
if(s->top==s->base) return ok;
else return error;
}
status visit(char e)
{
printf("%c",e);
return ok;
}
inorder(bitree t)
{
sqstack s;
bitree p;
initstack(&s);
p=t;
while(p||!stackempty(&s))
{
if(p)
{
push(&s,p);
p=p->lchild;
}
else
{
pop(&s,&p);
if(!visit(p->data))
return error;
p=p->rchild;
}
}
return ok;
}
int main()
{
bitree t;
t=null;
t=creatree();
inorder(t);
getch();
}
#define stack_init_size 100
#define stackincrement 10
#define ok 1
#define error 0
#define N 100
#define null 0
typedef int status;
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
typedef struct{
bitree *base;
bitree *top;
int stacksize;
}sqstack;
void initstack(sqstack *s)
{
s->base=(bitree *)malloc(stack_init_size*sizeof(bitree));
if(!s->base) exit(1);
s->top=s->base;
s->stacksize=stack_init_size;
}
push(sqstack *s,bitree e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(bitree *)realloc(s->base,(s->stacksize+stackincrement)*sizeof(bitree));
if(!s->base) exit(1);
s->top=s->base+s->stacksize;
s->stacksize+=stackincrement;
}
*s->top++=e;
return ok;
}
pop(sqstack *s,bitree *e)
{
if(s->top==s->base) return error;
*e=*--s->top;
return ok;
}
bitree gettop(sqstack *s)
{
bitree *e=null;
if(s->top==s->base) return error;
*e=*(s->top-1);
return *e;
}
bitree creatree()
{
int i;
char a[N];
sqstack s;
bitree ht,q,p;
scanf("%s",a);
ht=(bitree)malloc(sizeof(bitnode));
ht->data=a[0];
p=ht;
initstack(&s);
push(&s,ht);
i=1;
while(a[i]!='\0')
{
if(a[i]!='#')
{
q=(bitree)malloc(sizeof(bitnode));
q->data=a[i];
if(p==gettop(&s))
p->lchild=q;
else p->rchild=q;
p=q;
push(&s,p);
i++;
}
else
{
if(p==gettop(&s))
p->lchild=null;
else p->rchild=null;
pop(&s,&p);
i++;
}
}
return ht;
}
stackempty(sqstack *s)
{
if(s->top==s->base) return ok;
else return error;
}
status visit(char e)
{
printf("%c",e);
return ok;
}
inorder(bitree t)
{
sqstack s;
bitree p;
initstack(&s);
p=t;
while(p||!stackempty(&s))
{
if(p)
{
push(&s,p);
p=p->lchild;
}
else
{
pop(&s,&p);
if(!visit(p->data))
return error;
p=p->rchild;
}
}
return ok;
}
int main()
{
bitree t;
t=null;
t=creatree();
inorder(t);
getch();
}