# HG changeset patch
# User Viviane Pons
# Date 1361019792 18000
# Node ID 6b4d1ec8385fedcadc19ee66db76131e1dec06a0
# Parent 858bf0f51b5bb6fd1cf19fe3a304007318408c53
[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,18 @@ 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")
+ children = 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)