Sha256: 0e57199da30ed2f7474717fe6370f54fef5412b78a9733bcbb9e1233a0f7b91e

Contents?: true

Size: 459 Bytes

Versions: 1

Compression:

Stored size: 459 Bytes

Contents

class LevelNodes
  attr_reader :size

  def initialize(nodes)
    @hash = Hash.new{|h, k| h[k] = [] }
    @hash[0] = nodes.sort
    @size = nodes.size
  end

  def pop_min_node
    level, nodes = @hash.min_by{|_, v| v[0] }
    node = nodes.shift
    @hash.delete(level) if nodes.size == 0
    @size -= 1
    return level, node
  end

  def push_node(level, node)
    @hash[level] << node
    @hash[level].sort!
    @size += 1
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
huffman_coding-0.0.1 lib/huffman_coding/level_nodes.rb