Huffman编码

#include 
#include 
#include 

using namespace std;

const int MAXN = 1010;

typedef int ElemType;

struct HuffmanNode
{
    ElemType data;
    HuffmanNode* pLeft;
    HuffmanNode* pRight;
    HuffmanNode()
    {
        data = 0;
        pLeft = pRight = NULL;
    }
};


HuffmanNode* CreatHuffmanTree(ElemType a[], int n)//建树
{
    HuffmanNode **b, *p;
    b = new HuffmanNode* [n];
    for(int i = 0; i < n; ++i)
    {
        b[i] = new HuffmanNode;
        b[i]->data = a[i], b[i]->pLeft = b[i]->pRight = NULL;
    }
    for(int i = 1; i < n; ++i)//控制次数,在F集合中不断地选出两个权值最小的树
    {
        int k1 = -1, k2;
        for(int j = 0; j < n; ++j)
        {
            if(b[j] != NULL && k1 == -1)
            {
                k1 = j;
                continue ;
            }
            if(b[j] != NULL)
            {
                k2 = j;
                break ;
            }
        }
        for(int j = k2; j < n; ++j)
        {
            if(b[j] != NULL)
            {
                if(b[j]->data < b[k1]->data)
                    k2 = k1, k1 = j;
                else if(b[j]->data < b[k2]->data)
                    k2 = j;
            }
        }//建立新的树,并且删除旧的树
        p = new HuffmanNode;
        p->data = b[k1]->data + b[k2]->data;
        p->pLeft = b[k1];
        p->pRight = b[k2];
        b[k1] = p, b[k2] = NULL;
    }
    delete []b;
    return p;
}
ElemType WeightPathLenth(HuffmanNode* pRoot, int Len)//哈夫曼树的带权路径长度
{
    if(pRoot == NULL)
        return 0;
    if(pRoot->pLeft == NULL && pRoot->pRight == NULL)
        return pRoot->data * Len;
    return WeightPathLenth(pRoot->pLeft, Len+1) + WeightPathLenth(pRoot->pRight, Len+1);
}
void HuffmanCoding(HuffmanNode* pRoot, int Len)//编码
{
    static int a[MAXN];
    if(pRoot == NULL)
        return ;
    if(pRoot->pLeft == NULL && pRoot->pRight == NULL)
    {
        printf("结点权值为%d的编码: \n", pRoot->data);
        for(int i = 0; i < Len; ++i)
            printf("%d ", a[i]);
        printf("\n");
        return ;
    }
    a[Len] = 0;
    HuffmanCoding(pRoot->pLeft, Len+1);
    a[Len] = 1;
    HuffmanCoding(pRoot->pRight, Len+1);
}

void PrintBTree(HuffmanNode* pRoot)
{
    if(pRoot != NULL)//如果该树不空那么就输出根节点的值
    {
        printf("%d", pRoot->data);
        if(pRoot->pLeft != NULL || pRoot->pRight)//输出广义表的形式
        {
            printf("(");
            PrintBTree(pRoot->pLeft);
            if(pRoot->pRight != NULL)
                printf(",");
            PrintBTree(pRoot->pRight);
            printf(")");
        }
    }
}

void ClearBTree(HuffmanNode* pRoot)
{
    if(pRoot != NULL)
    {
        ClearBTree(pRoot->pLeft);
        ClearBTree(pRoot->pRight);
        delete pRoot;
        pRoot = NULL;
    }
    return ;
}

int main()
{
	int n, i;
	HuffmanNode* fbt = NULL;
	int T;
	scanf("%d", &T);
	while (T--)
	{
		cout<<"输入构造哈夫曼树中带权结点数n:";
		cin>>n;
		ElemType* a = new ElemType[n];
		cout<<"输入"<>a[i];
		fbt = CreatHuffmanTree(a, n);
		cout<<"广义表形式的哈夫曼树: ";
		PrintBTree(fbt);
		cout<<"哈夫曼树的权:";
		cout<

你可能感兴趣的:(数据结构,数据结构,Huffman)