데이터 구조의 이 진 트 리 의 각종 조작

#include 
#include 

#define LEN sizeof(struct BiTNode)

typedef struct BiTNode
{
    char data;
    struct BiTNode *lchild,*rchild;
}*BiTree;

void InitBiTree(BiTree *T)
{
    *T = NULL;
}

void CreateBiTree(BiTree *T)
{
    int ch;
    printf("Please input value:");
    scanf("%c",&ch);
    getchar();
    if(ch == '#')
        *T = NULL;
    else
    {
        *T = (BiTree)malloc(LEN);
        (*T) -> data = ch;
        CreateBiTree(&(*T) -> lchild);
        CreateBiTree(&(*T) -> rchild);
    }
}

void EmptyBiTree(BiTree T)
{
    if(T == NULL)
        printf("tree empty!
"
)
; else printf("tree not empty!
"
)
; } void ClearBiTree(BiTree *T) { if(*T) { if((*T) -> lchild) ClearBiTree(&(*T) -> lchild); if((*T) -> rchild) ClearBiTree(&(*T) -> rchild); free(*T); *T = NULL; } } int BiTreeDepth(BiTree T) { int i,j; if(!T) return 0; else { if(T -> lchild) i = BiTreeDepth(T -> lchild); else i = 0; if(T -> rchild) j = BiTreeDepth(T -> rchild); else j = 0; return i > j ? i + 1 : j + 1; } } int PrintDepth(BiTree T) { int n = BiTreeDepth(T); printf("depth = %d
"
,n)
; } void YeziBiTree(BiTree T) { if(T) { if(!(T -> lchild) && !(T -> rchild)) printf("%c",T -> data); YeziBiTree(T -> lchild); YeziBiTree(T -> rchild); } } void CenciBiTree(BiTree T) { int rear,front; BiTree b; BiTree queue[100]; rear = front = 0; queue[rear++] = T; while(rear != front) { b = queue[front++]; if(b) printf("%c",b -> data); if(b -> lchild) queue[rear++] = b ->
lchild; if(b -> rchild) queue[rear++] = b -> rchild; } } BiTree SearchBiTree(BiTree T,char e) { BiTree t; if(!T) return NULL; else { if(T -> data == e) return T; if(T -> lchild) t = SearchBiTree(T -> lchild,e); if(t) return t; if(T -> rchild) t = SearchBiTree(T -> rchild,e); if(t) return t; } return NULL; } int UpdateBiTree(BiTree T,char e) { char ch; if(!T) return 0; printf("Please input value:"); scanf("%c",&ch); getchar(); if(SearchBiTree(T,e)) { SearchBiTree(T,e) -> data = ch; } return 1; } void PreOrderTraverse(BiTree T) { if(!T) return; printf("%c",T -> data); PreOrderTraverse(T -> lchild); PreOrderTraverse(T -> rchild); } void InOrderTraverse(BiTree T) { if(!T) return; InOrderTraverse(T -> lchild); printf("%c",T -> data); InOrderTraverse(T -> rchild); } void PostOrderTraverse(BiTree T) { if(!T) return; PostOrderTraverse(T -> lchild); PostOrderTraverse(T -> rchild); printf("%c",T -> data); } int main() { BiTree T; int n; char ch; while(1) { printf("\t\t 1.Init 2.Create 3.Empty 4.Clear 5.Depth 6.Update
"
); printf("\t\t 7.Yezi 8.Cenci 9.Pre 10.In 11.Post 12.Exit
"
); printf("Please input function:"); scanf("%d",&n); getchar(); if(n == 12) break; else if(n == 6) { printf("Please input update value:"); scanf("%c",&ch); getchar(); } switch(n) { case 1:InitBiTree(&T);break; case 2:CreateBiTree(&T);break; case 3:EmptyBiTree(T);break; case 4:ClearBiTree(&T);break; case 5:PrintDepth(T);break; case 6:UpdateBiTree(T,ch);break; case 7:YeziBiTree(T);break; case 8:CenciBiTree(T);break; case 9:PreOrderTraverse(T);break; case 10:InOrderTraverse(T);break; case 11:PostOrderTraverse(T);break; } } return 0; }

좋은 웹페이지 즐겨찾기