annotate scripts/tree.rb @ 8:8a33595a0d8c

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