Chinaunix首页 | 论坛 | 博客
  • 博客访问: 65937
  • 博文数量: 12
  • 博客积分: 230
  • 博客等级: 入伍新兵
  • 技术积分: 210
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-22 13:20
文章分类

全部博文(12)

文章存档

2012年(12)

我的朋友

分类: C/C++

2012-03-29 20:21:34


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct _bitree_
  4. {
  5.     int data;
  6.     //char data; 鎵撳嵃瀛楁瘝锛?
  7.     struct _bitree_ *lchild , *rchild;
  8. }bitree;

  9. bitree *CreatBittree(int i,int n)
  10. {
  11.     bitree *root;

  12.     root = (bitree *)malloc(sizeof(bitree));
  13.     root->data = i;

  14.     if(2*i <= n)
  15.     {
  16.         root->lchild = CreatBittree(2*i,n);
  17.     }else{
  18.         root->lchild = NULL;
  19.     }

  20.     if(2*i + 1 <= n)
  21.     {
  22.         root->rchild = CreatBittree(2*i + 1 , n);
  23.     }else{
  24.     
  25.         root->rchild = NULL;
  26.     }

  27.     return root;
  28. }

  29. void PreOrder(bitree *root)
  30. {
  31.     if(NULL==root)
  32.         return;

  33.     printf("%4d",root->data);

  34.     PreOrder(root->lchild);
  35.     PreOrder(root->rchild);

  36.     return;
  37. }

  38. void InOrder(bitree *root)
  39. {
  40.     if(NULL == root)
  41.     {
  42.         return ;
  43.     }

  44.     InOrder(root ->lchild);
  45.     printf("%4d",root->data);
  46.     InOrder(root ->rchild);

  47.     return ;
  48. }
  49. //
  50. //灞傛閬嶅巻锛屽畾涔夐槦鍒楋紱
  51. //
  52. typedef bitree *datatype;

  53. typedef struct _node_
  54. {
  55.     datatype data;
  56.     struct _node_ *next;
  57. }linknode, *linklist;

  58. typedef struct
  59. {
  60.     linklist front ,rear;
  61. }linkqueue;

  62. linkqueue *Creatlinkqueue()
  63. {
  64.     linkqueue *lq;

  65.     lq = (linkqueue *)malloc(sizeof(linkqueue));

  66.     lq->front = lq->rear = (linklist)malloc(sizeof(linknode));
  67.     lq->front->next =NULL;

  68.     return lq;
  69. }

  70. int EmptyLinkqueue(linkqueue *lq)
  71. {
  72.     return (lq->front == lq->rear);
  73. }

  74. void Enlinkqueue(linkqueue *lq,datatype x)
  75. {
  76.     linklist p;

  77.     p = (linklist)malloc(sizeof(linknode));
  78.     p ->data = x;
  79.     p ->next = NULL;
  80.     lq->rear->next = p;
  81.     lq ->rear = p;

  82.     return ;
  83. }

  84. datatype Delinkqueue(linkqueue *lq)
  85. {
  86.     if(EmptyLinkqueue(lq))
  87.         return ;

  88.     linklist p = (linklist)malloc(sizeof(linklist));

  89.     p = lq->front ; //绉诲姩涓娆ront鎸囬拡锛涗粠绗竴涓妭鐐瑰紑濮嬪幓鍑洪槦锛?
  90.     lq->front = p->next;
  91.     free(p);

  92.     return lq->front->data;
  93. }

  94. //
  95. //灞傛閬嶅巻锛?
  96. //
  97. void NoOrder(bitree *root)
  98. {
  99.     if(NULL == root)
  100.         return;
  101.     linkqueue *lq = Creatlinkqueue();

  102.     Enlinkqueue(lq , root);//鍏堝叆闃熸牴锛?
  103.     while(!EmptyLinkqueue(lq))
  104.     {
  105.         root = Delinkqueue(lq); //鏈夊簭鍑洪槦锛屽苟灏嗗叾瀛愬瀛愬叆闃燂紱閫掑綊锛?
  106.         printf("%4d",root->data);
  107.         
  108.         if(root ->lchild != NULL)
  109.             Enlinkqueue(lq ,root->lchild);
  110.         if(root->rchild!=NULL)
  111.             Enlinkqueue(lq ,root->rchild);
  112.     }

  113.     return ;
  114. }

  115. void PostOrder(bitree *root)
  116. {
  117.     if(NULL == root)
  118.         return ;

  119.     PostOrder(root->lchild);
  120.     PostOrder(root->rchild);
  121.     printf("%4d",root->data);
  122. }

  123. int main()
  124. {
  125.     bitree *root;

  126.     root = CreatBittree(1 , 10);

  127.     printf("PreOrder:\n");
  128.     PreOrder(root);
  129.     printf("\n");

  130.     printf("InOrder:\n");
  131.     InOrder(root);
  132.     printf("\n");

  133.     printf("PostOrder:\n");
  134.     PostOrder(root);
  135.     printf("\n");

  136.     printf("NoOrder:\n");
  137.     NoOrder(root);
  138.     printf("\n");        

  139.     return 0;

  140. }
阅读(1411) | 评论(0) | 转发(0) |
0

上一篇:深入理解C程序内存布局

下一篇:图的存储

给主人留下些什么吧!~~