view scripts/topology/tree.rb @ 11:26e1da66ffe1

add script
author e095732
date Tue, 29 Jan 2013 15:04:13 +0900
parents
children
line wrap: on
line source

def create_nodes(node_num)
  (0..node_num - 1).map { |i|
    i = "node" + i.to_s
  }
end

def print_dot(connections)
  puts "digraph test {"
  connections.each { |connection|
    print "\t"
    print connection[0]
    print " -> "
    print connection[1]
    print ' [label="' + connection[2] + '"]'
    puts
  }
  puts "}"
end

node_num = ARGV[0].to_i
nodes = create_nodes(node_num)
connections = Array.new
nodes.each_with_index { |node, i|
  parent = (i - 1) / 2;
  child1 = 2 * i + 1;
  child2 = 2 * i + 2;
  if parent >= 0 then
    connections << [nodes[i], nodes[parent], "parent"]
  end
  if child1 < node_num then
    connections << [nodes[i], nodes[child1], "child1"]
  end
  if child2 < node_num then
    connections << [nodes[i], nodes[child2], "child2"]
  end
}
print_dot(connections)