二叉樹鏈式存儲的C實現

在實現二叉樹的鏈式存儲的過程中,我遇到了一些問題,感到對遞歸的理解還不夠深入。另外,代碼中有一處必須使用全局變量做數組索引,還在研究其中的原因,代碼已完成,現在貼在博客中供參考

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int TElemType;
typedef char String[MAXSIZE];
typedef struct BiTreeNode{
    TElemType data;
    struct BiTreeNode * lchild;
    struct BiTreeNode * rchild;
}BiTreeNode;
typedef struct BiTreeNode * BiTree;
String str;
int i=1;/*此處必須要用全局變量做索引,暫時不知爲何*/

/*對字符串進行賦值*/ 
Status AssignString(String S,char * chars){
    int i=0;
    if(strlen(chars)>MAXSIZE)
        return ERROR;
    else{
        S[0]=strlen(chars);
        for(i=1;i<=S[0];i++)
            S[i]=*(chars+i-1);
    }
    return OK;
}

Status Visit(TElemType c){
    printf("%c",c);
    return OK;
}

/*初始化二叉樹*/ 
Status InitBiTree(BiTree *T){
    (*T)=NULL;
    return OK;
}

/*創建二叉樹*/ 
Status CreateBiTree(BiTree *T){
    TElemType data;
    data=str[i++];
    if(data=='#')
        *T=NULL;
    else{
        *T=(BiTree)malloc(sizeof(BiTreeNode));
        if(!(*T))
           return ERROR;
        (*T)->data=data;
        CreateBiTree(&(*T)->lchild);/*遞歸建立左子樹*/ 
        CreateBiTree(&(*T)->rchild);/*遞歸建立右子樹*/ 
    }
}

/*判斷是否爲空樹*/ 
Status EmptyBiTree(BiTree T){
    if(!T)
       return TRUE;
    else
       return FALSE;
}

/*求樹的深度*/ 
Status DepthBiTree(BiTree T){
    int i,j;
    if(!T)
       return ERROR;
    else{
        if(T->lchild)
           i=DepthBiTree(T->lchild);
        else
           i=0;
        if(T->rchild)
           j=DepthBiTree(T->rchild);
        else
           j=0;
    }
    return i>j? i+1:j+1;
}

/*返回根節點*/ 
Status Root(BiTree T){
    return T->data;
}

/*銷燬二叉樹*/ 
Status DestroyBiTree(BiTree * T){
    if(!T)
       return ERROR;
    else{
        if((*T)->lchild)
        DestroyBiTree(&(*T)->lchild);/*遞歸銷燬左子樹*/ 
        if((*T)->rchild)
        DestroyBiTree(&(*T)->rchild);/*遞歸銷燬右子樹*/ 
    }
    free(*T);
    *T=NULL;/*free後指向空*/ 
}

/*前序遍歷*/ 
Status Preorder(BiTree T){
    if(!T)
        return ERROR;
    Visit(T->data);
    Preorder(T->lchild);
    Preorder(T->rchild);
    return OK;
}

/*中序遍歷*/ 
Status Inorder(BiTree T){
    if(!T)
       return ERROR;
    Inorder(T->lchild);
    Visit(T->data);
    Inorder(T->rchild);
    return OK;
}

/*後序遍歷*/ 
Status Lastorder(BiTree T){
    if(!T)
       return ERROR;
    Lastorder(T->lchild);
    Lastorder(T->rchild);
    Visit(T->data);
    return OK;
}

int main(void){
    BiTree T;
    AssignString(str,"ABDH#K###E##CFI###G#J##");
//  for(int i=1;i<=str[0];i++)
//     printf("%c ",str[i]);
    InitBiTree(&T);
    CreateBiTree(&T);
    printf("構造空二叉樹後,樹空否?%d(1:是 0:否) 樹的深度=%d\n",EmptyBiTree(T),DepthBiTree(T));
    printf("二叉樹的根爲: %c\n",Root(T));
    printf("\n前序遍歷二叉樹:");
    Preorder(T);
    printf("\n中序遍歷二叉樹:");
    Inorder(T);
    printf("\n後序遍歷二叉樹:");
    Lastorder(T);
    DestroyBiTree(&T);
    printf("\n清除二叉樹後,樹空否?%d(1:是 0:否) 樹的深度=%d\n",EmptyBiTree(T),DepthBiTree(T));
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章