-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.py
91 lines (87 loc) · 2.47 KB
/
Node.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
#Return sorted list of letters based on occurence
def sortedL(s):
letters={}
for i in s:
if i not in letters.keys():
letters[i]=s.count(i)
else: continue
sortedL = sorted(letters.items(), key=lambda x: x[1], reverse=True)
return sortedL
#Calculate the sum of values
def calc_sum(l):
s=[m[1] for m in l]
return sum(s)
# split the lists
def new_list(l):
Lc=[]
Rc=[]
count=0
i=0
if len(l)==2:
Lc.append(l[0])
Rc.append(l[1])
else:
while l[i][1]+count<=calc_sum(l)//2:
# print('index: ',i)
count+=l[i][1]
i+=1
Lc=l[:i]
Rc=l[i:]
return Lc,Rc
# Function to print leaf
# nodes from left to right
Leaf_nodes={}
def printLeafNodes(root)-> None :
global Leaf_nodes
# If node is null, return
if (not root):
return
# If node is leaf node,
# print its data
if (not root.left and
not root.right):
# print(f'character: {root.data[0][0]}',f'Occurence: {root.data[0][1]} times', f'code:{root.code}' )
Leaf_nodes[root.data[0][0]]=root.code
return
# If left child exists
# check for leaf recursively
if root.left:
printLeafNodes(root.left)
# If right child exists,
# check for leaf recursively
if root.right:
printLeafNodes(root.right)
return Leaf_nodes
#insert into the coding tree
def FillCodeTree(root,list):
if len(list) == 1:
root.data=list
else:
LL=new_list(list)[0]
# print(LL)
RL=new_list(list)[1]
# print(RL)
root.data=list
root.left=Node(LL)
root.left.code=root.code+'0'
root.right=Node(RL)
root.right.code=root.code+'1'
FillCodeTree(root.left,root.left.data)
FillCodeTree(root.right,root.right.data)
#Code the word
def channon_fano(word,dic):
s=''
for i in word:
s+=dic[i]+' '
return s
# node class
class Node:
def __init__(self, data):
# left child
self.left = None
# right child
self.right = None
# node's value
self.data = data
#charecter Code
self.code=""