/
Node.py
55 lines (45 loc) · 1.44 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
'''
A node in a Huffmans tree
'''
class Node:
'''
data represents:
leftChild - the left child of this Node
rightChild - the right child of this Node
ch - the character
frequency - the frequency of the character
NOTE - ch must be set to None for non-leaf nodes
'''
def __init__(self, frequency, ch, leftChild = None, rightChild = None):
self.leftChild = leftChild
self.rightChild = rightChild
self.frequency = frequency
self.ch = ch
self.level = 0
while leftChild:
if leftChild.getLeftChild() is None:
break
else:
leftChild = leftChild.getLeftChild()
self.level += 1
# returns the frequency count of this node
def getFrequency(self):
return self.frequency
# returns the character of this node
def getCh(self):
return self.ch
# returns the left child of this node
def getLeftChild(self):
return self.leftChild
# returns the right child of this node
def getRightChild(self):
return self.rightChild
# returns whether this node is a leaf node or not
def isLeafNode(self):
return self.leftChild == None and self.rightChild == None
# sets the level of this node
def setLevel(self,level):
self.level = level
# returns the level of this node
def getLevel(self):
return self.level