数据结构二叉树非递归算法,哪位高手帮忙 看一下,我已经无能为力了

#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#define MAXSIZE 10
#define MAXSIZE_IN_CREMENT 2
typedef struct BiTnode{
 char data;
 struct BiTnode *lchild,*rchild;
}BiTree;
typedef struct {
 BiTree *base;
 BiTree *top;
 int stacksize;
}SqStack;
void InitStack(SqStack *s){
 s->base=(BiTree *)malloc(MAXSIZE*sizeof(BiTree));
 if(!s->base)exit(0);
 s->top=s->base;
 s->stacksize=MAXSIZE;
}
struct BiTnode *CreateBiTree(){
 BiTree *T;
 char c;
 printf("Please enter the element of the BiTree:");
 scanf("%c",&c);getchar();
    if(c==' ')
   T=NULL;
 else
   {
    T=(BiTree*)malloc(sizeof(BiTree));
    T->data=c;
    T->lchild=CreateBiTree();
    T->rchild=CreateBiTree();
   }
 return T;
}
void push(SqStack *s,BiTree *p){
 if(s->top-s->base>=s->stacksize){
  s->base=(BiTree *)realloc(s->base,(MAXSIZE+MAXSIZE_IN_CREMENT)*sizeof(BiTree));
  if(!s->base)exit(0);
  s->top=s->base+s->stacksize;
  s->stacksize+=MAXSIZE_IN_CREMENT;}
 s->top=p;
 s->top++;
}
void pop(SqStack *s){
  BiTree *p;
  s->top--;
     p=s->top;
  printf("%c",p->data);
}
void Traverse(BiTree *T,BiTree *p){
 SqStack Snode;
 InitStack(&Snode);
 p=T;
 while(1){
  if(p!=NULL){push(&Snode,p);p=p->lchild;}
  else {
   pop(&Snode);
   //printf("%c",p->data);
   p=p->rchild;
  }
 }
}
void main()
{
 BiTree T,p;
 T=*CreateBiTree();
  Traverse(&T,&p);
 printf("/n");
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值