Class: LevelNodes

Inherits:
Object
  • Object
show all
Defined in:
lib/huffman_coding/level_nodes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ LevelNodes

Returns a new instance of LevelNodes.



6
7
8
9
10
# File 'lib/huffman_coding/level_nodes.rb', line 6

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

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



4
5
6
# File 'lib/huffman_coding/level_nodes.rb', line 4

def size
  @size
end

Instance Method Details

#pop_min_nodeObject



12
13
14
15
16
17
18
# File 'lib/huffman_coding/level_nodes.rb', line 12

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

#push_node(level, node) ⇒ Object



20
21
22
23
24
# File 'lib/huffman_coding/level_nodes.rb', line 20

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