class Node
Attributes
left[RW]
order[RW]
right[RW]
value[RW]
weight[RW]
Public Class Methods
new(value, weight, order=0, left=nil, right=nil)
click to toggle source
# File lib/huffman.rb, line 3 def initialize(value, weight, order=0, left=nil, right=nil) @value = value @weight = weight @left = left @right = right @order = order #actuality of the node end
Public Instance Methods
<=>(other)
click to toggle source
the sorting order should be defined by weight and order of the internal node for obtaining Huffman
code with minimum variance the most recent internal node should be later in the list
# File lib/huffman.rb, line 22 def <=>(other) if @weight < other.weight -1 elsif @weight > other.weight 1 else if @order < other.order return -1 elsif @order > other.order return 1 end 0 end end
leaf?()
click to toggle source
# File lib/huffman.rb, line 37 def leaf? @left.nil? && @right.nil? end
traverse(code, hash)
click to toggle source
# File lib/huffman.rb, line 11 def traverse(code, hash) if leaf? hash[@value] = code else @left.traverse(code + '1', hash) @right.traverse(code + '0', hash) end end