이 진 링크 의 실현 과 응용

#include
#include
#include
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define NULL 0
typedef char TElemType;
typedef int Status;
typedef struct BiTNode{
     
    TElemType data;
    struct BiTNode *lchild,*rchild;//        
}BiTNode,*BiTree;
Status InitBiTree(BiTree *T){
     
    *T=NULL;//
    return OK;
}
BiTree MakeBiTree(TElemType e,BiTree L,BiTree R){
     //    
//       T,        e,L R       ,   
    BiTree T;
    T=(BiTNode*)malloc(sizeof(BiTNode));
    if(NULL==T)return NULL;
    T->data=e;//      e
    T->lchild=L;
    T->rchild=R;
    return T;

}
BiTree CreatBiTree(char *defBT,int *p){
     //  
//             ,defBT       ,i defBT     ,    0
        BiTree T;
        TElemType ch;
        ch=defBT[(*p)++];
		if('#'==ch)InitBiTree(&T);
        else {
     
            T=MakeBiTree(ch,NULL,NULL);//    ch
		    T->lchild=CreatBiTree(defBT,p);//           
            T->rchild=CreatBiTree(defBT,p);
        }
        return T;
}
Status FirstOderTraverse_VLR(BiTree T){
     
    if(T!=NULL){
     
        printf("%c",T->data);
		InOderTraverse_LVR(T->lchild);
        InOderTraverse_LVR(T->rchild);
    }
}
Status InOderTraverse_LVR(BiTree T){
     
    if(T!=NULL){
     
        InOderTraverse_LVR(T->lchild);
        printf("%c",T->data);
        InOderTraverse_LVR(T->rchild);
    }
}
Status AfterOderTraverse_LRV(BiTree T){
     
    if(T!=NULL){
     
       InOderTraverse_LVR(T->lchild);
        InOderTraverse_LVR(T->rchild);
		 printf("%c",T->data);
    }
}
void main(){
     
    BiTree T1,a,b,c;
	char tree[]="ABD##E##C##";
	int i=0;
   
    if(InitBiTree(&T1)){
     
        printf("     !
"
); }else{ printf(" !
"
); } T1=CreatBiTree(tree,&i); printf("
"
); FirstOderTraverse_VLR(T1); printf("
"
); printf("
"
); InOderTraverse_LVR(T1); printf("
"
); printf("
"
); AfterOderTraverse_LRV(T1); printf("
"
); }

좋은 웹페이지 즐겨찾기