Ticket #14731: trac_14731-binary-trees-to-permutations.patch

File trac_14731-binary-trees-to-permutations.patch, 4.2 KB (added by VivianePons, 6 years ago)
  • sage/combinat/binary_tree.py

    # HG changeset patch
    # User Viviane Pons <vivianepons at gmail.com>
    # Date 1371052018 -7200
    # Node ID b9d423ce251f4641326a998765537d32139e1bb9
    # Parent  41dc79ef7d27bdc6bba31ccec8fcea8a9dfbda42
    #14731 Adding maps from binary trees to permutations
    
    diff --git a/sage/combinat/binary_tree.py b/sage/combinat/binary_tree.py
    a b class BinaryTree(AbstractClonableTree, C 
    532532            [[[[]], [[]]], [[]], []]
    533533        """
    534534        return self._to_ordered_tree(bijection="right")
    535    
     535
     536    def _postfix_word(self, left_first = True, start = 1):
     537        r"""
     538        Internal recursive method to obtain a postfix canonical read of the
     539        binary tree.
     540
     541        EXAMPLES::
     542
     543            sage: bt = BinaryTree([[],[]])                     
     544            sage: bt._postfix_word()               
     545            [1, 3, 2]
     546            sage: bt._postfix_word(left_first=False)
     547            [3, 1, 2]
     548            sage: bt = BinaryTree([[[], [[], None]], [[], []]])
     549            sage: bt._postfix_word()               
     550            [1, 3, 4, 2, 6, 8, 7, 5]
     551            sage: bt._postfix_word(left_first=False)
     552            [8, 6, 7, 3, 4, 1, 2, 5]
     553        """
     554        if not self:
     555            return []
     556        left = self[0]._postfix_word(left_first, start)
     557        label = start + self[0].node_number()
     558        right = self[1]._postfix_word(left_first, start = label +1)
     559        if left_first:
     560            left.extend(right)
     561            left.append(label)
     562            return left
     563        else:
     564            right.extend(left)
     565            right.append(label)
     566            return right
     567
     568    @combinatorial_map(name="To 312 avoiding permutation")
     569    def to_312_avoiding_permutation(self):
     570        r"""
     571        Return a 312-avoiding permutation corresponding to the binary tree.
     572
     573        The linear extensions of a binary tree form an interval of the weak
     574        order called the sylester class of the tree. This permutation is
     575        the minimal element of this sylvester class.
     576
     577        EXAMPLES::
     578
     579            sage: bt = BinaryTree([[],[]])                     
     580            sage: bt.to_312_avoiding_permutation()
     581            [1, 3, 2]
     582            sage: bt = BinaryTree([[[], [[], None]], [[], []]])
     583            sage: bt.to_312_avoiding_permutation()
     584            [1, 3, 4, 2, 6, 8, 7, 5]
     585
     586        TESTS::
     587
     588            sage: bt = BinaryTree([[],[]])                     
     589            sage: bt == bt.to_312_avoiding_permutation().binary_search_tree_shape(left_to_right=False)
     590            True
     591            sage: bt = BinaryTree([[[], [[], None]], [[], []]])
     592            sage: bt == bt.to_312_avoiding_permutation().binary_search_tree_shape(left_to_right=False)
     593            True
     594        """
     595        from sage.combinat.permutation import Permutation
     596        return Permutation(self._postfix_word())
     597
     598
     599    @combinatorial_map(name="To 132 avoiding permutation")
     600    def to_132_avoiding_permutation(self):
     601        r"""
     602        Return a 132-avoiding permutation corresponding to the binary tree.
     603
     604        The linear extensions of a binary tree form an interval of the weak
     605        order called the sylester class of the tree. This permutation is
     606        the maximal element of this sylvester class.
     607
     608        EXAMPLES::
     609
     610            sage: bt = BinaryTree([[],[]])
     611            sage: bt.to_132_avoiding_permutation()
     612            [3, 1, 2]
     613            sage: bt = BinaryTree([[[], [[], None]], [[], []]])
     614            sage: bt.to_132_avoiding_permutation()
     615            [8, 6, 7, 3, 4, 1, 2, 5]
     616
     617        TESTS::
     618
     619            sage: bt = BinaryTree([[],[]])
     620            sage: bt == bt.to_132_avoiding_permutation().binary_search_tree_shape(left_to_right=False)
     621            True
     622            sage: bt = BinaryTree([[[], [[], None]], [[], []]])
     623            sage: bt == bt.to_132_avoiding_permutation().binary_search_tree_shape(left_to_right=False)
     624            True
     625        """
     626        from sage.combinat.permutation import Permutation
     627        return Permutation(self._postfix_word(left_first=False))
     628
    536629    @combinatorial_map(order = 2, name="Left-right symmetry")
    537630    def left_right_symmetry(self):
    538631        r"""