如何打印整个父节点 - 子结构

时间:2018-06-06 09:34:26

标签: python python-3.x printf nodes parent

我想用打印功能显示连接到特定节点的所有内容,但到目前为止,我无法使用全局来执行此操作。如何让我的__repr__功能更清洁,更本地?我到目前为止得到的整个代码:

current_parent = None
indent_increase = 1


class Node(object):
    def __init__(self, data):
        self.data = data
        self.parent = None
        self.children = []

    def add_child(self, obj):
        self.children.append(obj)
        obj.parent = self.data

    def __repr__(self):
        global current_parent
        global indent_increase

        if self.children:
            print_data = ""
            print_data += "Node " + str(self.data) + " ↴" + "\n"
            indent = "    "

            for child in self.children:
                if current_parent != child.parent:
                    current_parent = child.parent
                    indent_increase += 1
                    print_data += ((indent * indent_increase) + str(child) + "\n")
                else:
                    print_data += ((indent * indent_increase) + str(child) + "\n")

            indent_increase = 1
            current_parent = 0
            return print_data
        else:
            return "Node " + str(self.data)


a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)
e = Node(5)

c.add_child(d)

a.add_child(b)
a.add_child(c)
a.add_child(e)

print(a)

期望的输出:

Node 1 ↴
    Node 2
    Node 3 ↴
        Node 4
    Node 5

3 个答案:

答案 0 :(得分:1)

您可以在没有class Node(object): def __init__(self, data): self.data = data self.parent = None self.children = [] def add_child(self, obj): self.children.append(obj) obj.parent = self def __repr__(self): if self.children: print_data = "Node " + repr(self.data) + " ↴" + "\n" indent = " " for child in self.children: for line in repr(child).splitlines(): print_data += indent + line + "\n" return print_data else: return "Node " + repr(self.data) 的情况下工作 - 您只需要将结果从嵌套节点拆分为行并在返回结果之前缩进每个行:

<button type="button" onclick="create()">Click me</button>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">
 function create () {
   $.ajax({
      url: "AjaxTestRegistration.php",
      type: "POST",
      data: {
        'bid': 10,
        'kid': 20
        },

        success: function (msg) {
            alert("!");
        }
        });
  }
</script>

我在https://repl.it/repls/ZanyKlutzyBase

处添加了更多嵌套节点的示例

答案 1 :(得分:1)

您可以使用默认值提供__repr__个更多参数:

class Node(object):
    def __init__(self, data):
        self.data = data
        self.parent = None
        self.children = []

    def add_child(self, obj):
        self.children.append(obj)
        obj.parent = self.data

    def __repr__(self, current_parent=None, indent_increase=1):
        if self.children:
            print_data = ""
            print_data += "Node " + str(self.data) + " ↴" + "\n"
            indent = "    "

            for child in self.children:
                if current_parent != child.parent:
                    print_data += ((indent * indent_increase) + child.__repr__(child.parent, indent_increase + 1) + "\n")
                else:
                    print_data += ((indent * indent_increase) + str(child) + "\n")
            return print_data
        else:
            return "Node " + str(self.data)


a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)
e = Node(5)

c.add_child(d)

a.add_child(b)
a.add_child(c)
a.add_child(e)

print(a)

答案 2 :(得分:1)

一种简单的方法是特殊方法__repr__将实际处理委托给递归方法。为了在界面中使用clutter,该方法的名称可以以下划线(_)开头。代码可以是:

class Node(object):
    def __init__(self, data):
        self.data = data
        self.parent = None
        self.children = []

    def add_child(self, obj):
        self.children.append(obj)
        obj.parent = self.data

    def __repr__(self):
        return self._do_repr(0)

    def _do_repr(self, indent_increase):
        indent = "    "
        print_data = (indent * indent_increase) + "Node " + str(self.data)
        if self.children:
            print_data += " ↴" + "\n"

            for child in self.children:
                print_data += child._do_repr(indent_increase + 1)
        else:
            print_data += '\n'
        return print_data

这符合预期:

Node 1 ↴
    Node 2
    Node 3 ↴
        Node 4
    Node 5