发现写哈夫曼还是比较好玩 献丑了
:就是每次取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;
}