ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 02 Mar 2015 23:38:56 +0100Use a Binary Treehttps://ask.sagemath.org/question/25973/use-a-binary-tree/ Hi folks,
newbie alert.
I want to have a binary tree with a single integer value at each node. I'm looking at BinaryTree() but this seems to have the structure of a tree but without values!!!
Please point me in the right direction. I don't mind creating a class from scratch if I can just make a start.
Thanks
JoeSat, 28 Feb 2015 20:27:34 +0100https://ask.sagemath.org/question/25973/use-a-binary-tree/Answer by vdelecroix for <p>Hi folks,</p>
<p>newbie alert.</p>
<p>I want to have a binary tree with a single integer value at each node. I'm looking at BinaryTree() but this seems to have the structure of a tree but without values!!!</p>
<p>Please point me in the right direction. I don't mind creating a class from scratch if I can just make a start.</p>
<p>Thanks
Joe</p>
https://ask.sagemath.org/question/25973/use-a-binary-tree/?answer=25979#post-id-25979You should have a look at **LabelledBinaryTree**
sage: t1 = LabelledBinaryTree([None, None], label=3)
sage: t2 = LabelledBinaryTree([None, None], label=4)
sage: t = LabelledBinaryTree([t1,t2], label=-2)
sage: t
-2[3[., .], 4[., .]]
And easier to read
sage: ascii_art(t)
-2
/ \
3 4
Sun, 01 Mar 2015 00:19:01 +0100https://ask.sagemath.org/question/25973/use-a-binary-tree/?answer=25979#post-id-25979Comment by Joe for <p>You should have a look at <strong>LabelledBinaryTree</strong> </p>
<pre><code>sage: t1 = LabelledBinaryTree([None, None], label=3)
sage: t2 = LabelledBinaryTree([None, None], label=4)
sage: t = LabelledBinaryTree([t1,t2], label=-2)
sage: t
-2[3[., .], 4[., .]]
</code></pre>
<p>And easier to read</p>
<pre><code>sage: ascii_art(t)
-2
/ \
3 4
</code></pre>
https://ask.sagemath.org/question/25973/use-a-binary-tree/?comment=26005#post-id-26005Perfect - just what I was looking for.
Thank you for your help and example.Mon, 02 Mar 2015 23:38:56 +0100https://ask.sagemath.org/question/25973/use-a-binary-tree/?comment=26005#post-id-26005