[数据结构]计算WPL 解题报告

Problem Description

Huffman编码是通信系统中常用的一种不等长编码,它的特点是:能够使编码之后的电文长度最短。


输入:

第一行为要编码的符号数量n
第二行~第n+1行为每个符号出现的频率

输出:

对应哈夫曼树的带权路径长度WPL


测试输入

5
7
5
2
4
9

测试输出

WPL=60

AcCode

//
//  main.cpp
//  计算WPL
//
//  Created by jetviper on 2017/3/26.
//  Copyright © 2017年 jetviper. All rights reserved.
//

#include <stdio.h>
#define true 1
#define false 0
typedef  struct {
    unsigned int weight;
    unsigned int parent,leftChild,rightChild;
}HTNode, *HuffmnTree;

HTNode hufmanTree[100000];
int main() {
    
    int num,m;
    scanf("%d",&num);
    m = 2 * num -1;
    for(int i=1;i<=num;i++){
        scanf("%d",&hufmanTree[i].weight);
        hufmanTree[i].parent = 0;
        hufmanTree[i].leftChild = 0;
        hufmanTree[i].rightChild = 0;
    }
    int s1,s2,max1,max2,iset1,iset2;
    
    for(int i=num+1;i<=m;i++){
        max1 =0,max2=0;
        iset1 =0,iset2=0;
        for(int j=1;j<i;j++){
            if(hufmanTree[j].parent == 0){
                if(iset1 == 0){
                    max1 = hufmanTree[j].weight;
                    s1 = j;
                    iset1 = 1;
                    continue;
                }
                if(max1>hufmanTree[j].weight){
                    max1 = hufmanTree[j].weight;
                    s1 = j;
                    
                }
                
            }
        }
        for(int j =1;j<i;j++){
            if(j == s1)continue;
            if(hufmanTree[j].parent == 0) {
                if (iset2 == 0) {
                    max2 = hufmanTree[j].weight;
                    s2 = j;
                    iset2 = 1;
                    continue;
                }
                if (max2 > hufmanTree[j].weight) {
                    max2 = hufmanTree[j].weight;
                    s2 = j;
                }
            }
        }
        
        hufmanTree[s1].parent = i;
        hufmanTree[s2].parent = i;
        hufmanTree[i].leftChild = s1;
        hufmanTree[i].rightChild = s1;
        hufmanTree[i].weight = hufmanTree[s1].weight + hufmanTree[s2].weight;
    }
    
    int result =0;
    for(int i =1;i<=num;i++){
        
        if(hufmanTree[i].parent != 0){
            int temp= hufmanTree[i].parent;
            int count = 1;
            while(1){
                if(hufmanTree[temp].parent!=0){
                    temp = hufmanTree[temp].parent;
                    count++;
                    continue;
                }
                else break;
            }
            result += count * hufmanTree[i].weight;
        }
    }
    
    printf("WPL=%d\n",result);
    
    
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,554评论 19 139
  • 前面的文章主要从理论的角度介绍了自然语言人机对话系统所可能涉及到的多个领域的经典模型和基础知识。这篇文章,甚至之后...
    我偏笑_NSNirvana阅读 14,433评论 2 64
  • 我是一个单身狗,可我现在却要来跟大家讨论“表白”——这个对我来说久违的事情。 (久违你也有脸给我们讲!!!) 那又...
    看星星的人阅读 785评论 1 1
  • 为了应景,写点和孩子有关的事情,但,也许无关。 1 昨天去了一所特殊学校,和孩子们待了一天。这里大多是语言迟缓和身...
    思洁大太阳哦阅读 327评论 1 1
  • 总说生活源自根本,其实相处并不是一件易事。爱人、家人、朋友,以至身边的各种关系,不与我相关的人和事,可以不去关心,...
    了了烁然阅读 532评论 0 0

友情链接更多精彩内容