<acronym id="xonnx"></acronym>
      <td id="xonnx"></td>
    1. <pre id="xonnx"></pre>

      1. 專注電子技術學習與研究
        當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

        二叉樹的前、中、后序遍歷

        作者:余春雨老師   來源:本站原創   點擊數:  更新時間:2014年04月26日   【字體:

          c程序如下:

        # include <stdio.h>
        # include <malloc.h>
         
        struct BTNode
        {
        char data;
        struct BTNode * pLchild;
        struct BTNode * pRchild;
        };
        struct BTNode * creat_BTree(void);
        void pro_traverse(struct BTNode * pT);
        void mid_traverse(struct BTNode * pT);
        void rear_traverse(struct BTNode * pT);
        int main(void)
        {
        struct BTNode * pT = creat_BTree();
         
        printf("二叉樹前序遍歷結果如下:\n");
        pro_traverse(pT);
        printf("\n");
         
        printf("二叉樹中序遍歷結果如下:\n");
        mid_traverse(pT);
        printf("\n");
         
        printf("二叉樹后序遍歷結果如下:\n");
        rear_traverse(pT);
        printf("\n");
         
        return 0;
        }
         
        void pro_traverse(struct BTNode * pT)
        {
        if(pT != NULL)
        {
        printf("%c   ",pT->data);
         
        if(NULL != pT->pLchild)
        pro_traverse(pT->pLchild);
         
        if(NULL != pT->pRchild)
        pro_traverse(pT->pRchild);
        }
         
        }
         
        void mid_traverse(struct BTNode * pT)
        {
        if(pT != NULL)
        {
         
         
        if(NULL != pT->pLchild)
        mid_traverse(pT->pLchild);
         
        printf("%c   ",pT->data);
         
        if(NULL != pT->pRchild)
        mid_traverse(pT->pRchild);
        }
         
        }
        void rear_traverse(struct BTNode * pT)
        {
        if(pT != NULL)
        {
         
        if(NULL != pT->pLchild)
        rear_traverse(pT->pLchild);
         
        if(NULL != pT->pRchild)
        rear_traverse(pT->pRchild);
         
        printf("%c   ",pT->data);
        }
         
        }
         
        struct BTNode * creat_BTree(void)
        {
        struct BTNode * pA = (struct BTNode *)malloc(sizeof(struct BTNode));
        struct BTNode * pB = (struct BTNode *)malloc(sizeof(struct BTNode));
        struct BTNode * pC = (struct BTNode *)malloc(sizeof(struct BTNode));
        struct BTNode * pD = (struct BTNode *)malloc(sizeof(struct BTNode));
        struct BTNode * pE = (struct BTNode *)malloc(sizeof(struct BTNode));
        struct BTNode * pF = (struct BTNode *)malloc(sizeof(struct BTNode));
        struct BTNode * pI = (struct BTNode *)malloc(sizeof(struct BTNode));
        struct BTNode * pG = (struct BTNode *)malloc(sizeof(struct BTNode));
        struct BTNode * pH = (struct BTNode *)malloc(sizeof(struct BTNode));
         
        pA->data = 'A';
        pB->data = 'B';
        pC->data = 'C';
        pD->data = 'D';
        pE->data = 'E';
        pF->data = 'F';
        pG->data = 'G';
        pH->data = 'H';
        pI->data = 'I';
         
        pA->pLchild = pB;
        pA->pRchild = pE;
         
        pB->pLchild = pC;
        pB->pRchild = pD;
         
        pC->pLchild = pC->pRchild = NULL;
         
        pD->pLchild = pD->pRchild = NULL;
         
        pE->pLchild = pF;
        pE->pRchild = pI;
         
        pF->pLchild = pF->pRchild = NULL;
         
        pI->pLchild = pG;
        pI->pRchild = pH;
         
        pG->pLchild = pG->pRchild = NULL;
         
        pH->pLchild = pH->pRchild = NULL;
         
        return pA;
         
        }

        運行結果如下:

         二叉樹前序遍歷結果如下:
        A   B   C   D   E   F   I   G   H
        二叉樹中序遍歷結果如下:
        C   B   D   A   F   E   G   I   H
        二叉樹后序遍歷結果如下:
        C   D   B   F   G   H   I   E   A
        Press any key to continue
        關閉窗口

        相關文章

        欧美性色欧美精品视频,99热这里只有精品mp4,日韩高清亚洲日韩精品一区二区,2020国自产拍精品高潮