Chinaunix首页 | 论坛 | 博客
  • 博客访问: 519362
  • 博文数量: 96
  • 博客积分: 2102
  • 博客等级: 上尉
  • 技术积分: 1695
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:12
文章分类

全部博文(96)

文章存档

2014年(2)

2012年(94)

分类: C/C++

2012-04-30 10:44:00

        二叉树也是 递归 定 义的,其结点有左右子树之分,逻辑上二叉树有五种基本形态:
  •  空二叉树——(a);
  • 只有一个根结点的二叉树——(b);
  • 右子树为空的二叉树——(c);
  • 左子树为空的二叉树——(d);
  • 完全二叉树——(e)   注意:尽管二叉树与树有许多相似之处,但二叉树是树的特殊情形。
      二叉树的递归一直是一个另大家纠结的问题。尤其是二叉树的非递归,十分令人头痛。
      

中序的递归遍历:

  1. void walk(BTree * node)
  2. {
  3.    for(;;) {
  4.                   if(node==NULL)
  5.                        return ;
  6.                    walk(node->left);
  7.                    printf("%d ",node->data);
  8.                    walk(node->right);
  9.          }
  10. }
        中序的非递归遍历: 左----根----右
        由上面的递归信息了解到,中序的中序遍历首先访问的最左侧最喜下边的节点。
        因此可以使用循环一直向左子树的左孩子遍历,由于要对访问过的节点对右孩子进行访问,因此使用栈将方法访问的节点全部记录 。找到最左侧之后。出栈--------》访问 -----(将出栈的每个结点看作只有右子树的节点)-------遍历右子树------(将右结点看作一个新的二叉树又重复上面的工作--------循环;
       

非递归实现

  1. void Fdg_walk(BTree *node)
  2. {
  3.   for(;;) {
  4.        while(node)<=======================>if(node):考虑到时间的复杂度两者是等价的。
  5.        {
  6.          PUSH(stack,*node);
  7.           node = node->left;
  8.         }
  9.         访问;
  10.          POP(stack,node);
  11.         node = node->right;
  12.         if( isStackEmpty(stack) == TRUE)
  13.              return ;
  14.     }
  15. }

销毁二叉树!!!!!

  1. static void destroy(struct BTree *node)
  2. {
  3.             if(node == NULL)
  4.                       return ;
  5.             destroy(node->left);
  6.             destroy(node->right);
  7.            free(node);
  8. }
如果是含有父节点指针的结构体,那么就不需要栈了 。
穿线-----》记录前趋和后继;


使用二叉树构建哈夫曼的压缩和相应的解压缩 ;

点击(此处)折叠或打开

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

  4. #define MAX 128
  5. struct NODE {
  6.     char ch;
  7.     int fre;
  8.     int parent;
  9.     int Lchild;
  10.     int Rchild;
  11.     char *code;
  12. };
  13. struct MESS {
  14.     int total;
  15.     struct NODE * static_link;
  16. };

  17. int getString(struct MESS*,char**);
  18. void fillMessage(struct MESS*, int *,int);
  19. void creatTree(struct MESS *);
  20. int findSmall(struct MESS);
  21. void makeCode(struct MESS *);
  22. char * haFuMa(struct MESS, char *);
  23. void unHaFuMa(struct MESS,char *);
  24. void unHaFuMa(struct MESS mess,char *code)
  25. {
  26.   int index = mess.total-1,in=0;
  27.   char st[80];
  28.   printf("please input the string\n");
  29.   gets(st);
  30.   while(st[in])
  31.   {
  32.       if(mess.static_link[index].ch=='#')
  33.       {
  34.           if(st[in]=='0')
  35.           index = mess.static_link[index].Lchild;
  36.         else
  37.               index = mess.static_link[index].Rchild;
  38.           in++;
  39.       }
  40.       else
  41.       {
  42.           printf("%c",mess.static_link[index].ch);
  43.           index = mess.total-1;
  44.       }
  45.   }
  46.   if(mess.static_link[index].
  47.       printf("%c\n",mess.static_link[index].ch);
  48.   else
  49.       printf("\nhas wrong\n");
  50. }
  51. char * haFuMa(struct MESS mess, char*st)
  52. {
  53.     int index = 0;
  54.     char *code = (char *)malloc(sizeof(char)*300);
  55.     printf("%d \n",mess.total);
  56.     for(index = 0; index <(mess.total); index++)
  57.         printf("%c %d %d %d %d %s\n",mess.static_link[index].ch,mess.static_link[index].fre,mess.static_link[index].parent,mess.static_link[index].Lchild,mess.static_link[index].Rchild,mess.static_link[index].code);
  58.     printf("\n******************************************************************************\n");
  59.     while(*st) {
  60.     for(index = 0; index <(mess.total+1)/2&&mess.static_link[index].ch!=*st; index++)
  61.                 ;
  62.     if(mess.static_link[index].ch==*st)
  63.         strcat(code,mess.static_link[index].code);
  64.     st++;
  65.     }
  66.     return code;
  67. }
  68. void makeCode(struct MESS *mess)
  69. {
  70.     int index = 0,in ,n,count, ij =0;
  71.     char code[(mess->total+1)/2+1];

  72.     code[(mess->total+1)/2] = 0;
  73.     for(index = 0; index <( mess->total+1)/2; index++) {
  74.         count = (mess->total+1)/2-1;
  75.         ij =0;
  76.         in = mess->static_link[index].parent;
  77.         n = index;
  78.         while(in>=0) {
  79.         if(mess->static_link[in].Lchild == n)
  80.             code[count--] = '0';
  81.         else
  82.             code[count--] = '1';
  83.         n = in;
  84.         in = mess->static_link[in].parent ;
  85.         ij++;
  86.         }
  87.         strcpy(mess->static_link[index].code,code+count+1);
  88.     }
  89. }
  90. int findSmall(struct MESS mess)
  91. {
  92.     int index = 0,count = -1;

  93.     for(index =0;index<mess.total;index++)
  94.     {
  95.         if(mess.static_link[index].parent==-1) {
  96.             if(count==-1)
  97.                 count = index;
  98.          if(mess.static_link[index].fre<mess.static_link[count].fre)
  99.              count = index;
  100.         }
  101.     }
  102.     return count;
  103. }
  104. void fillMessage(struct MESS*mess, int *ch, int total)
  105. {
  106.     int count = 0,index=0;
  107.     int il = -1,ir = -1;
  108.     mess->total = total;
  109.     mess->static_link = (struct NODE *)malloc((2*total-1)*sizeof(struct NODE));
  110.     for(count = 0; count < MAX; count++)
  111.         if(ch[count]!=0) {
  112.             mess->static_link[index].ch = count;
  113.             mess->static_link[index].fre = ch[count];
  114.             mess->static_link[index].Rchild = mess->static_link[index].Lchild = mess->static_link[index].parent = -1;
  115.             mess->static_link[index++].code = (char *)malloc(total*sizeof(char));
  116.         }
  117.     while(mess->total < 2*total-1&&(il = findSmall(*mess))!=-1)
  118.         {
  119.             mess->static_link[il].parent = index;
  120.             ir = findSmall(*mess);
  121.             if(ir !=-1) {
  122.                 mess->static_link[index].ch ='#';
  123.                 mess->static_link[index].fre = mess->static_link[ir].fre+mess->static_link[il].fre;
  124.                 mess->static_link[index].Rchild=ir;
  125.                 mess->static_link[index].Lchild = il;
  126.                 mess->static_link[ir].parent= index;
  127.                    mess->static_link[index].parent = -1;
  128.                 mess->static_link[index++].code = NULL;
  129.                 mess->total++;
  130.         }
  131.     }
  132. }
  133. int getString(struct MESS *mess, char **st)
  134. {
  135.     char *string;
  136.     int index,count = 0, in = 0, ch[MAX] = {0};

  137.     string = (char*)malloc(sizeof(char)*MAX);
  138.     printf("please input the complier string\n");
  139.     count = read(0,string,MAX);
  140.     for(index =0 ; index < count-1; index++) {
  141.         if(ch[*(string+index)]==0)
  142.             in++;
  143.         ch[*(string+index)]++;
  144.     }
  145.     fillMessage(mess,ch,in);
  146.     makeCode(mess);
  147.     *st=haFuMa(*mess,string);
  148.     free(string);
  149. }
  150. int main()
  151. {
  152.     struct MESS mess = {0,NULL};
  153.     char *code;
  154.     getString(&mess,&code);
  155.     puts(code);
  156.     unHaFuMa(mess,code);
  157. }



阅读(1200) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~