def _ascii_art_(self): r""" TESTS:: sage: t = OrderedTree([]) sage: ascii_art(t) o sage: t = OrderedTree([[]]) sage: aa = ascii_art(t);aa o | o sage: aa.get_baseline() 2 sage: tt1 = OrderedTree([[],[[],[],[[[[]]]]],[[[],[],[],[]]]]) sage: ascii_art(tt1) _____o_______ / / / o _o__ o / / / | o o o __o___ | / / / / o o o o o | o | o sage: ascii_art(tt1.canonical_labelling()) ______1_______ / / / 2 _3__ 10 / / / | 4 5 6 ___11____ | / / / / 7 12 13 14 15 | 8 | 9 sage: ascii_art(OrderedTree([[],[[]]])) o_ / / o o | o sage: t = OrderedTree([[[],[[[],[]]],[[]]],[[[[[],[]]]]],[[],[]]]) sage: ascii_art(t) _____o_______ / / / __o____ o o_ / / / | / / o o o o o o | | | o_ o o / / | o o o_ / / o o sage: ascii_art(t.canonical_labelling()) ______1________ / / / __2____ 10 16_ / / / | / / 3 4 8 11 17 18 | | | 5_ 9 12 / / | 6 7 13_ / / 14 15 """ node_to_str = lambda t: str(t.label()) if hasattr(t, "label") else "o" if self.is_empty(): from sage.misc.ascii_art import empty_ascii_art return empty_ascii_art from sage.misc.ascii_art import AsciiArt if len(self) == 0: t_repr = AsciiArt( [node_to_str(self)] ) t_repr._root = 1 return t_repr if len(self) == 1: repr_child = self[0]._ascii_art_() sep = AsciiArt( [" "*(repr_child._root-1)] ) t_repr = AsciiArt( [node_to_str(self)] ) t_repr._root = 1 repr_root = (sep + t_repr)*(sep + AsciiArt( ["|"] )) t_repr = repr_root * repr_child t_repr._root = repr_child._root t_repr._baseline = t_repr._h - 1 return t_repr # General case l_repr = [subtree._ascii_art_() for subtree in self] acc = l_repr.pop(0) whitesep = acc._root+1 lf_sep = " "*(acc._root+1) + "_"*(acc._l-acc._root) ls_sep = " "*(acc._root) + "/" + " "*(acc._l-acc._root) while len(l_repr) > 0: t_repr = l_repr.pop(0) acc += AsciiArt([" "]) + t_repr if len(l_repr) == 0: lf_sep += "_"*(t_repr._root+1) else: lf_sep += "_"*(t_repr._l+1) ls_sep += " "*(t_repr._root) + "/" + " "*(t_repr._l-t_repr._root) mid = whitesep + int((len(lf_sep)-whitesep)/2) node = node_to_str( self ) t_repr = AsciiArt([lf_sep[:mid-1] + node + lf_sep[mid+len(node)-1:], ls_sep]) * acc t_repr._root = mid t_repr._baseline = t_repr._h - 1 return t_repr
def _ascii_art_( self ): r""" TESTS:: sage: ascii_art(BinaryTree()) <BLANKLINE> sage: ascii_art(BinaryTree([])) o sage: for bt in BinaryTrees(3): ....: print ascii_art(bt) o \ o \ o o \ o / o o / \ o o o / o \ o o / o / o sage: ascii_art(BinaryTree([None,[]])) o \ o sage: ascii_art(BinaryTree([None,[None,[]]])) o \ o \ o sage: ascii_art(BinaryTree([None,[[],None]])) o \ o / o sage: ascii_art(BinaryTree([None,[[[],[]],[]]])) o \ _o_ / \ o o / \ o o sage: ascii_art(BinaryTree([None,[[None,[[],[]]],None]])) o \ o / o \ o / \ o o sage: ascii_art(BinaryTree([[],None])) o / o sage: ascii_art(BinaryTree([[[[],None], None],None])) o / o / o / o sage: ascii_art(BinaryTree([[[],[]],None])) o / o / \ o o sage: ascii_art(BinaryTree([[[None,[]],[[[],None],None]], None])) o / ___o___ / \ o o \ / o o / o sage: ascii_art(BinaryTree([[None,[[],[]]],None])) o / o \ o / \ o o sage: ascii_art(BinaryTree([[],[]])) o / \ o o sage: ascii_art(BinaryTree([[],[[],None]])) _o_ / \ o o / o sage: ascii_art(BinaryTree([[None,[]],[[[],None],None]])) ___o___ / \ o o \ / o o / o sage: ascii_art(BinaryTree([[[],[]],[[],None]])) __o__ / \ o o / \ / o o o sage: ascii_art(BinaryTree([[[],[]],[[],[]]])) __o__ / \ o o / \ / \ o o o o sage: ascii_art(BinaryTree([[[[],[]],[[],[]]],[]])) ___o___ / \ __o__ o / \ o o / \ / \ o o o o sage: ascii_art(BinaryTree([[],[[[[],[]],[[],[]]],[]]])) _____o______ / \ o ___o___ / \ __o__ o / \ o o / \ / \ o o o o """ node_to_str = lambda bt: str(bt.label()) if hasattr(bt, "label") else "o" if self.is_empty(): from sage.misc.ascii_art import empty_ascii_art return empty_ascii_art from sage.misc.ascii_art import AsciiArt if self[0].is_empty() and self[1].is_empty(): bt_repr = AsciiArt( [node_to_str(self)] ) bt_repr._root = 1 return bt_repr if self[0].is_empty(): node = node_to_str(self) rr_tree = self[1]._ascii_art_() if rr_tree._root > 2: f_line = " " ** Integer( rr_tree._root - 3 ) + node s_line = " " ** Integer( len( node ) + rr_tree._root - 3 ) + "\\" t_repr = AsciiArt( [f_line, s_line] ) * rr_tree t_repr._root = rr_tree._root - 2 else: f_line = node s_line = " " + "\\" t_line = " " ** Integer( len( node ) + 1 ) t_repr = AsciiArt( [f_line, s_line] ) * ( AsciiArt( [t_line] ) + rr_tree ) t_repr._root = rr_tree._root t_repr._baseline = t_repr._h - 1 return t_repr if self[1].is_empty(): node = node_to_str(self) lr_tree = self[0]._ascii_art_() f_line = " " ** Integer( lr_tree._root + 1 ) + node s_line = " " ** Integer( lr_tree._root ) + "/" t_repr = AsciiArt( [f_line, s_line] ) * lr_tree t_repr._root = lr_tree._root + 2 t_repr._baseline = t_repr._h - 1 return t_repr node = node_to_str(self) lr_tree = self[0]._ascii_art_() rr_tree = self[1]._ascii_art_() nb_ = lr_tree._l - lr_tree._root + rr_tree._root - 1 nb_L = int( nb_ / 2 ) nb_R = nb_L + ( 1 if nb_ % 2 == 1 else 0 ) f_line = " " ** Integer( lr_tree._root + 1 ) + "_" ** Integer( nb_L ) + node f_line += "_" ** Integer( nb_R ) s_line = " " ** Integer( lr_tree._root ) + "/" + " " ** Integer( len( node ) + rr_tree._root - 1 + ( lr_tree._l - lr_tree._root ) ) + "\\" t_repr = AsciiArt( [f_line, s_line] ) * ( lr_tree + AsciiArt( [" " ** Integer( len( node ) + 2 )] ) + rr_tree ) t_repr._root = lr_tree._root + nb_L + 2 t_repr._baseline = t_repr._h - 1 return t_repr
def _ascii_art_(self): r""" TESTS:: sage: t = OrderedTree([]) sage: ascii_art(t) o sage: t = OrderedTree([[]]) sage: aa = ascii_art(t);aa o | o sage: aa.get_baseline() 2 sage: tt1 = OrderedTree([[],[[],[],[[[[]]]]],[[[],[],[],[]]]]) sage: ascii_art(tt1) _____o_______ / / / o _o__ o / / / | o o o __o___ | / / / / o o o o o | o | o sage: ascii_art(tt1.canonical_labelling()) ______1_______ / / / 2 _3__ 10 / / / | 4 5 6 ___11____ | / / / / 7 12 13 14 15 | 8 | 9 sage: ascii_art(OrderedTree([[],[[]]])) o_ / / o o | o sage: t = OrderedTree([[[],[[[],[]]],[[]]],[[[[[],[]]]]],[[],[]]]) sage: ascii_art(t) _____o_______ / / / __o____ o o_ / / / | / / o o o o o o | | | o_ o o / / | o o o_ / / o o sage: ascii_art(t.canonical_labelling()) ______1________ / / / __2____ 10 16_ / / / | / / 3 4 8 11 17 18 | | | 5_ 9 12 / / | 6 7 13_ / / 14 15 """ node_to_str = lambda t: str(t.label()) if hasattr(t, "label") else "o" if self.is_empty(): from sage.misc.ascii_art import empty_ascii_art return empty_ascii_art from sage.misc.ascii_art import AsciiArt if len(self) == 0: t_repr = AsciiArt([node_to_str(self)]) t_repr._root = 1 return t_repr if len(self) == 1: repr_child = self[0]._ascii_art_() sep = AsciiArt([" " * (repr_child._root - 1)]) t_repr = AsciiArt([node_to_str(self)]) t_repr._root = 1 repr_root = (sep + t_repr) * (sep + AsciiArt(["|"])) t_repr = repr_root * repr_child t_repr._root = repr_child._root t_repr._baseline = t_repr._h - 1 return t_repr # General case l_repr = [subtree._ascii_art_() for subtree in self] acc = l_repr.pop(0) whitesep = acc._root + 1 lf_sep = " " * (acc._root + 1) + "_" * (acc._l - acc._root) ls_sep = " " * (acc._root) + "/" + " " * (acc._l - acc._root) while len(l_repr) > 0: t_repr = l_repr.pop(0) acc += AsciiArt([" "]) + t_repr if len(l_repr) == 0: lf_sep += "_" * (t_repr._root + 1) else: lf_sep += "_" * (t_repr._l + 1) ls_sep += " " * (t_repr._root) + "/" + " " * (t_repr._l - t_repr._root) mid = whitesep + int((len(lf_sep) - whitesep) / 2) node = node_to_str(self) t_repr = AsciiArt([ lf_sep[:mid - 1] + node + lf_sep[mid + len(node) - 1:], ls_sep ]) * acc t_repr._root = mid t_repr._baseline = t_repr._h - 1 return t_repr