-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeclasses.py
More file actions
21 lines (18 loc) · 861 Bytes
/
nodeclasses.py
File metadata and controls
21 lines (18 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# leaf node class
class Leaf:
def __init__(self, char, freq):
self.char=char
self.freq=freq
def __str__(self):
# for printing node
return f'char: \'{self.char}\' \tfrequency: {self.freq}'
# parent node class
class Parent:
def __init__(self, freq):
self.left, self.right={}, {}
self.freq=freq
def __str__(self):
# for printing node
#return f'left: {self.left if self.left!={} else "[empty]"} \tright: {self.right if self.right!={} else "[empty]"} \tfrequency: {self.freq}'
a="'"
return f'left: {"[empty]" if self.left=={} else (a+self.left.char+a) if self.left.__class__.__name__=="Leaf" else self.left.freq} \tright: {"[empty]" if self.right=={} else (a+self.right.char+a) if self.right.__class__.__name__=="Leaf" else self.right.freq} \tfrequency: {self.freq}'