• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
donneyming
博客园    首页    新随笔    联系   管理    订阅  订阅
c 哈夫曼~

发现写哈夫曼还是比较好玩 献丑了

:就是每次取2个最小的值算成一个新的节点直到最后一个节点

// hfm.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdlib.h"

int a[10];
struct node
{
 int value;
 int left;
 int right;
};
struct linknode
{
 node value;
 linknode *next;
};
void sort(int a[],int index)
{
 int temp = 0;
 for (int i=index;i<10;i++)
 {
  for (int j=i;j<10;j++)
  {
   if (a[i]>a[j])
   {
    temp = a[i];
    a[i] =a[j];
    a[j] =temp;
   }
  }
 }
}


int main(int argc, char* argv[])
{

 linknode *root =(linknode *)malloc(sizeof(linknode));
 linknode *list = root;

for (int i=0;i<10;i++)
 {
  a[i] =i+1;
 }
 for (int j=0;j<10;j++)
 {
  sort(a,j);
  list->next =(linknode *)malloc(sizeof(linknode));
  node temp;
  temp.value = a[j] +a[j+1];  
  temp.right =a[j+1];
  temp.left =a[j];

  list->value =temp;
  list =list->next;
  a[j+1] =a[j] +a[j+1]; 
  a[j] =0;

 }

 list->next =NULL;
 while (root->next)
 {
  printf("%d____",root->value.left);
  printf("%d\n",root->value.right);

  root =root->next;
 }

 return 0;
}

 

posted on 2009-03-10 02:23  donneyming  阅读(222)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3