# HG changeset patch
# User Viviane Pons
# Date 1361019792 18000
# Node ID dfe3e0822d6202cc06a8225b7d023a7cd5631d69
# Parent 21586b8448e381f02c844e91d42d5b472ff0dacd
[mq]: trac_8703-additional-feature-vp.patch
#8703 Additional feature :
- added the construction of a tree from a string
for both binary and ordered trees
diff --git a/sage/combinat/binary_tree.py b/sage/combinat/binary_tree.py
--- a/sage/combinat/binary_tree.py
+++ b/sage/combinat/binary_tree.py
@@ -116,7 +116,9 @@ class BinaryTree(AbstractClonableTree, C
INPUT:
- ``children`` -- ``None`` (default) or a list, tuple or iterable of
- length 2 of binary trees or convertible objects.
+ length 2 of binary trees or convertible objects. Alternatively a string
+ is also accepted. The syntax is the same as for printing: empty trees
+ are denoted by `.` and children are grouped by square brackets.
- ``check`` -- (default to ``True``) whether check for binary should be
performed or not.
@@ -135,6 +137,9 @@ class BinaryTree(AbstractClonableTree, C
[., [., .]]
sage: BinaryTree([[], None])
[[., .], .]
+ sage: BinaryTree("[[], .]")
+ [[., .], .]
+
sage: BinaryTree([[], None, []])
Traceback (most recent call last):
...
@@ -205,7 +210,19 @@ class BinaryTree(AbstractClonableTree, C
sage: BinaryTree([None, None]).parent()
Binary trees
+ sage: BinaryTree("[., [., [., .]]]")
+ [., [., [., .]]]
+ sage: BinaryTree("[.,.,.]")
+ Traceback (most recent call last):
+ ...
+ AssertionError: This is not a binary tree
+ sage: all(BinaryTree(repr(bt)) == bt for i in range(6) for bt in BinaryTrees(i))
+ True
"""
+ if(type(children) is str):
+ children = children.replace(".","None")
+ import ast
+ children = ast.literal_eval(children)
if children is None:
children = []
elif children == [] or isinstance(children, (Integer, int)):
diff --git a/sage/combinat/ordered_tree.py b/sage/combinat/ordered_tree.py
--- a/sage/combinat/ordered_tree.py
+++ b/sage/combinat/ordered_tree.py
@@ -31,8 +31,12 @@ class OrderedTree(AbstractClonableTree,
children of a node which is given by the the order of the element in the
list. Note that there is no empty ordered tree.
+ INPUT:
+
One can create a tree from any list (or more generally iterable) of trees
- or objects convertible to a tree.
+ or objects convertible to a tree. Alternatively a string is also
+ accepted. The syntax is the same as for printing: children are grouped by
+ square brackets.
EXAMPLES::
@@ -224,7 +228,13 @@ class OrderedTree(AbstractClonableTree,
sage: t1 = OrderedTrees(4)([[],[[]]])
sage: TestSuite(t1).run()
+ sage: OrderedTrees()("[]") # indirect doctest
+ []
+ sage: all(OrderedTree(repr(tr)) == tr for i in range(6) for tr in OrderedTrees(i))
+ True
"""
+ if type(children) is str:
+ children = eval(children)
if (children.__class__ is self.__class__ and
children.parent() == parent):
children = list(children)