python实现哈弗曼树进行数据压缩

#!/usr/bin/python

#Filename:readfile.py

codelist=[]

f=file(‘input.txt’,’r’)

for line in f.readlines():

     line=line.strip(‘\n’)

     codelist+=line.split(‘ ‘)

print codelist

###########################################

map={}

for i in codelist:

    if map.has_key(i)==False:

        map.setdefault(i,1)

    else:

        count=map.get(i)+1

        map[i]=count

print map

###########################################

#      sort map                           #

#      sort map                           #

###########################################

map=sorted(map.iteritems(),key=lambda asd:asd[1],reverse=True)

print map

list_map=list(map);

print list_map[0][1]

###############################################################

class HaffNode:

   def  __init__(self):

       self.weight=0

       self.flag=0

       self.parent=0

       self.leftChild=-1

       self.rightChild=-1

class Code:

    def __init__(self):

        self.code=””

        self.weight=0

        self.original_code=””

map_count=len(map)

node_list=list(range(map_count*2-1))

for i in node_list:

    temp=HaffNode()

    if i<map_count:

        temp.weight=list_map[i][1]

    node_list[i]=temp

for i in range(map_count-1):

    m1=m2=100000

    x1=x2=0

    for j in range(map_count+i):

        if node_list[j].weight<m1 and node_list[j].flag==0:

            m2=m1

            x2=x1

            m1=node_list[j].weight

            x1=j

        elif node_list[j].weight<m2 and node_list[j].flag==0:

            m2=node_list[j].weight

            x2=j

    node_list[x1].parent=map_count+i;

    node_list[x2].parent=map_count+i;

    node_list[x1].flag=1;

    node_list[x2].flag=1;

    node_list[map_count+i].weight=node_list[x1].weight+node_list[x2].weight

    node_list[map_count+i].leftChild=x1;

    node_list[map_count+i].rightChild=x2;

###################################################################

###################################################################

haffCode=list(range(map_count))

for i in range(map_count):

     k=0;f=0;temp_str=””;

     k=i;f=node_list[k].parent;

     while f!=0:

          if node_list[f].leftChild==k:

               temp_str+=’0′

               k=f;

               f=node_list[k].parent;

          elif node_list[f].rightChild==k:

               temp_str+=’1′

               k=f

               f=node_list[k].parent

     haffCode[i]=Code()

     haffCode[i].code=temp_str[::-1]

     haffCode[i].weight=node_list[i].weight

     haffCode[i].original_code=list_map[i][0]

##################################################################

#       change the file                                          #

#################################################################3

changeCode={}

for i in haffCode:

     changeCode[i.original_code]=i.code;

print “changeCode”,changeCode,”\n”

##################################################################

#              out put file                                      #

##################################################################

change_codelist=[]

for i in codelist:

     temp=changeCode[i]

     print “codelist”,i,”changeCode”,temp,’\n’

     change_codelist.append(temp)

print change_codelist,’\n’

print ‘############################################################’

print ‘write to the file’

f=file(‘output.txt’,’w’)

for i in change_codelist:

     f.write(i)

     f.write(‘\t’)

f.close()

print “Done ,over!”

input.txt

0010 0100 0010 0110 0000 0010 1011 0100 0010 0100 0110 0010
0010 0100 0010 0110 0000 0110 0010 0100 0110 0010 0010 0000
0010 0110 0010 0010 0010 0100 0100 0110 0010 0010 1000 0101
0001 0100 0010 0111 0010 0010 0111 0111 0100 0100 1000 0101
1100 0100 0100 0111 0010 0010 0111 1101 0010 0100 1111 0011

output.txt

11 01 11 100 0011 11 00101 01 11 01 100 11 11 01 11 100 0011 100 11 01 100 11 11 0011 11 100 11 11 11 01 01 100 11 1110101 10100 101100 01 11 000 11 11 000 000 01 01 10101 10100 101111 01 01 000 11 11 000 00100 11 01 101110 101101

    原文作者:哈夫曼树
    原文地址: https://blog.csdn.net/chujiangke001/article/details/17326013
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞