二叉树中序遍历递归算法
问题补充说明:if(T){ if(Visit(T->data)) if(PreOrderTraverse(T->lchild,Visit)) if(PreOrderTraverse(T->rchild,Visit)) return OK; return ERROR; }else return OK;这是前序遍历递归算法主要部分,请问中序递归遍历算法如何编写?其中Visit是对数据元素操作的应用函数
if(T){
if(InOrderTraverse(T->l,Visit))
if(Visit(T->data))
if(InOrderTraverse(T->r,Visit))来自returnOK;
returnERROR;
}elseretur360问答nOK;
以上就是中序遍历二叉树
这段程序我全有,具体和促而州离言都么职如下:
#include<alloc.h>
#defineERROR0;
#defineFALSE0;
#defineTRUE1;
#defineOK1;
t积件清庆占跑万太斤攻交ypedefintE推用指请甲交环跟lemType;
typedefintStatus;
typedefintKeyType;
#defineEQ(a,b直局心临担走)((a)==(b))
#defineLT(a,b)((a)<(b))
#defineLQ(a,b)((a)<=(b))
typedefstru定假度棉受目计文部够ctBinaryTree//定义二叉树
{
ElemTypedata;
structBinaryTree延不识英业黑还条胞音*l;
structBinaryTree*r;
}*BiTree,BiNode;//
BiNode*new(密全源川区春管投讨光)//为新结点开辟空间
南直随可{
return((BiNode*)malloc(sizeof(BiNode)));
}
CreateSubTree(BiTree*T,ElemType*all,inti)//创建新有子树
{
if((all[i]==0)||i>16)
{
*T=NULL;
return跑OK;
}
*T=new();
if(*T==NULL)retu燃问总rnERROR;
(*T)->d足问天施北川ata=all[i];
CreateSubTree(&((*T)->l),all,2*i);
CreateSu考物宽bTree(&((*T)->r),all,2*i+1);
}
CreateBiTree(BiTree*T)//创建新结缩倍让令省及失载点
{
ElemTypeall[16]={0,1,2,3,0,0,4,5,0可营跑今下令范,0,0,0,6,0,衣督再特层重始深末千袁0,0,};
CreateSubTree(T,all,1);
}
printelem(ElemTyped)//输出
{
printf("%d\n",d);
}
PreOr么式业笑劳真天导巴derTraverse(BiTreeT,int(*Visit)(Ele王轻节mTyped))//前序遍历
{
if(T){
if(Visit(T->data))
if(PreOrderTraverse(T->l,Visit))
if(PreOrderTraverse(T->r,Visit))returnOK;
returnERROR;
}elsereturnOK;
}
InOrderTraverse(BiTreeT,int(*Visit)(ElemTyped))//中序遍历
{
if(T){
if(InOrderTraverse(T->l,Visit))
if(Visit(T->data))
if(InOrderTraverse(T->r,Visit))returnOK;
returnERROR;
}elsereturnOK;
}
StatusSearchBST(BiTreeT,KeyTypekey,BiTreef,BiTree*p){
if(!T){*p=f;returnFALSE;}
elseifEQ(key,T->data){*p=T;returnTRUE;}
elseifLT(key,T->data)SearchBST(T->l,key,T,p);
elseSearchBST(T->r,key,T,p);
}
StatusInsertBST(BiTree*T,ElemTypee){
BiTreep;
BiTrees;
if(!SearchBST(*T,e,NULL,&p)){
s=(BiTree)malloc(sizeof(BiNode));
s->data=e;s->l=s->r=NULL;
if(!p)*T=s;
elseif(LT(e,p->data))p->l=s;
elsep->r=s;
returnTRUE;
}
elsereturnFALSE;
}
voidDelete(BiTree*p){
BiTreeq,s;
if(!(*p)->r){
q=(*p);
(*p)=(*p)->l;
free(q);
}
elseif(!(*p)->l){
q=(*p);
(*p)=(*p)->r;
free(q);
}
else{
/*q=(*p);
s=(*p)->l;
while(s->r){q=s;s=s->r;}
(*p)->data=s->data;
if(q!=(*p))q->r=s->l;
elseq->l=s->l;
free(s);
*/
q=s=(*p)->l;
while(s->r)s=s->r;
s->r=(*p)->r;
free(*p);
(*p)=q;
}
}
StatusDeleteBST(BiTree*T,KeyTypekey){
if(!(*T))
{returnFALSE;}
else{
if(EQ(key,(*T)->data))Delete(T);
elseif(LT(key,(*T)->data))DeleteBST(&((*T)->l),key);
elseDeleteBST(&((*T)->r),key);
returnTRUE;
}
}
main()
{
BiTreeroot;
BiTreesroot=NULL;
inti;
inta[10]={45,23,12,3,33,27,56,90,120,62};
system("cls");
CreateBiTree(&root);
printf("PreOrderTraverse:\n");
PreOrderTraverse(root,printelem);
printf("InOrderTraverse:\n");
InOrderTraverse(root,printelem);
for(i=0;i<10;i++)
InsertBST(&sroot,a[i]);
printf("InOrderTraverse:\n");
InOrderTraverse(sroot,printelem);
for(i=0;i<3;i++)
DeleteBST(&sroot,a[i]);
printf("Nowsroothasnodes:\n");
InOrderTraverse(sroot,printelem);
}
标签:二叉树,中序,遍历