이 진 트 리 가 옮 겨 다 니 는 재 귀적 실현

/*          */
#include<stdio.h>
typedef struct btnode
{
    char data;
    struct btnode *lchild,*rchild;      /*         */
}BinTree;
/*      */
BinTree * CreateTree(BinTree *T)
{
    char ch;
    scanf("%c",&ch);
    if(ch=='#')
        return 0;
    else
    {
        T=(BinTree *)malloc(sizeof(BinTree));
        T->data=ch;
        T->lchild=CreateTree(T->lchild);    /*     */
        T->rchild=CreateTree(T->rchild);    /*     */
        return(T);
    }
}
/*        T    */
void Preorder(BinTree *T)
{
    if(T)
    {
        printf("%c ",T->data);  /*     T*/
        Preorder(T->lchild);    /*       */
        Preorder(T->rchild);    /*       */
    }
}
/*        T    */
void inorder(BinTree *T)
{
    if(T)
    {
        inorder(T->lchild);     /*       */
        printf("%c ",T->data);  /*     T*/
        inorder(T->rchild);     /*       */
    }
}
/*        T    */
void postorder(BinTree *T)
{
    if(T)
    {
        postorder(T->lchild);   /*       */
        postorder(T->rchild);   /*       */
        printf("%c ",T->data);  /*     T*/
    }
}
main()
{
    BinTree *b;
    printf("                 :
"
); b=CreateTree(b); printf(" :
"
); Preorder(b); printf("
"
); printf(" :
"
); inorder(b); printf("
"
); printf(" :
"
); postorder(b); printf("
"
); }

좋은 웹페이지 즐겨찾기