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.Fri, 13 Jan 2023 02:15:12 +0100Why are the binary trees immutable?https://ask.sagemath.org/question/65894/why-are-the-binary-trees-immutable/The class BinaryTree is immutable. Is there an advantage to this? mrutkuokurFri, 13 Jan 2023 02:15:12 +0100https://ask.sagemath.org/question/65894/Obtaining all posets from binary trees up to isomorphismhttps://ask.sagemath.org/question/54030/obtaining-all-posets-from-binary-trees-up-to-isomorphism/One can obtain all posets of binary trees for a given n as follows in Sage:
n=4
posets = [bt.to_poset() for bt in BinaryTrees(n)]
Is there a quick method to obtain the list of all such posets up to isomorphism?klaaaMon, 26 Oct 2020 00:43:45 +0100https://ask.sagemath.org/question/54030/Insert children of a specified node in a binary tree and retrieve all leaves of a binary tree.https://ask.sagemath.org/question/37512/insert-children-of-a-specified-node-in-a-binary-tree-and-retrieve-all-leaves-of-a-binary-tree/ I am trying to use a binary tree to store data. When different cases happens at some step, I will need to create two children from the current node.
I also need to collect all information on the leaves (nodes with no children). I use the following definition of binary trees:
from sage.misc.binary_tree import BinaryTree
T = BinaryTree();
I can insert node the following way:
t.insert(0,[1,2,4]);
t.insert(1,[4,5]);
t.insert(2,[5,0]);
But I couldn't find a way to visualize the tree. Also I don't know how to add two children of one certain node. For example, if `0` is the root (it seems so), `1` is its left child and `2` is its right child (which I am not sure), how do I make sure I add two children for the node `2`?
I also looked at this page: (http://doc.sagemath.org/html/en/reference/combinat/sage/combinat/binary_tree.html). I couldn't see how to insert a node with values in it, or how to retrieve information of leaves.
Do I have to define my own tree structure?
Thank you for your help!KittyLThu, 04 May 2017 21:22:32 +0200https://ask.sagemath.org/question/37512/Plotting LabelledOrderedTrees in Knuth stylehttps://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?
Peter LuschnyMon, 29 Aug 2016 15:04:40 +0200https://ask.sagemath.org/question/34641/Generating a LabelledBinaryTree from its string representationhttps://ask.sagemath.org/question/34614/generating-a-labelledbinarytree-from-its-string-representation/Consider
t = Permutation([1, 3, 2]).increasing_tree()
print t
print t.parent()
This gives
1[., 2[3[., .], .]]
Labelled binary trees
Conversely, I would like to generate a labelled binary tree from its string representation.
s = "1[., 2[3[., .], .]]"
LabelledBinaryTree(s)
This gives
ValueError: malformed string
How do I have to proceed?
*EDIT:*
I will turn now my question into a feature request:
Make all trees reconstructable by their string representation!
This would be a great help for developing and testing one's code.
Below Maybeso83 demonstrates how this can be done for
labelled binary trees.Peter LuschnySat, 27 Aug 2016 14:03:11 +0200https://ask.sagemath.org/question/34614/Rotating binary trees gives unexpected errorhttps://ask.sagemath.org/question/34589/rotating-binary-trees-gives-unexpected-error/ Hi all,
when I try
for d in DyckWords(3):
print "--------------"
t = d.to_binary_tree()
s = t.left_rotate()
print ascii_art(t)
print ascii_art(s)
I get "IndexError: list index out of range".
What am I doing wrong?Peter LuschnyFri, 26 Aug 2016 10:26:24 +0200https://ask.sagemath.org/question/34589/