其他分享
首页 > 其他分享> > Tree 笨方法实现

Tree 笨方法实现

作者:互联网

#include <stdio.h>
#include <malloc.h>
typedef struct BTnode
{
    int data;
    struct BTnode *pLchild;
    struct BTnode *pRchild;
}BTNode;
BTNode* createBTree()//静态造链式树
{
    BTNode* pA =(BTNode*) malloc(sizeof(BTNode));
    BTNode* pB =(BTNode*) malloc(sizeof(BTNode));
    BTNode* pC =(BTNode*) malloc(sizeof(BTNode));
    BTNode* pD =(BTNode*) malloc(sizeof(BTNode));
    BTNode* pE =(BTNode*) malloc(sizeof(BTNode));
    pA->data = 'A';
    pB->data = 'B';
    pC->data = 'C';
    pD->data = 'D';
    pE->data = 'E';
    pA->pLchild = pB;
    pA->pRchild = pC;
    pB->pLchild = NULL;
    pB->pRchild = NULL;
    pC->pLchild = pD;
    pC->pRchild = NULL;
    pD->pLchild = NULL;
    pD->pRchild = pE;
    pE->pLchild = NULL;
    pE->pRchild = NULL;
    return pA;
}
void First_print(BTNode* pT) //先序遍历
{
    //先访问根节点  先序访问左子树 先序访问右子树
    if(pT)
    {
        printf("%c\n",pT->data);
        if(pT->pLchild)
        {
            First_print(pT->pLchild);
        }
        if(pT->pRchild)
        {
            First_print(pT->pRchild);
        }
    }
}
void Middl_print(BTNode* pT) //mid序遍历
{
    //先访问根节点  先序访问左子树 先序访问右子树
    if(pT)
    {
        if(pT->pLchild)
        {
            Middl_print(pT->pLchild);
        }
        printf("%c\n",pT->data);
        if(pT->pRchild)
        {
            Middl_print(pT->pRchild);
        }
    }
}
void end_print(BTNode* pT) //先序遍历
{
    //先访问根节点  先序访问左子树 先序访问右子树
    if(pT)
    {
        if(pT->pLchild)
        {
            end_print(pT->pLchild);
        }
        if(pT->pRchild)
        {
            end_print(pT->pRchild);
        }
        printf("%c\n",pT->data);
    }
}
int main ()
{
    BTNode* pT = createBTree();
    printf("first traverse print: \n");
    First_print(pT);
    printf("middle traverse print: \n");
    Middl_print(pT);
    printf("end traverse print: \n");
    end_print(pT);
    return 0;
}



标签:pT,实现,data,pRchild,Tree,BTNode,pLchild,print,方法
来源: https://blog.51cto.com/u_14582976/2829171