<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Visualizing binary trees with Graphviz</title>
	<atom:link href="http://eli.thegreenplace.net/2009/11/23/visualizing-binary-trees-with-graphviz/feed/" rel="self" type="application/rss+xml" />
	<link>http://eli.thegreenplace.net/2009/11/23/visualizing-binary-trees-with-graphviz/</link>
	<description>Eli Bendersky's personal website</description>
	<lastBuildDate>Wed, 10 Mar 2010 17:32:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Tom Lynn</title>
		<link>http://eli.thegreenplace.net/2009/11/23/visualizing-binary-trees-with-graphviz/comment-page-1/#comment-248719</link>
		<dc:creator>Tom Lynn</dc:creator>
		<pubDate>Mon, 11 Jan 2010 19:31:29 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1991#comment-248719</guid>
		<description>Here&#039;s a translation into Python:

`&lt;code&gt;def bst_to_dot(tree, stream,
               get_key=lambda node: node__abENT__#46;key,
               get_left=lambda node: node__abENT__#46;left,
               get_right=lambda node: node__abENT__#46;right,
               nodestyle=__abENT__apos;fontname=__abENT__quot;Arial__abENT__quot;__abENT__apos;):
    __abENT__quot;__abENT__quot;__abENT__quot;Prints a GraphViz __abENT__#46;dot file of tree to stream__abENT__#46;
       get_left(node) and get_right(node) should return the appropriate
       child of node, or None if there is no such child__abENT__#46;
       get_key(node) should return a str()-able object to use as both the
       node name and its displayed value (FIXME: this is not a great idea)__abENT__#46;
    __abENT__quot;__abENT__quot;__abENT__quot;
    def escape(text):
        return __abENT__apos;__abENT__quot;%s__abENT__quot;__abENT__apos; % text  # TODO: escaping

    nullcount = [0]
    def null_to_dot(key):
        print __abENT__gt;__abENT__gt;stream, __abENT__quot;    null%d [shape=point];__abENT__quot; % nullcount[0]
        print __abENT__gt;__abENT__gt;stream, __abENT__quot;    %s -__abENT__gt; null%d;__abENT__quot; % (escape(key), nullcount[0])
        nullcount[0] += 1

    def node_to_dot(node):
        if get_left(node) is not None:
            print __abENT__gt;__abENT__gt;stream, __abENT__quot;    %s -__abENT__gt; %s;__abENT__quot; % (
                escape(get_key(node)), escape(get_key(get_left(node))))
            node_to_dot(get_left(node))
        else:
            null_to_dot(get_key(node))
        if get_right(node) is not None:
            print __abENT__gt;__abENT__gt;stream, __abENT__quot;    %s -__abENT__gt; %s;__abENT__quot; % (
                escape(get_key(node)), escape(get_key(get_right(node))))
            node_to_dot(get_right(node))
        else:
            null_to_dot(get_key(node))

    print __abENT__gt;__abENT__gt;stream, __abENT__quot;digraph BST {__abENT__quot;
    print __abENT__gt;__abENT__gt;stream, __abENT__quot;    node [%s];__abENT__quot; % nodestyle
    if tree is None:
        print __abENT__gt;__abENT__gt;stream
    elif get_right(tree) is None and get_left(tree) is None:
        print __abENT__gt;__abENT__gt;stream, __abENT__quot;    %s;__abENT__quot; % escape(get_key(tree))
    else:
        node_to_dot(tree)
    print __abENT__gt;__abENT__gt;stream, __abENT__quot;}__abENT__quot;

def bst_to_dot_string(tree, *args, **kwargs):
    import StringIO
    stream = StringIO__abENT__#46;StringIO()
    bst_to_dot(tree, stream, *args, **kwargs)
    return stream__abENT__#46;getvalue()

def save_bst(tree, filename, *args, **kwargs):
    __abENT__apos;Saves a PNG of tree to filename by executing __abENT__quot;dot -Tpng -o__abENT__lt;filename__abENT__gt;__abENT__quot;__abENT__#46;__abENT__apos;
    import subprocess
    p = subprocess__abENT__#46;Popen([__abENT__quot;dot__abENT__quot;, __abENT__quot;-Tpng__abENT__quot;, __abENT__quot;-o__abENT__quot;+filename],
                         stdin=subprocess__abENT__#46;PIPE)
    p__abENT__#46;stdin__abENT__#46;write(bst_to_dot_string(tree, *args, **kwargs))
    p__abENT__#46;stdin__abENT__#46;close()
    if p__abENT__#46;wait() != 0:
        raise RuntimeError(__abENT__apos;Unexpected return code: %d__abENT__apos; % p__abENT__#46;returncode)

def test():
    import sys
    class Node(object):
        def __init__(self, key, left=None, right=None):
            self__abENT__#46;key, self__abENT__#46;left, self__abENT__#46;right = key, left, right
    tree = Node(15, Node(6), Node(18, Node(17)))
    try:
        filename = sys__abENT__#46;argv[1]
    except IndexError:
        sys__abENT__#46;exit(__abENT__apos;usage: %s FILENAME__abENT__#92;n__abENT__apos; % sys__abENT__#46;argv[0] +
                 __abENT__apos;Test this code - save a test PNG to FILENAME__abENT__apos;)
    save_bst(tree, filename)

if __name__==__abENT__apos;__main____abENT__apos;:
    test()&lt;/code&gt;`</description>
		<content:encoded><![CDATA[<p>Here&#8217;s a translation into Python:</p>
<div class="backtick"><pre><code>def bst_to_dot(tree, stream,
               get_key=lambda node: node&#46;key,
               get_left=lambda node: node&#46;left,
               get_right=lambda node: node&#46;right,
               nodestyle=&#039;fontname=&quot;Arial&quot;&#039;):
    &quot;&quot;&quot;Prints a GraphViz &#46;dot file of tree to stream&#46;
       get_left(node) and get_right(node) should return the appropriate
       child of node, or None if there is no such child&#46;
       get_key(node) should return a str()-able object to use as both the
       node name and its displayed value (FIXME: this is not a great idea)&#46;
    &quot;&quot;&quot;
    def escape(text):
        return &#039;&quot;%s&quot;&#039; % text  # TODO: escaping

    nullcount = [0]
    def null_to_dot(key):
        print &gt;&gt;stream, &quot;    null%d [shape=point];&quot; % nullcount[0]
        print &gt;&gt;stream, &quot;    %s -&gt; null%d;&quot; % (escape(key), nullcount[0])
        nullcount[0] += 1

    def node_to_dot(node):
        if get_left(node) is not None:
            print &gt;&gt;stream, &quot;    %s -&gt; %s;&quot; % (
                escape(get_key(node)), escape(get_key(get_left(node))))
            node_to_dot(get_left(node))
        else:
            null_to_dot(get_key(node))
        if get_right(node) is not None:
            print &gt;&gt;stream, &quot;    %s -&gt; %s;&quot; % (
                escape(get_key(node)), escape(get_key(get_right(node))))
            node_to_dot(get_right(node))
        else:
            null_to_dot(get_key(node))

    print &gt;&gt;stream, &quot;digraph BST {&quot;
    print &gt;&gt;stream, &quot;    node [%s];&quot; % nodestyle
    if tree is None:
        print &gt;&gt;stream
    elif get_right(tree) is None and get_left(tree) is None:
        print &gt;&gt;stream, &quot;    %s;&quot; % escape(get_key(tree))
    else:
        node_to_dot(tree)
    print &gt;&gt;stream, &quot;}&quot;

def bst_to_dot_string(tree, *args, **kwargs):
    import StringIO
    stream = StringIO&#46;StringIO()
    bst_to_dot(tree, stream, *args, **kwargs)
    return stream&#46;getvalue()

def save_bst(tree, filename, *args, **kwargs):
    &#039;Saves a PNG of tree to filename by executing &quot;dot -Tpng -o&lt;filename&gt;&quot;&#46;&#039;
    import subprocess
    p = subprocess&#46;Popen([&quot;dot&quot;, &quot;-Tpng&quot;, &quot;-o&quot;+filename],
                         stdin=subprocess&#46;PIPE)
    p&#46;stdin&#46;write(bst_to_dot_string(tree, *args, **kwargs))
    p&#46;stdin&#46;close()
    if p&#46;wait() != 0:
        raise RuntimeError(&#039;Unexpected return code: %d&#039; % p&#46;returncode)

def test():
    import sys
    class Node(object):
        def __init__(self, key, left=None, right=None):
            self&#46;key, self&#46;left, self&#46;right = key, left, right
    tree = Node(15, Node(6), Node(18, Node(17)))
    try:
        filename = sys&#46;argv[1]
    except IndexError:
        sys&#46;exit(&#039;usage: %s FILENAME&#92;n&#039; % sys&#46;argv[0] +
                 &#039;Test this code - save a test PNG to FILENAME&#039;)
    save_bst(tree, filename)

if __name__==&#039;__main__&#039;:
    test()</code></pre></div>
]]></content:encoded>
	</item>
</channel>
</rss>
