Ticket #8703: trac_8703-additional-feature-fh.patch

File trac_8703-additional-feature-fh.patch, 3.2 KB (added by hivert, 6 years ago)
  • sage/combinat/binary_tree.py

    # 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 
    116116    INPUT:
    117117
    118118    - ``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.
    120122
    121123    - ``check`` -- (default to ``True``) whether check for binary should be
    122124      performed or not.
    class BinaryTree(AbstractClonableTree, C 
    135137        [., [., .]]
    136138        sage: BinaryTree([[], None])
    137139        [[., .], .]
     140        sage: BinaryTree("[[], .]")
     141        [[., .], .]
     142
    138143        sage: BinaryTree([[], None, []])
    139144        Traceback (most recent call last):
    140145        ...
    class BinaryTree(AbstractClonableTree, C 
    205210
    206211            sage: BinaryTree([None, None]).parent()
    207212            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
    208221        """
     222        if(type(children) is str):
     223            children = children.replace(".","None")
     224            children = eval(children)
    209225        if children is None:
    210226            children = []
    211227        elif children == [] or isinstance(children, (Integer, int)):
  • sage/combinat/ordered_tree.py

    diff --git a/sage/combinat/ordered_tree.py b/sage/combinat/ordered_tree.py
    a b class OrderedTree(AbstractClonableTree,  
    3131    children of a node which is given by the the order of the element in the
    3232    list. Note that there is no empty ordered tree.
    3333
     34    INPUT:
     35
    3436    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.
    3640
    3741    EXAMPLES::
    3842
    class OrderedTree(AbstractClonableTree,  
    224228
    225229            sage: t1 = OrderedTrees(4)([[],[[]]])
    226230            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
    227235        """
     236        if type(children) is str:
     237            children = eval(children)
    228238        if (children.__class__ is self.__class__ and
    229239            children.parent() == parent):
    230240            children = list(children)