Sha256: 3fec5bd742fe6fd05bccf7827040b9e4699e0d187079aff845235b2f44e5ee1f

Contents?: true

Size: 500 Bytes

Versions: 2

Compression:

Stored size: 500 Bytes

Contents

# frozen_string_literal: true

class Node
  attr_reader :weight

  def initialize(value, weight, left = nil, right = nil)
    @value = value
    @weight = weight
    @left = left
    @right = right
    @leaf = (@left == nil && @right == nil)
  end

  def traverse(code, hash)
    if @leaf
      hash[@value] = code
    else
      @left.traverse("#{code}1", hash)
      @right.traverse("#{code}0", hash)
    end
  end

  def <=>(other)
    @weight <=> other.weight
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
huffman_coding-1.1.0 lib/huffman_coding/node.rb
huffman_coding-1.0.0 lib/huffman_coding/node.rb