Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2226617
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: C/C++

2012-08-25 21:03:34

点击(此处)折叠或打开

  1. 二叉树操作
  2.                 遍历
  3.                       
  4.                       先序遍历【先访问根节点】
  5.                               先访问根节点
  6.                               再先序访问左子树
  7.                               再先序访问右子树
  8.                       
  9.                       中序遍历【中间访问根节点】
  10.                               中序遍历左子树
  11.                               再访问根节点
  12.                               再中序遍历右子树
  13.                               
  14.                       后序遍历【最后访问根节点】
  15.                               先后序遍历左子树
  16.                               再后序遍历右子树
  17.                               再访问根节点

点击(此处)折叠或打开

  1. 链式二叉树.cpp
  2. --------------------------------------
  3. #include <stdio.h>
  4. #include <malloc.h>
  5. typedef struct BTNode
  6. {
  7.      char data;
  8.      struct BTNode * pLchild;
  9.      struct BTNode * pRchild;
  10. }BTNode;

  11. BTNode * CreateBTree(void);
  12. void PreTraverseBTree(BTNode *);
  13. void InTraverseBTree(BTNode *);
  14. void PostTraverseBTree(BTNode *);
  15. int main()
  16. {
  17.     BTNode * pT = CreateBTree();
  18.     //PreTraverseBTree(pT);
  19.     //InTraverseBTree(pT);
  20.     PostTraverseBTree(pT);

  21.     return 0;
  22. }

  23. BTNode * CreateBTree(void)
  24. {
  25.    BTNode * pA = (BTNode *)malloc(sizeof(BTNode));
  26.    BTNode * pB = (BTNode *)malloc(sizeof(BTNode));
  27.    BTNode * pC = (BTNode *)malloc(sizeof(BTNode));
  28.    BTNode * pD = (BTNode *)malloc(sizeof(BTNode));
  29.    BTNode * pE = (BTNode *)malloc(sizeof(BTNode));
  30.     
  31.    pA->data = 'A';
  32.    pB->data = 'B';
  33.    pC->data = 'C';
  34.    pD->data = 'D';
  35.    pE->data = 'E';
  36.     
  37.    pA->pLchild = pB;
  38.    pA->pRchild = pC;
  39.    pB->pLchild = pB->pRchild = NULL;
  40.    pC->pLchild = pD;
  41.    pC->pRchild =NULL;
  42.    pD->pLchild =NULL;
  43.    pD->pRchild =pE;
  44.    pE->pLchild = pE->pRchild = NULL;

  45.    return pA;
  46. }

  47. void PreTraverseBTree(BTNode * pT)
  48. {
  49.      if (NULL!=pT)
  50.      {
  51.          printf("%c\n",pT->data);
  52.     
  53.      if (NULL!=pT->pLchild)
  54.          {
  55.          PreTraverseBTree(pT->pLchild); //pLchild代表整个左子树
  56.          }
  57.      if (NULL!=pT->pRchild)
  58.          {
  59.          PreTraverseBTree(pT->pRchild);
  60.          }
  61.      }
  62. }

  63. void InTraverseBTree(BTNode * pT)
  64. {
  65.     if (NULL!=pT)
  66.     {
  67.      if (NULL!=pT->pLchild)
  68.      {
  69.          InTraverseBTree(pT->pLchild); //pLchild代表整个左子树
  70.      }
  71.        
  72.      printf("%c\n",pT->data);

  73.      if (NULL!=pT->pRchild)
  74.      {
  75.          InTraverseBTree(pT->pRchild);
  76.      }
  77.     }
  78. }

  79. void PostTraverseBTree(BTNode * pT)
  80. {
  81.     if (NULL!=pT)
  82.     {
  83.      if (NULL!=pT->pLchild)
  84.      {
  85.          PostTraverseBTree(pT->pLchild); //pLchild代表整个左子树
  86.      }

  87.      if (NULL!=pT->pRchild)
  88.      {
  89.         PostTraverseBTree(pT->pRchild);
  90.      }
  91.      printf("%c\n",pT->data);
  92.     }
  93. }
阅读(1386) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~