• 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
    ",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
    ",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
    ",pT->data);
        }
    }
    int main ()
    {
        BTNode* pT = createBTree();
        printf("first traverse print: 
    ");
        First_print(pT);
        printf("middle traverse print: 
    ");
        Middl_print(pT);
        printf("end traverse print: 
    ");
        end_print(pT);
        return 0;
    }
    


    关注公众号 海量干货等你
  • 相关阅读:
    HDU 3695 Computer Virus on Planet Pandora (AC自己主动机)
    Crawley Project · Crawley Project
    Ubuntu下deb包的安装方法
    Google Chrome 59 Released – Install on Debian, Ubuntu and Linux Mint
    如何从 WEB 页面中提取信息 | Binuxの杂货铺
    Binuxの杂货铺 Data Highlighter
    pyspider爬京东商品信息案例分享
    Token bucket
    debian8上安装pyspider
    求助下 Ubuntu 15.10(64 位)下安装 pyspider 下的问题
  • 原文地址:https://www.cnblogs.com/sowhat1412/p/12734501.html
Copyright © 2020-2023  润新知