简体   繁体   English

有人可以解释一下这条线吗

[英]can someone explain me this line

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

    def get_level(self):
        level = 0
        p = self.parent
        while p:
            level += 1
            p = p.parent

        return level

    def print_tree(self):
        spaces = ' ' * self.get_level() * 3
        prefix = spaces + "|__" if self.parent else ""
        print(prefix + self.data)
        if self.children:
            for child in self.children:
                child.print_tree()

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

def build_product_tree():
    root = TreeNode("Electronics")

    laptop = TreeNode("Laptop")
    laptop.add_child(TreeNode("Mac"))
    laptop.add_child(TreeNode("Surface"))
    laptop.add_child(TreeNode("Thinkpad"))

    cellphone = TreeNode("Cell Phone")
    cellphone.add_child(TreeNode("iPhone"))
    cellphone.add_child(TreeNode("Google Pixel"))
    cellphone.add_child(TreeNode("Vivo"))

    tv = TreeNode("TV")
    tv.add_child(TreeNode("Samsung"))
    tv.add_child(TreeNode("LG"))

    root.add_child(laptop)
    root.add_child(cellphone)
    root.add_child(tv)

    root.print_tree()

if __name__ == '__main__':
    build_product_tree()

I know the meaning self.parent but someone please explain these: p = p.parent and child.parent = self我知道 self.parent 的含义,但有人请解释一下: p = p.parentchild.parent = self

Imagine you have a whole bunch of buckets.想象一下,你有一大堆桶。 A red one which is the smallest, an orange one which is a bit bigger, a yellow one which is a bit bigger, all the colours of the rainbow.最小的红色,大一点的橙色,大一点的黄色,彩虹的所有颜色。

You put the red inside the orange, then you put the orange inside the yellow, and so on.你把红色放在橙色里面,然后你把橙色放在黄色里面,以此类推。 All of your buckets are now inside other buckets.您的所有存储桶现在都在其他存储桶中。 Except for the violet one.除了紫罗兰。 That's the biggest bucket.那是最大的桶。

Now you start at the red bucket.现在你从红色桶开始。 We'll call that self .我们称它为self We want an algorithm to count how many buckets there are.我们想要一个算法来计算有多少桶。 Let's create a counter variable called level .让我们创建一个名为level的计数器变量。

The parent of the red bucket is the orange bucket, which we can get using p = self.parent .红色桶的父节点是橙色桶,我们可以使用p = self.parent获得。 Now p refers to the orange bucket.现在p指的是橙色桶。 We also want to add 1 to our counter.我们还想给我们的计数器加 1。 So we do level += 1 .所以我们做level += 1

Right now p is the orange bucket.现在p是橙色桶。 But we're not done yet.但我们还没有完成。 We need to find out if the orange bucket also has a parent.我们需要找出橙色桶是否也有父桶。 We will ask for its parent, and reuse the same variable p , by saying p = p.parent .我们将询问其父级,并通过说p = p.parent重用相同的变量p Afte we do this p becomes yellow bucket.在我们这样做之后, p变成了黄色桶。 And we add one to the level again.我们再次向关卡添加一个。

We keep going like this until we get to the violet bucket.我们一直这样走,直到我们到达紫罗兰色桶。 The violet bucket is the outermost bucket.紫水桶是最外层的水桶。 So when we ask the violet bucket for its parent, we won't get an answer.所以当我们向紫罗兰桶询问它的父母时,我们不会得到答案。 In other words, when p is the violet bucket, and we do p = p.parent , p is no longer a bucket!换句话说,当p是紫色桶时,我们做p = p.parentp不再是桶!

Well, we were only looping as long as p is still a bucket.好吧,只要p仍然是一个桶,我们就只是在循环。 In the code, that's the bit that says while p: .在代码中,这就是说while p:的位。 So now we stop looping, and our level variable tells us how many buckets there were.所以现在我们停止循环,我们的level变量告诉我们有多少桶。

child.parent = self is telling a bucket where to go. child.parent = self告诉存储桶 go 的位置。 Let's get an even bigger bucket.让我们来一个更大的桶。 A black bucket.一个黑色的桶。 We'll call that self .我们称它为self And we'll also get the violet bucket.我们还会得到紫罗兰色桶。 We'll call that child .我们会叫那个child Now, I want to tell the violet bucket to go into the black bucket.现在,我要告诉紫水桶到go进黑水桶。 child.parent = self . child.parent = self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM