from arrayheap import ArrayHeap
def generateTable(self, node, table):
def codeTable(node, codeString):
if node.isLeaf:
table[node._char] = codeString
return
if node.getleft() is not None:
table(node.getLeft(), codeString + '0')
if node.getRight() is not None:
table(node.getRight(), codeString + '1')
codeTable(node, '')
return(table)
def main():
with open('codetable.txt', 'w') as cdt:
codeTable = {}
codeTable = HTree.generateTable(HTree, codeTable)
for i in sorted(codeTable):
cdt.write(str(i) + '\t' + str(codeTable[i]) + '\n')
main()
I am trying to output to a file the binary traversal to each individual node in a tree. I have left the preorder and postorder traversal methods in the file for reference. My question is what is wrong with my generateTable() method (most of the code was provided to me) I would like to output it so that it lists the traversal, 0 being left 1 being right, in the format <ascii value> : <binary path>
. The thing is, when I run this code, my output file codetable.txt is empty. What am I doing wrong?