Ruby GraphViz Binary Tree Record

Posted by Jason M on Stack Overflow See other posts from Stack Overflow or by Jason M
Published on 2010-05-31T02:56:16Z Indexed on 2010/05/31 3:02 UTC
Read the original article Hit count: 314

Filed under:
|
|

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}"

© Stack Overflow or respective owner

Related posts about ruby

Related posts about binary-trees