class Kaninchen::DataStructure::Node

Attributes

type[R]
value[R]

Public Class Methods

new(params = {}) click to toggle source
# File lib/kaninchen/data_structure/node.rb, line 7
def initialize(params = {})
  if params.is_a? Hash
    @type  = params[:type]
    @value = params[:value]
  else
    @value = params
    @type  = nil
  end
end
node_type(type) click to toggle source
# File lib/kaninchen/data_structure/node.rb, line 32
def node_type(type)
  define_method "#{type}_node?" do
    @type === type
  end

  define_singleton_method "#{type}_node_attr_reader" do |*attrs|
    attrs.each do |attr|
      define_method attr do
        send("#{type}_node?") ? instance_variable_get("@#{attr}") : nil
      end
    end
  end
end

Public Instance Methods

<<(params) click to toggle source
# File lib/kaninchen/data_structure/tree.rb, line 30
def <<(params)
  case
  when (params.is_a?(Kaninchen::DataStructure::Node) and params.nil?)
    params.send(:set_tree_node_data, tree: self.tree, parent: self)
    set_tree_node_data(children: @children.push(params))
  when params.is_a?(Array) then params.each { |node| self << node }
  when params.is_a?(Hash)
    params.each_pair do |key, value|
      self << key
      key << value if key.is_a? Kaninchen::DataStructure::Node
    end
  end
  self
end
degree() click to toggle source
# File lib/kaninchen/data_structure/tree/properties.rb, line 29
def degree
  self.children.size
end
depth() click to toggle source
# File lib/kaninchen/data_structure/tree/properties.rb, line 51
def depth
  count = 0
  current_node = self
  until current_node.root?
    count += 1
    current_node = current_node.parent          
  end
  count
end
height() click to toggle source
# File lib/kaninchen/data_structure/tree/properties.rb, line 71
def height
  self.subtree.height
end
inspect()
Alias for: to_s
leaf?() click to toggle source
# File lib/kaninchen/data_structure/tree/properties.rb, line 75
def leaf?
  self.children.empty?
end
left_child() click to toggle source
# File lib/kaninchen/data_structure/tree/properties.rb, line 43
def left_child
  self.degree.zero? ? nil : self.children[0]
end
level() click to toggle source
# File lib/kaninchen/data_structure/tree/properties.rb, line 61
def level
  depth + 1
end
nil?() click to toggle source
# File lib/kaninchen/data_structure/node.rb, line 17
def nil?
  @type.nil?
end
path() click to toggle source
# File lib/kaninchen/data_structure/tree/properties.rb, line 33
def path
  node = self
  arr = [node]
  until node.root?
    node = node.parent
    arr << node
  end
  arr
end
right_child() click to toggle source
# File lib/kaninchen/data_structure/tree/properties.rb, line 47
def right_child
  self.degree < 2 ? nil : self.children[1]
end
root?() click to toggle source
# File lib/kaninchen/data_structure/tree.rb, line 45
def root?
  @parent.nil?
end
subtree() click to toggle source
# File lib/kaninchen/data_structure/tree/properties.rb, line 65
def subtree
  tree = Kaninchen::DataStructure::Tree.new(self.value)
  recursive_attach_subtree_nodes(tree.root, self)
  tree
end
to_s() click to toggle source
# File lib/kaninchen/data_structure/node.rb, line 21
def to_s
  case @type
  when nil
    "<NilNode value=\"#{value}\">"
  when :tree
    "<TreeNode value=\"#{value}\" depth=\"#{depth}\" level=\"#{level}\" degree=\"#{degree}\">"
  end
end
Also aliased as: inspect

Private Instance Methods

left_most_child(node = self) click to toggle source
# File lib/kaninchen/data_structure/tree/properties.rb, line 96
def left_most_child(node = self)
  node = node.left_child until node.left_child.nil?
  node
end
recursive_attach_subtree_nodes(new_node, origin_node) click to toggle source
# File lib/kaninchen/data_structure/tree/properties.rb, line 81
def recursive_attach_subtree_nodes(new_node, origin_node)
  origin_node.children.each do |origin_child_node|
    new_child_node = self.class.new(origin_child_node.value)
    new_node << new_child_node
    recursive_attach_subtree_nodes(new_child_node, origin_child_node)
  end
end
right_children() click to toggle source

Right children is for the n-ary but not for the binary-tree system which will be served as private method due to the needs of the implementation of traversal

# File lib/kaninchen/data_structure/tree/properties.rb, line 92
def right_children
  self.degree < 2 ? [] : self.children[1..-1]
end
set_tree_node_data(params) click to toggle source
# File lib/kaninchen/data_structure/tree.rb, line 51
def set_tree_node_data(params)
  @type     = :tree
  @tree     ||= params[:tree]
  @parent   ||= params[:parent]
  @children ||= params[:children] || []
end