Ruby GraphViz Binary Tree Record
- by Jason M
I'm using the ruby-graphviz gem and I'm trying to draw binary trees. I'd like to use the record shape so that each node can have a left, middle, and right field and, thus, if there are two edges leaving a node, the left and right edges can be distinguished.
I tried specifying the field by concatenating the field name like this: @node1.name + ":left" But that did not work. What is the correct way of specifying the field?
require 'rubygems'
require 'graphviz'
@graph = GraphViz.new( :G, :type => :digraph )
@node1 = @graph.add_node("1",
"shape" => "record",
"label" => "<left>|<f1> 1|<right>" )
@node2 = @graph.add_node("2",
"shape" => "record",
"label" => "<left>|<f1> 2|<right>" )
@graph.add_edge(@node1.name + ":left", @node2)
# generate a random filename
filename = "/tmp/#{(0...8).map{65.+(rand(25)).chr}.join}.png"
@graph.output( :png => filename )
exec "open #{filename}"