题目:利用栈进行二叉树的非递归形式的非递归的算法?
#define null 0
#define MAXNUM 100
#define N 100
typedef struct node
{char data;
struct node * lchild,* rchild;
}JD;
typedef struct
{JD * stack[MAXNUM];
int top;
int base;
}sqstack;
void initstack(sqstack *s)
{s=(sqstack *)malloc(sizeof(sqstack));
s->top=-1;
} /*栈的初始化*/
void creatBinaryTree (JD *bt)
{char e;
bt=(JD*)malloc(sizeof(JD));
printf("please input the char e:\n");
scanf("%c",&e);
if(e!='#')
{bt->data=e;
creatBinaryTree (bt->lchild);
creatBinaryTree (bt->rchild);
}
} /*创建一个二叉树*/
void push(sqstack *s,JD *p)
{s->top++;
s->stack[s->top]=p;
}
void BinaryTree(JD *p)
{JD *e;sqstack *s;
initstack(s);
while(p!=null)
{printf("%c ",p->data);
if(p->rchild!=null)
push(s,p->rchild);
if(p->lchild!=null)
p=p->lchild;
else if(p->lchild==null) break;
}
while(s->top!=s->base)
{e=s->stack[s->top];
s->top=s->top-1;
printf("%c ",e->data);
}
}
main()
{JD *bt;
creatBinaryTree(bt);
BinaryTree(bt);
}
希望高手们能指点迷津!!