这是N年前写的代码
void inorder(BTCHINALR *bt)
/*中序遍历二叉树(递归算法)*/
{
if(bt != NULL){
inorder(bt->lchild);
printf("%c ",bt->data);
inorder(bt->rchild);
}
}
void lastorder(BTCHINALR *bt)
/*后序遍历二叉树(递归算法)*/
{
if(bt != NULL){
lastorder(bt->lchild);
lastorder(bt->rchild);
printf("%c ",bt->data);
}
}
void firstorder(BTCHINALR *bt)
/*先序遍历二叉树(递归算法)*/
{
if(bt != NULL){
printf("%c ",bt->data);
firstorder(bt->lchild);
firstorder(bt->rchild);
}
}
void firstorder_notrecursive(BTCHINALR *bt)
/*先序遍历二叉树(非递归算法)*/
{
BTCHINALR *q, *s[20];
int top = 0;
int bool = 1;
q = bt;
do {while(q != NULL)
{
top ++;
s[top] = q;
printf("%c ",q->data);
q = q->lchild;
}
if(top == 0)
bool = 0;
else { q = s[top];
top --;
q = q->rchild; }
}while(bool);
}
void lastorder_notrecursive_1(BTCHINALR *bt)
/*后序遍历二叉树_1(非递归算法)*/
{
BTCHINALR *q, *s[20];
int top = 0;
int bool = 1;
int i=0;
char data[100];
q = bt;
do
{
while(q != NULL)
{ top ++; s[top] = q; i++;data[i]=q->data; q = q->rchild; }
if(top == 0) bool = 0;
else { q = s[top];
top --;
q = q->lchild;
}
}while(bool);
for(i;i>0;i--) printf("%c ",data[i]);
}
void lastorder_notrecursive_2(BTCHINALR *bt)
/*后序遍历二叉树_2(非递归算法)*/
{BTCHINALR *q, *s[20];
int top = 0;
int bool = 1;
int bar[20];//一个节点第二次遍历无效的标记
q = bt;
do {while(q != NULL){
top ++; s[top]= q;bar[top]=1;q = q->lchild; }//遍历左子树
if(top==0) bool=0;
else { if(s[top]->lchild==NULL&&s[top]->rchild!=NULL&&bar[top]==1);//如果左子树不存在,则进行右子树遍历
else {q = s[top];
top --;
printf("%c ",q->data);}//输出遍历结果
if(bar[top]==2||top==0) q=NULL;//节点只能遍历一次.top为零,遍历结束
else {q = s[top];
bar[top]=bar[top]+1;//标记遍历次数
q = q->rchild; }//右子树遍历
}
}while(bool);
}
void inorder_notrecursive(BTCHINALR *bt)
/*中序遍历二叉树(非递归算法)*/
{BTCHINALR *q, *s[20];
int top = 0;
int bool = 1;
q = bt;
do {while(q != NULL)
{ top ++; s[top] = q; q = q->lchild; }
if(top == 0) bool = 0;
else { q = s[top];
top --;
printf("%c ",q->data);
q = q->rchild; }
}while(bool);
}