Class: Huffman::Node
- Inherits:
-
Object
- Object
- Huffman::Node
- Defined in:
- lib/huffman/node.rb
Instance Attribute Summary collapse
-
#binary_value ⇒ Object
Returns the value of attribute binary_value.
-
#left ⇒ Object
Returns the value of attribute left.
-
#right ⇒ Object
Returns the value of attribute right.
-
#symbol ⇒ Object
Returns the value of attribute symbol.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(value = nil, symbol = nil, left = nil, right = nil) ⇒ Node
constructor
Set the getters and setters.
- #leaf? ⇒ Boolean
-
#set_binary_values(&block) ⇒ Object
Parcours les noeuds pour leur donner leur valeur binaire de Huffman Peut invoquer une action sur chaque noeud visité.
-
#visit(order = :preorder, &block) ⇒ Object
Le parametre &block signifie qu’on peut passer en paramètre de la fonction un bloc de codes Soit en créeant un objet Proc.new avec du code, ou soit un lambda et en le passant en parametre => visit(:order) Ou encore directement un bloc comme : visit{|node| puts node.value} Ce bloc vas servir de visiteur et va permettre d’effectuer une action sur le bloc visité avec le mot clé “yield”.
-
#visit_and_map(order = :preorder, &block) ⇒ Object
Creates a new array containing the values returned by the block.
Constructor Details
#initialize(value = nil, symbol = nil, left = nil, right = nil) ⇒ Node
Set the getters and setters
6 7 8 9 10 |
# File 'lib/huffman/node.rb', line 6 def initialize(value=nil, symbol=nil, left=nil, right=nil) # The value of the node can't be nil raise StandardError.new "The value of the node cannot be nil" if not value @value, @symbol, @left, @right, @binary_value = value, symbol, left, right, '' end |
Instance Attribute Details
#binary_value ⇒ Object
Returns the value of attribute binary_value.
3 4 5 |
# File 'lib/huffman/node.rb', line 3 def binary_value @binary_value end |
#left ⇒ Object
Returns the value of attribute left.
3 4 5 |
# File 'lib/huffman/node.rb', line 3 def left @left end |
#right ⇒ Object
Returns the value of attribute right.
3 4 5 |
# File 'lib/huffman/node.rb', line 3 def right @right end |
#symbol ⇒ Object
Returns the value of attribute symbol.
3 4 5 |
# File 'lib/huffman/node.rb', line 3 def symbol @symbol end |
#value ⇒ Object
Returns the value of attribute value.
3 4 5 |
# File 'lib/huffman/node.rb', line 3 def value @value end |
Instance Method Details
#leaf? ⇒ Boolean
59 60 61 |
# File 'lib/huffman/node.rb', line 59 def leaf? (not @left and not @right) end |
#set_binary_values(&block) ⇒ Object
Parcours les noeuds pour leur donner leur valeur binaire de Huffman Peut invoquer une action sur chaque noeud visité
49 50 51 52 53 54 55 56 57 |
# File 'lib/huffman/node.rb', line 49 def set_binary_values(&block) [@left,@right].each_with_index do |node,bit_value| if node node.binary_value = @binary_value + bit_value.to_s yield node if block_given? node.set_binary_values(&block) end end end |
#visit(order = :preorder, &block) ⇒ Object
Le parametre &block signifie qu’on peut passer en paramètre de la fonction un bloc de codes Soit en créeant un objet Proc.new avec du code, ou soit un lambda et en le passant en parametre => visit(:order) Ou encore directement un bloc comme : visit{|node| puts node.value} Ce bloc vas servir de visiteur et va permettre d’effectuer une action sur le bloc visité avec le mot clé “yield”
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/huffman/node.rb', line 18 def visit(order=:preorder, &block) raise StandardError.new "Wrong order" if not [:preorder,:inorder,:postorder].include?(order) case order when :preorder yield self @left.visit(order, &block) if left @right.visit(order, &block) if right when :inorder @left.visit(order, &block) if left yield self @right.visit(order, &block) if right when :postorder @left.visit(order, &block) if left @right.visit(order, &block) if right yield self end end |
#visit_and_map(order = :preorder, &block) ⇒ Object
Creates a new array containing the values returned by the block.
40 41 42 43 44 |
# File 'lib/huffman/node.rb', line 40 def visit_and_map(order=:preorder, &block) array = [] visit(order){|node| array << yield(node)} array end |