# HG changeset patch
# User Viviane Pons <pons at univ-mlv.fr>
# 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
|
b
|
class BinaryTree(AbstractClonableTree, C |
116 | 116 | INPUT: |
117 | 117 | |
118 | 118 | - ``children`` -- ``None`` (default) or a list, tuple or iterable of |
119 | | length 2 of binary trees or convertible objects. |
| 119 | length 2 of binary trees or convertible objects. Alternatively a string |
| 120 | is also accepted. The syntax is the same as for printing: empty trees |
| 121 | are denoted by `.` and children are grouped by square brackets. |
120 | 122 | |
121 | 123 | - ``check`` -- (default to ``True``) whether check for binary should be |
122 | 124 | performed or not. |
… |
… |
class BinaryTree(AbstractClonableTree, C |
135 | 137 | [., [., .]] |
136 | 138 | sage: BinaryTree([[], None]) |
137 | 139 | [[., .], .] |
| 140 | sage: BinaryTree("[[], .]") |
| 141 | [[., .], .] |
| 142 | |
138 | 143 | sage: BinaryTree([[], None, []]) |
139 | 144 | Traceback (most recent call last): |
140 | 145 | ... |
… |
… |
class BinaryTree(AbstractClonableTree, C |
205 | 210 | |
206 | 211 | sage: BinaryTree([None, None]).parent() |
207 | 212 | Binary trees |
| 213 | sage: BinaryTree("[., [., [., .]]]") |
| 214 | [., [., [., .]]] |
| 215 | sage: BinaryTree("[.,.,.]") |
| 216 | Traceback (most recent call last): |
| 217 | ... |
| 218 | AssertionError: This is not a binary tree |
| 219 | sage: all(BinaryTree(repr(bt)) == bt for i in range(6) for bt in BinaryTrees(i)) |
| 220 | True |
208 | 221 | """ |
| 222 | if(type(children) is str): |
| 223 | children = children.replace(".","None") |
| 224 | children = eval(children) |
209 | 225 | if children is None: |
210 | 226 | children = [] |
211 | 227 | elif children == [] or isinstance(children, (Integer, int)): |
diff --git a/sage/combinat/ordered_tree.py b/sage/combinat/ordered_tree.py
a
|
b
|
class OrderedTree(AbstractClonableTree, |
31 | 31 | children of a node which is given by the the order of the element in the |
32 | 32 | list. Note that there is no empty ordered tree. |
33 | 33 | |
| 34 | INPUT: |
| 35 | |
34 | 36 | One can create a tree from any list (or more generally iterable) of trees |
35 | | or objects convertible to a tree. |
| 37 | or objects convertible to a tree. Alternatively a string is also |
| 38 | accepted. The syntax is the same as for printing: children are grouped by |
| 39 | square brackets. |
36 | 40 | |
37 | 41 | EXAMPLES:: |
38 | 42 | |
… |
… |
class OrderedTree(AbstractClonableTree, |
224 | 228 | |
225 | 229 | sage: t1 = OrderedTrees(4)([[],[[]]]) |
226 | 230 | sage: TestSuite(t1).run() |
| 231 | sage: OrderedTrees()("[]") # indirect doctest |
| 232 | [] |
| 233 | sage: all(OrderedTree(repr(tr)) == tr for i in range(6) for tr in OrderedTrees(i)) |
| 234 | True |
227 | 235 | """ |
| 236 | if type(children) is str: |
| 237 | children = eval(children) |
228 | 238 | if (children.__class__ is self.__class__ and |
229 | 239 | children.parent() == parent): |
230 | 240 | children = list(children) |