在二叉链表表示的二叉树中,增设一个指针域,初值为空,给出算法在不使用堆栈又不破坏二叉树的情况下,前序遍历二叉树。
#include <stdio.h>
#include <stdlib.h>
#define maxsize 20
typedef struct Node
{
char data;
struct Node *lchild,*rchild,*father;
}Node,*BiTree;
int CreateBiTree(BiTree *T);
void PreTraverse(BiTree T);
int main()
{
BiTree *T;
T=(BiTree *)malloc(sizeof(BiTree));
CreateBiTree(T);
BiTree Q[maxsize];
PreTraverse(*T);
return 0;
}
int CreateBiTree(BiTree *T)
{
fflush(stdin);
printf("please input the node data:");
char ch;
scanf("%c",&ch);
*T=(BiTree)malloc(sizeof(Node));
(*T)->data=ch;
(*T)->father=NULL;
char c;
fflush(stdin);
printf("Dose %c has leftchild(y--yes,n--no):",ch);
scanf("%c",&c);
if(c=='y')
CreateBiTree(&(*T)->lchild);
else
(*T)->lchild=NULL;
fflush(stdin);
printf("Dose %c has rightchild(y--yes,n--no):",ch);
scanf("%c",&c);
if(c=='y')
CreateBiTree(&(*T)->rchild);
else
(*T)->rchild=NULL;
return 0;
}
void PreTraverse(BiTree T)
{
BiTree t,f;
t=f=(BiTree)malloc(sizeof(Node));
t=f=T; //f为父结点 ,t为当前结点
//如果不是根结点,或者是根结点,但有右子树,且右子树的父结点为空,则继续,否则停止。
//否则的情况有两种,一种是从左子树回溯到了根结点,但根结点无右子树,此时应停止
//第二种为从右子树回溯到了根结点,此时右子树的父结点不为空,
while(t->father!=t || (t->father==t && t->rchild&&!t->rchild->father))
{
if(!t->father) //父结点为空,则该结点尚未被遍历
{
printf("%c ",t->data);
t->father=f;
if(t->lchild)
{
f=t;
t=t->lchild;
}
else if(t->rchild)
{
f=t;
t=t->rchild;
}
else
t=t->father;
}
else if(!t->rchild || t->rchild->father) //父结点不为空,则当前结点已遍历,则考虑是否有右子树,如果有,是否已经遍历
t=t->father;
else
t=t->rchild; //此时为右子树尚未遍历情况
}
}