Every Node is either a Leaf or a Branch.
A Node has a weight, width, height, and offset. All but the weight are used to generate an image of the tree.
Height of the bounding box of the tree in graphics.
Offset of node from edge of the bounding box in graphics.
The total weight of all leaves of the tree.
Width of the bounding box of the tree in graphics.
Called only by subclass initializers.
# File huffman_tree.rb, line 14 def initialize(weight, width, height, offset) raise ArgumentError unless weight.is_a? Numeric raise ArgumentError unless width.is_a? Numeric raise ArgumentError unless height.is_a? Numeric raise ArgumentError unless offset.is_a? Numeric raise 'Do not instantiate the base class directly' unless self.is_a? Leaf or self.is_a? Branch @weight = weight @width = width @height = height @offset = offset end
Generate JavaScript to draw link to node.
# File huffman_tree.rb, line 37 def draw_link(bits) "draw_link(#{@offset}, '#{bits}', '#{@weight}');" end
Return HTML/Javascript (as String) to draw tree (sideways).
# File huffman_tree.rb, line 44 def html if @width > 128 then y_scale = 32 font_size = 11 else y_scale = 42 font_size = 14 end x_scale = 0.62 * font_size * @height; left_margin = 0.1 right_margin = margin(@height, 1) / @height.to_f + 0.1 " <!DOCTYPE html> <html> <body> <canvas id='myCanvas' width='#{x_scale * (@height + left_margin + right_margin)}' height='#{y_scale * @width}' style='border:1px solid #d3d3d3;'> </canvas> <script> const X_SCALE = #{x_scale}; const Y_SCALE = #{y_scale}; const FONT_SIZE = #{font_size}; const FONT = '#{font_size}px \"Lucida Console\", Monaco'; #{JAVASCRIPT_DRAWING_DEFINITIONS} translate(#{left_margin}, 0); #{draw} </script> </body> </html> " end