ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 01 Sep 2016 11:47:56 -0500Plotting LabelledOrderedTrees in Knuth stylehttp://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/ Consider the tree
t = Permutation([1,3,2,6,5,7,4]).increasing_tree().as_ordered_tree()
A nice simple plot is given by
ascii_art(t)
_________1___________
/ /
None ___________2_____________
/ /
_3___ ______4_______
/ / / /
None None ____5______ None
/ /
_6___ _7___
/ / / /
None None None None
What I need is a graph-plot in exactly this layout in Knuth style
(cf. the tables in 7.2.1.6) where the inner nodes (above labeled
by numbers) are filled black disks and the leaves (above labeld
by 'None') are empty squares.
How can I achieve this?
Mon, 29 Aug 2016 08:04:40 -0500http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/Comment by FrédéricC for <p>Consider the tree</p>
<pre><code>t = Permutation([1,3,2,6,5,7,4]).increasing_tree().as_ordered_tree()
</code></pre>
<p>A nice simple plot is given by </p>
<pre><code>ascii_art(t)
_________1___________
/ /
None ___________2_____________
/ /
_3___ ______4_______
/ / / /
None None ____5______ None
/ /
_6___ _7___
/ / / /
None None None None
</code></pre>
<p>What I need is a graph-plot in exactly this layout in Knuth style
(cf. the tables in 7.2.1.6) where the inner nodes (above labeled
by numbers) are filled black disks and the leaves (above labeld
by 'None') are empty squares.</p>
<p>How can I achieve this?</p>
http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/?comment=34682#post-id-34682Maybe you could use "t.map_labels(lambda x: 0 if x in ZZ else 1)" to avoid part of the post-processing.Thu, 01 Sep 2016 11:47:56 -0500http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/?comment=34682#post-id-34682Comment by FrédéricC for <p>Consider the tree</p>
<pre><code>t = Permutation([1,3,2,6,5,7,4]).increasing_tree().as_ordered_tree()
</code></pre>
<p>A nice simple plot is given by </p>
<pre><code>ascii_art(t)
_________1___________
/ /
None ___________2_____________
/ /
_3___ ______4_______
/ / / /
None None ____5______ None
/ /
_6___ _7___
/ / / /
None None None None
</code></pre>
<p>What I need is a graph-plot in exactly this layout in Knuth style
(cf. the tables in 7.2.1.6) where the inner nodes (above labeled
by numbers) are filled black disks and the leaves (above labeld
by 'None') are empty squares.</p>
<p>How can I achieve this?</p>
http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/?comment=34668#post-id-34668not yet implemented for binary trees, indeedWed, 31 Aug 2016 12:06:07 -0500http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/?comment=34668#post-id-34668Comment by Peter Luschny for <p>Consider the tree</p>
<pre><code>t = Permutation([1,3,2,6,5,7,4]).increasing_tree().as_ordered_tree()
</code></pre>
<p>A nice simple plot is given by </p>
<pre><code>ascii_art(t)
_________1___________
/ /
None ___________2_____________
/ /
_3___ ______4_______
/ / / /
None None ____5______ None
/ /
_6___ _7___
/ / / /
None None None None
</code></pre>
<p>What I need is a graph-plot in exactly this layout in Knuth style
(cf. the tables in 7.2.1.6) where the inner nodes (above labeled
by numbers) are filled black disks and the leaves (above labeld
by 'None') are empty squares.</p>
<p>How can I achieve this?</p>
http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/?comment=34656#post-id-34656Ah, I see now, it works with 7.3 (but did not with 7.2). But it does not work with LabelledBinaryTrees. With t = Permutation([1,3,2,6,5,7,4]).increasing_tree() I get the AttributeError 'UnicodeArt' object has no attribute '_root'.Tue, 30 Aug 2016 07:38:45 -0500http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/?comment=34656#post-id-34656Comment by Peter Luschny for <p>Consider the tree</p>
<pre><code>t = Permutation([1,3,2,6,5,7,4]).increasing_tree().as_ordered_tree()
</code></pre>
<p>A nice simple plot is given by </p>
<pre><code>ascii_art(t)
_________1___________
/ /
None ___________2_____________
/ /
_3___ ______4_______
/ / / /
None None ____5______ None
/ /
_6___ _7___
/ / / /
None None None None
</code></pre>
<p>What I need is a graph-plot in exactly this layout in Knuth style
(cf. the tables in 7.2.1.6) where the inner nodes (above labeled
by numbers) are filled black disks and the leaves (above labeld
by 'None') are empty squares.</p>
<p>How can I achieve this?</p>
http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/?comment=34648#post-id-34648Frédéric, in my jupyter notebook I see no difference although I expected one after looking into abstract_tree.py. What are the requirements?Tue, 30 Aug 2016 01:01:51 -0500http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/?comment=34648#post-id-34648Comment by FrédéricC for <p>Consider the tree</p>
<pre><code>t = Permutation([1,3,2,6,5,7,4]).increasing_tree().as_ordered_tree()
</code></pre>
<p>A nice simple plot is given by </p>
<pre><code>ascii_art(t)
_________1___________
/ /
None ___________2_____________
/ /
_3___ ______4_______
/ / / /
None None ____5______ None
/ /
_6___ _7___
/ / / /
None None None None
</code></pre>
<p>What I need is a graph-plot in exactly this layout in Knuth style
(cf. the tables in 7.2.1.6) where the inner nodes (above labeled
by numbers) are filled black disks and the leaves (above labeld
by 'None') are empty squares.</p>
<p>How can I achieve this?</p>
http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/?comment=34644#post-id-34644There is now also "unicode_art" (not answering the question).Mon, 29 Aug 2016 11:39:14 -0500http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/?comment=34644#post-id-34644Answer by Peter Luschny for <p>Consider the tree</p>
<pre><code>t = Permutation([1,3,2,6,5,7,4]).increasing_tree().as_ordered_tree()
</code></pre>
<p>A nice simple plot is given by </p>
<pre><code>ascii_art(t)
_________1___________
/ /
None ___________2_____________
/ /
_3___ ______4_______
/ / / /
None None ____5______ None
/ /
_6___ _7___
/ / / /
None None None None
</code></pre>
<p>What I need is a graph-plot in exactly this layout in Knuth style
(cf. the tables in 7.2.1.6) where the inner nodes (above labeled
by numbers) are filled black disks and the leaves (above labeld
by 'None') are empty squares.</p>
<p>How can I achieve this?</p>
http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/?answer=34651#post-id-34651This is not an answer but a workaround in the case that one
wishes a standalone picture.
Instead of ascii_art(t) execute pretty_print(t).
Then edit the TeX code like this:
* Replace commands of the type
\newcommand{\nodec}{\node[draw,circle] (c) {\$None\$} by
\newcommand{\nodeb}{\node[draw,rectangle] (c) {$$}
* Replace commands of the type
\newcommand{\nodei}{\node[draw,circle] (i) {\$6\$} by
\newcommand{\nodei}{\node[fill=black,draw,circle] (i) {$$}
* Replace
\matrix[column sep=.3cm, row sep=.3cm,ampersand replacement=\&] by
\matrix[row sep=0.7cm,ampersand replacement=\&]
* Replace
\path[ultra thick, red] by
\path[black]
This will replace the tree in Sage style
![image description](/upfiles/1472541784426767.png)
by a tree in Knuth style:
![image description](/upfiles/14725418031231103.png)
I really would like to see this as the default style for binary trees.Tue, 30 Aug 2016 02:24:55 -0500http://ask.sagemath.org/question/34641/plotting-labelledorderedtrees-in-knuth-style/?answer=34651#post-id-34651