数据结构之---C语言实现哈夫曼树和编码

//哈夫曼树
//杨鑫
#include 
#include 
typedef int ElemType;
struct BTreeNode
{
    ElemType data;
    struct BTreeNode* left;
    struct BTreeNode* right;
};

//遍历哈夫曼树
void PrintBTree_int(struct BTreeNode* BT)
{
    if (BT != NULL)
    {
        printf("%d", BT->data); 
        if (BT->left != NULL || BT->right != NULL)
        {
            printf(" ( ");
            PrintBTree_int(BT->left); //输出左子树
            if (BT->right != NULL)
                printf(" , ");
            PrintBTree_int(BT->right); //输出右子树
            printf(" ) ");
        }
    }
}

//创建哈夫曼树
struct BTreeNode* CreateHuffman(ElemType a[], int n)
{
    int i, j;
    struct BTreeNode **b, *q;
    b = malloc(n*sizeof(struct BTreeNode));
    for (i = 0; i < n; i++) 							//动态内存分配
    {
        b[i] = malloc(sizeof(struct BTreeNode));
        b[i]->data = a[i];
        b[i]->left = b[i]->right = NULL;
    }
    for (i = 1; i < 

你可能感兴趣的:(C,&&,C++,数据结构,算法)