Last active
September 29, 2020 09:32
-
-
Save jonelf/639589 to your computer and use it in GitHub Desktop.
Binary tree with some boundary rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'graphviz' | |
@min_level = 1 | |
@max_level = 12 | |
@max_depth = 10 | |
start_level = 6 | |
@g = GraphViz.new(:G, :type => "strict digraph" ) | |
def add_node(level, depth, parent) | |
if depth < @max_depth | |
current = [level, depth].join(",") | |
sub = level <=> @min_level | |
add = @max_level <=> level | |
add_node(level - sub, depth + 1, current) | |
add_node(level + add, depth + 1, current) | |
@g.add_node(current).label = level.to_s | |
@g.add_edge(parent, current) unless parent == "00" | |
end | |
end | |
add_node(start_level, 0, "00") | |
@g.output( :png => "/www/pages/g.png" ) # http://plea.se/g.png |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment