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.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  • Hiring developers?
  1. Practice
  2. Data Structures
  3. Trees
  4. Tree: Huffman Decoding
  5. Discussions

Tree: Huffman Decoding

  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

    You are viewing a single comment's thread. Return to all comments →

  • davebrophy 2 years ago+ 1 comment

    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.

    1|
    ParentPermalink
    • timnosov 2 years ago+ 1 comment

      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.

      def is_last(node):
          return not node.left and not node.right
      
      def go_right(node):
          return node.right
      
      def go_left(node):
          return node.left
      
      # Enter your code here. Read input from STDIN. Print output to STDOUT
      def decodeHuff(root , s):
          result = []
          node = root
          for char in s:
              if char == '1':
                  node = go_right(node)
              elif char == '0':
                  node = go_left(node)
              
              if is_last(node):
                  result.append(node.data)
                  node = root
                  
          print ''.join(result)
      
      3|
      ParentPermalink
      • uds5501 1 year ago+ 1 comment

        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

        -1|
        ParentPermalink
        • gelmilorenzo 6 months ago+ 0 comments

          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.

          0|
          ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature