forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffmantree.py
25 lines (23 loc) · 810 Bytes
/
huffmantree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Python 3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
from heapq import heapify as hpf
from heapq import heappop as hpp
from heapq import heappush as hppu
class Node:
def __init(self,ch,freq,left=None,right=None):
self.ch,self.freq=ch,freq
self.left,self.right=left,right
def __lt__(self,other):
return self.freq<other.freq
def getHuffmanTree(txt):
if len(txt)==0:
return
freq={ch:text.count(ch) for k,v in set(txt)}
pq=[Node(k,v) for k,v in freq.items()]
hpf(pq)
while len(pq)>1:
left,right=hpp(pq),hpp(pq);
newFreq=left.freq+right.freq
hppu(pq,Node(None,newFreq,left,right))
root=pq[0]
return root