comparison scripts/tree.rb @ 8:8a33595a0d8c

add tree.rb
author suruga
date Wed, 07 Feb 2018 19:31:33 +0900
parents
children
comparison
equal deleted inserted replaced
7:3fda0de2d139 8:8a33595a0d8c
1 def create_nodes(node_num)
2 (0..node_num - 1).map { |i|
3 i = "node" + i.to_s
4 }
5 end
6
7 def print_dot(connections)
8 puts "digraph test {"
9 connections.each { |connection|
10 print "\t"
11 print connection[0]
12 print " -> "
13 print connection[1]
14 print ' [label="' + connection[2] + '"]'
15 puts
16 }
17 puts "}"
18 end
19
20 node_num = ARGV[0].to_i
21 nodes = create_nodes(node_num)
22 connections = Array.new
23 nodes.each_with_index { |node, i|
24 parent = (i - 1) / 2;
25 child1 = 2 * i + 1;
26 child2 = 2 * i + 2;
27 if parent >= 0 then
28 connections << [nodes[i], nodes[parent], "parent"]
29 end
30 if child1 < node_num then
31 connections << [nodes[i], nodes[child1], "child1"]
32 end
33 if child2 < node_num then
34 connections << [nodes[i], nodes[child2], "child2"]
35 end
36 }
37 print_dot(connections)
38