class HuffmanTree::Node

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.

Attributes

height[R]

Height of the bounding box of the tree in graphics.

offset[R]

Offset of node from edge of the bounding box in graphics.

weight[R]

The total weight of all leaves of the tree.

width[R]

Width of the bounding box of the tree in graphics.

Public Class Methods

new(weight, width, height, offset) click to toggle source

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

Public Instance Methods

html() click to toggle source

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
leaf?() click to toggle source

Is this Node an instance of subclass Leaf?

# File huffman_tree.rb, line 30
def leaf?
    self.is_a? Leaf
end