体系结构 通过python实现 哈夫曼编码 扩展编码 等长编码

#!usr/bin/env python
# -*- coding: utf-8 -*-


def fun():
        Q = x  # 通过队列进行处理
        while len(Q) > 1:
            Q.sort(key=lambda xx: xx.prob)# 根据使用频度排序
            # for i in Q:
            #     print i.prob
            l = Q.pop(0)
            r = Q.pop(0)
            if l.name is not None:
                newQ.append(l)
            if r.name is not None:
                newQ.append(r)
            # 左1右0左小右大
            l.codeword = '0'
            r.codeword = '1'

            fa = hafnode(prob=l.prob+r.prob)
            fa.l = l
            fa.r = r
            l.fa = fa
            r.fa = fa
            Q.append(fa)

        Q[0].fa = None
        return Q[0]

def hufdeal():
        root = fun()
        pro = []
        codes = [''] * len(newQ)
        symbol = []

        for i in range(len(newQ)):
            tmp = newQ[i]
            while tmp.fa != None:
                codes[i] = tmp.codeword + codes[i]
                tmp = tmp.fa
            newQ[i].codeword = codes[i]



class hafnode():
    def __init__(self, name=None, prob=None, codeword='', procode=None):
        self.name = name
        self.prob = prob
        self.codeword = codeword
        self.procode = procode
        self.fa = None
        self.l = None
        self.codeword = None

# ----------------------------------哈夫曼编码-----------------------------------------------------
class dengchang():
    def __init__(self, name=None, prob=None, codeword=''):
        self.name = name
        self.prob = prob
        self.codewode = codeword


def dcdeal():
    n = 0
    for i in range(1, 10):
        if 2**i >= len(y):
            n = i
            break
    for i in range(len(y)):
        y[i].codeword = str(bin(i))[2:].zfill(n)
        print y[i].codeword
# ------------------------------------等长编码-----------------------------------


def extend(n):
    # x = input("请输入需要几种编码长度?")
    # len = []
    # for i in range(0,10):
    #    len[i] = (2**i)-1

    print '3-5编码的结果是:\n'
    z.sort(key=lambda xx: xx.prob, reverse=True)
    cnt = 0

    for i in range(len(z)):
        if cnt == 7:
            break
        if i <= len(z)-2:
            tmp = z[i].prob/z[i+1].prob
            if int(tmp*1000000+5) >= int(3.0*1000000):
                break
        cnt += 1
    cnt+=1
    for i in range(cnt):
        z[i].codeword = str(bin(i))[2:].zfill(3)
        print z[i].codeword
    for i in range(cnt, len(z)):
        z[i].codeword = str(bin(i-cnt))[2:].rjust(3, '0').rjust(5, '1')
        print z[i].codeword

x = []
y = []
z = []
newQ = []  # 保存使用过的节点


def main():
    n = input("请输入个数:")
    for i in range(n):
        rate = input("请输入第%d的频率:" % i)
        x.append(hafnode(i, rate, ''))
        y.append(dengchang(i, rate))
        z.append(dengchang(i,rate))

    hufdeal()

    print '输出哈夫曼编码:\n'
    for i in newQ:
         print i.name, i.codeword
    print '输出等长编码:\n'
    dcdeal()
    extend(n)


    len1 = 0
    len2 = 0
    len3 = 0

    for i in range(len(z)):
        len1 += len(newQ[i].codeword)
        len2 += len(y[i].codeword)
        len3 += len(z[i].codeword)

    len1 = float(1.0*len1/n)
    len2 = float(1.0*len2/n)
    len3 = float(1.0*len3/n)
    print "哈夫曼编码平均码长为%.2f,等长编码平均码长为%.2f,扩展编码平均码长为%.2f" % (len1,len2,len3)



if __name__ == '__main__':
    main()

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值