简体   繁体   中英

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

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 . We want an algorithm to count how many buckets there are. Let's create a counter variable called level .

The parent of the red bucket is the orange bucket, which we can get using p = self.parent . Now p refers to the orange bucket. We also want to add 1 to our counter. So we do level += 1 .

Right now p is the orange bucket. 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 . Afte we do this p becomes yellow bucket. 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!

Well, we were only looping as long as p is still a bucket. In the code, that's the bit that says while p: . So now we stop looping, and our level variable tells us how many buckets there were.

child.parent = self is telling a bucket where to go. Let's get an even bigger bucket. A black bucket. We'll call that self . And we'll also get the violet bucket. We'll call that child . Now, I want to tell the violet bucket to go into the black bucket. child.parent = self .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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