在 Python 中打印树数据结构 [英] Printing a Tree data structure in Python

查看:108
本文介绍了在 Python 中打印树数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种可能的树打印实现,它以用户友好的方式打印树,而不是作为对象的实例.

我在网上发现了这个解决方案:

来源:http://cbio.ufs.ac.za/live_docs/nbn_tut/trees.html

类节点(对象):def __init__(self, value, children = []):self.value = 价值self.children = 孩子def __repr__(self, level=0):ret = "\t"*level+repr(self.value)+"\n"对于 self.children 中的孩子:ret += child.__repr__(level+1)返回 ret

此代码以下列方式打印树:

'祖母''女儿''孙女''孙子''儿子''孙女''孙子'

是否可以在不更改 __repr__ 方法的情况下获得相同的结果,因为我将其用于其他目的.

不修改__repr____str__

的解决方案

def other_name(self, level=0):打印 '\t' * level + repr(self.value)对于 self.children 中的孩子:child.other_name(level+1)

解决方案

是的,将 __repr__ 代码移动到 __str__,然后调用 str()code> 或将其传递给 print 语句.记住在递归调用中也要使用 __str__ :

类节点(对象):def __init__(self, value, children = []):self.value = 价值self.children = 孩子def __str__(self, level=0):ret = "\t"*level+repr(self.value)+"\n"对于 self.children 中的孩子:ret += child.__str__(level+1)返回 retdef __repr__(self):return '<树节点表示>'

演示:

<预><代码>>>>root = node('祖母')>>>root.children = [node('daughter'), node('son')]>>>root.children[0].children = [node('孙女'), node('孙子')]>>>root.children[1].children = [node('孙女'), node('孙子')]>>>根<树节点表示>>>>字符串(根)"'祖母'\n\t'女儿'\n\t\t'孙女'\n\t\t'孙子'\n\t'儿子'\n\t\t'孙女'\n\t\t'孙子'\n">>>打印根'祖母''女儿''孙女''孙子''儿子''孙女''孙子'

I was looking for a possible implementation of tree printing, which prints the tree in a user-friendly way, and not as an instance of object.

I came across this solution on the net:

source: http://cbio.ufs.ac.za/live_docs/nbn_tut/trees.html

class node(object):
    def __init__(self, value, children = []):
        self.value = value
        self.children = children

    def __repr__(self, level=0):
        ret = "\t"*level+repr(self.value)+"\n"
        for child in self.children:
            ret += child.__repr__(level+1)
        return ret

This code prints the tree in the following way:

'grandmother'
    'daughter'
        'granddaughter'
        'grandson'
    'son'
        'granddaughter'
        'grandson'

Is it possible to have the same result but without changing the __repr__ method, because I am using it for another purpose.

EDIT:

Solution without modifying __repr__ and __str__

def other_name(self, level=0):
    print '\t' * level + repr(self.value)
    for child in self.children:
        child.other_name(level+1)

解决方案

Yes, move the __repr__ code to __str__, then call str() on your tree or pass it to the print statement. Remember to use __str__ in the recursive calls too:

class node(object):
    def __init__(self, value, children = []):
        self.value = value
        self.children = children

    def __str__(self, level=0):
        ret = "\t"*level+repr(self.value)+"\n"
        for child in self.children:
            ret += child.__str__(level+1)
        return ret

    def __repr__(self):
        return '<tree node representation>'

Demo:

>>> root = node('grandmother')
>>> root.children = [node('daughter'), node('son')]
>>> root.children[0].children = [node('granddaughter'), node('grandson')]
>>> root.children[1].children = [node('granddaughter'), node('grandson')]
>>> root
<tree node representation>
>>> str(root)
"'grandmother'\n\t'daughter'\n\t\t'granddaughter'\n\t\t'grandson'\n\t'son'\n\t\t'granddaughter'\n\t\t'grandson'\n"
>>> print root
'grandmother'
    'daughter'
        'granddaughter'
        'grandson'
    'son'
        'granddaughter'
        'grandson'

这篇关于在 Python 中打印树数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆