//如果队列不为空的话,访问结点值再出队列{p=t[front];printf("%c",p->data);if(p->lchild)
//左孩子进队列(如果有的话),右孩子进队列(如果有的话)t[rear++]=t[front]->lchild;if(p->rchild)
t[rear++]=t[front]->rchild;++front;}}//先序递归遍历并输出voidpreorder(btreebt)
{if(NULL!
=bt)
{printf("%c",bt->data);preorder(bt->lchild);preorder(bt->rchild);}}//先序非递归遍历并输出voidpreorder2(btreebt){btreet[MAX];//建立数组栈btreep;p=bt;inti=0;while(p||i>0)
{while(p)
/*根结点先进栈,访问结点值,左孩子依次进栈,直到最左的孩子进栈*/{printf("%c",p->data);++i;t[i]=p;p=p->lchild;}if(i>0)
/*出栈*/{p=t[i];i;p=p->rchild;}}//栈顶指针出栈,退至上一层,然后访问右子树,右子树访问完成之后,退至上一层,直至栈空,节点访问完成}//中序递归遍历并输出voidinorder(btreebt){if(NULL!
=bt)
{inorder(bt->lchild);printf("%c",bt->data);inorder(bt->rchild);}}//中序非递归遍历并输出voidinorder2(btreebt){btreet[MAX];//建立数组栈btreep;p=bt;inti=0;while(p||i>0)
{while(p)
/*根结点先进栈,左孩子依次进栈,直到最左的孩子进栈*/{++i;t[i]=p;p=p->lchild;}if(i>0)
/*出栈*/{p=t[i];i;printf("%c",p->data);p=p->rchild;}}//栈顶指针出栈,退至上一层,访问结点值,然后访问右子树,右子树访问完成之后,退至上一层,直至栈空,节点访问完成}//后序递归遍历并输出voidpostorder(btreebt){if(NULL!
=bt)
{postorder(bt->lchild);postorder(bt->rchild);printf("%c",bt->data);}}//后序非递归并输出voidpostorder2(btreebt)
{btreet[MAX];//建立数组栈btreep,q;inti=0;p=bt;q=NULL;while(p||i>0)
{while(p)
{++i;t[i]=p;p=p->lchild;}if(i>0)
{p=t[i];/*取栈顶元素*/if(!
p->rchild||p->rchild==q)
/*如果p没有右孩子,或右孩子已经访问过*/{printf("%c",p->data);q=p;p=NULL;i;/*退栈*/}elsep=p->rchild;}}}/*树以广义表输出*/voidprintBTree(btreebt)
{/*树为空时结束递归,否则执行如下操作*/if(bt!
=NULL){printf("%c",bt->data);/*输出根结点的值*/if(bt->lchild!
=NULL||bt->rchild!
=NULL){printf("(");printBTree(bt->lchild);if(bt->rchild!
=NULL){printf(",");}printBTree(bt->rchild);printf(")");}}}voidprintlist(btreep,boola){//把线索二叉树用广义表显示if(a==0){printf("%c",p->data);if(p>RTag==0){printf("(");printlist(p->lchild,p->LTag);if(p->RTag==0){printf(",");}printlist(p>RTag);printf(")");}}}voidprethreading(btreep){//先序线索化二叉树if(p)
{if(!
p->lchild)
{p->lchild=pre;p->LTag=1;}//前驱线索else{p->LTag=0;}if(!
pre->rchild){pre->rchild=p;pre->RTag=1;}//后继线索else{pre->RTag=0;}pre=p;//保持pre指向p前驱if(p->LTag==0)prethreading(p->lchild);//左子树线索化if(p>rchild);//右子树线索化}}voidpreorderthreading(btree&prehead,btreeT){//先序线索化二叉树(加表头)prehead=newbitnode;//建立头结点prehead->data=0;prehead->LTag=0;prehead->RTag=0;prehead->rchild=prehead;//右指针回指if(!
T)
//为空时左指针回指{prehead->lchild=prehead;prehead->LTag=1;}else{prehead->lchild=T;pre=prehead;prethreading(T);//先序遍历先序线索化pre->rchild=prehead;//最后一个节点线索化pre->RTag=1;}pre=NULL;}voidinthreading(btreep){//中序线索化二叉树if(p)
{if(p->LTag==0)inthreading(p->lchild);//左子树线索化if(!
p->lchild){//前驱线索p->lchild=pre;p->LTag=1;}else{p->LTag=0;}if(!
pre->rchild){//后继线索pre->rchild=p;pre->RTag=1;}else{pre->RTag=0;}pre=p;if(p->RTag==0)inthreading(p->rchild);//右子树线索化}}voidinorderthreading(btree&inhead,btreeT){//中序线索化二叉树(加表头)inhead=newbitnode;//创建头结点inhead->data=0;inhead->LTag=0;inhead->RTag=0;inhead->rchild=inhead;if(!
T){inhead->lchild=inhead;//空二叉树inhead->LTag=1;}else{inhead->lchild=T;pre=inhead;//pre:
刚刚访问过的结点;inthreading(T);pre->rchild=inhead;pre->RTag=1;}pre=NULL;}voidpostthreading(btreep){//后序线索化二叉树if(p){if(p->LTag==0){postthreading(p>RTag==0){postthreading(p>lchild){p->LTag=1;p->lchild=pre;}//前驱线索else{p->LTag=0;}if(!
pre->rchild){pre->RTag=1;pre->rchild=p;}//后继线索else{p->RTag=0;}pre=p;}//后序搜索化节点的建立}voidpostorderthreading(btree&posthead,btreeT){//后序线索化二叉树(加表头)posthead=newbitnode;posthead->data=0;posthead->LTag=0;posthead->RTag=0;//建头结点posthead->rchild=posthead;//右指针回指if(!
T){posthead->lchild=posthead;//若二叉树空,则左指针回指posthead->LTag=1;}else{posthead->lchild=T;pre=posthead;postthreading(T);//后序遍历进行后序线索化}//最后一个节点处理pre=NULL;}//求线索二叉树先序后续节点btreeprenext(btreep){//求线索二叉树先序后继节点btreeQ;if(p>lchild;else{Q=p;while(Q>rchild;Q=Q->rchild;};return(Q);}//求线索二叉树中序后继节点btreeinnext(btreep){btreeQ;Q=p->rchild;if(p->RTag==0)while(Q->LTag==0)Q=Q->lchild;return(Q);}//返回元素x的位置btreelocate(charx,btreeinhead){btreetmp;tmp=inhead;do{tmp=prenext(tmp);if(tmp!
=inhead){if(tmp->data==x)
//若节点对应数据是X返回该节点位置{returntmp;}}}while(tmp!
=inhead);return0;}intmain(){btreeT,T1,T2,q,p;btreeprehead;btreeinhead,posthead;inta;chars;T=(btree)malloc(sizeof(bnode));printf("先序输入二叉树(#表示空):
");T=create();T1=(btree)malloc(sizeof(bnode));T2=(btree)malloc(sizeof(bnode));p=(btree)malloc(sizeof(bnode));q=(btree)malloc(sizeof(bnode));T1=copy(T);T2=copy(T);//复制树T到T1T2,以便线索化使用printf("以广义表形式输出二叉树:
");printBTree(T);//二叉树遍历printf("\n先序递归遍历为:
");preorder(T);printf("\n先序非递归遍历为:
");preorder2(T);printf("\n中序递归遍历为:
");inorder(T);printf("\n中序非递归遍历为:
");inorder2(T);printf("\n后序递归遍历为:
");postorder(T);printf("\n后序非递归遍历为:
");postorder2(T);//二叉树线索化printf("\n二叉树的线索化!
");printf("\n先序线索化:
");preorderthreading(prehead,T);//先序线索化printlist(prehead->lchild,prehead->LTag);//以广义表形式打印线索树printf("\n中序线索化:
");preorderthreading(inhead,T1);//中序线索化printlist(inhead->lchild,inhead->LTag);//以广义表形式打印线索树printf("\n后序线索化:
");preorderthreading(posthead,T2);//后序线索化printlist(posthead->lchild,posthead->LTag);//以广义表形式打印线索树//二叉树后继节点printf("\n线索二叉树后继节点!
输入一字符:
");getchar();scanf("%c",&s);p=locate(s,prehead);q=innext(p);printf("中序后继节点为%c",q->data);getchar();getchar();return0;}//输入样例dba##c##fe##g##