We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Root is stored and never changes, so you can go back there after you reach a leaf. The variable "temp" stores the current position as you traverse down towards a leaf.
Once you reach a leaf, identified by the lack of left/right links, you retrieve the data and add it to your string. Then you need to reset your position to the root node, so you set temp=root.
temp here is obviously a poor variable name choice. The variable head should be left untouched and a new variable, forexample current_node or just node should be used to traverse the tree down.
defis_last(node):returnnotnode.leftandnotnode.rightdefgo_right(node):returnnode.rightdefgo_left(node):returnnode.left# Enter your code here. Read input from STDIN. Print output to STDOUTdefdecodeHuff(root,s):result=[]node=rootforcharins:ifchar=='1':node=go_right(node)elifchar=='0':node=go_left(node)ifis_last(node):result.append(node.data)node=rootprint''.join(result)
I don't see how exactly your solution is different. Additionaly, temp has a local scope and leaves the root un bothered in the original solution. I can't really see how your solution improved the former
I know this is old but...
Python is all about clarity (arguably, all languages should be, but python in particular).
We might discuss about the need to create functions just to move along the tree, but variable names are not something that can just be overlooked.
Tree: Huffman Decoding
You are viewing a single comment's thread. Return to all comments →
Not my code but I did something similar.
Root is stored and never changes, so you can go back there after you reach a leaf. The variable "temp" stores the current position as you traverse down towards a leaf.
Once you reach a leaf, identified by the lack of left/right links, you retrieve the data and add it to your string. Then you need to reset your position to the root node, so you set temp=root.
temp
here is obviously a poor variable name choice. The variablehead
should be left untouched and a new variable, forexamplecurrent_node
or justnode
should be used to traverse the tree down.I don't see how exactly your solution is different. Additionaly,
temp
has a local scope and leaves theroot
un bothered in the original solution. I can't really see how your solution improved the formerI know this is old but... Python is all about clarity (arguably, all languages should be, but python in particular). We might discuss about the need to create functions just to move along the tree, but variable names are not something that can just be overlooked.