-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffman.py
119 lines (85 loc) · 2.76 KB
/
huffman.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from heapq import heapify, heappush, heappop
class Tree:
def __init__(self, left, right):
self._left = left
self._right = right
def get_data(self):
return self._left.get_data() + self._right.get_data()
def get_max_depth(self):
left_depth = self._left.get_max_depth()
right_depth = self._right.get_max_depth()
if left_depth > right_depth:
return left_depth + 1
return right_depth + 1
def get_min_depth(self):
left_depth = self._left.get_min_depth()
right_depth = self._right.get_min_depth()
if left_depth < right_depth:
return left_depth + 1
return right_depth + 1
def __eq__(self, other):
return self.get_data() == other.get_data()
def __ne__(self, other):
return not (self == other)
def __lt__(self, other):
return self.get_data() < other.get_data()
def __le__(self, other):
return self.get_data() <= other.get_data()
def __gt__(self, other):
return self.get_data() > other.get_data()
def __ge__(self, other):
return self.get_data() >= other.get_data()
def __repr__(self):
return "Tree: " + str(self.get_data())
class Leaf:
def __init__(self, data):
self._data = data
def get_data(self):
return self._data
def get_max_depth(self):
return 0
def get_min_depth(self):
return 0
def __eq__(self, other):
return self.get_data() == other.get_data()
def __ne__(self, other):
return not (self == other)
def __lt__(self, other):
return self.get_data() < other.get_data()
def __le__(self, other):
return self.get_data() <= other.get_data()
def __gt__(self, other):
return self.get_data() > other.get_data()
def __ge__(self, other):
return self.get_data() >= other.get_data()
def __repr__(self):
return "Leaf: " + str(self.get_data())
def run_huffman(path):
_, weights = read_huffman(path)
heapify(weights)
while len(weights) > 1:
first = heappop(weights)
second = heappop(weights)
new_tree = Tree(first, second)
heappush(weights, new_tree)
assert len(weights) == 1
print(weights[0])
print(weights[0].get_max_depth())
print(weights[0].get_min_depth())
def read_huffman(path):
file_data = read_lines_from_file(path)
no_symbols = int(file_data[0])
del file_data[0]
weights = []
for line in file_data:
weights.append(Leaf(int(line.strip())))
return no_symbols, weights
def read_lines_from_file(path):
lst = []
for line in open(path):
lst.append(line.rstrip('\n'))
return lst
if __name__ == "__main__":
run_huffman("huffman.txt")
# ex1 19
# ex2 9