calculating depth of a binary tree in Python

后端 未结 4 1311
春和景丽
春和景丽 2021-02-20 15:16

I am new to programming and am trying to calculate the depth of a binary tree in Python . I believe that my error is because depth is a method of the Node class and not a regula

相关标签:
4条回答
  • 2021-02-20 15:49

    For clarity I would suggest writing your depth method like this:

    def depth(self):
        current_depth = 0
    
        if self.left:
            current_depth = max(current_depth, self.left.depth())
    
        if self.right:
            current_depth = max(current_depth, self.right.depth())
    
        return current_depth + 1
    
    0 讨论(0)
  • 2021-02-20 15:53

    You've got four cases to consider:

    1. Both subtrees are empty.
    2. The left subtree alone is empty.
    3. The right subtree alone is empty.
    4. Neither subtree is empty.

    You've covered cases 1 and 4, but missed 2 and 3. The fix:

    # Return height of tree rooted at this node.
    def depth(self):
        if self.left == None and self.right == None:
            return 1
        elif self.left == None:
            return self.right.depth() + 1
        elif self.right == None:
            return self.left.depth() + 1
        else:
            return max(self.left.depth(), self.right.depth()) + 1
    
    0 讨论(0)
  • 2021-02-20 15:54
    def depth(self):
        if self.left == None and self.right == None:
            return 1
    
        return max(depth(self.left), depth(self.right)) + 1
    

    should be

    def depth(self):
        return max(self.left.depth() if self.left else 0, self.right.depth() if self.right else 0) + 1
    

    A more readable version:

    def depth(self):
        left_depth = self.left.depth() if self.left else 0
        right_depth = self.right.depth() if self.right else 0
        return max(left_depth, right_depth) + 1
    

    The issue is that there is no function depth. It's a method of the Node object, so you would need to call it from the object itself (left and right). I shortened the code to self.left.depth() if self.left else 0 and self.right.depth() if self.right else 0 in order to remove the checks you previously have (they're implicit now) since I believe it is entirely possible that the left is None while the right is a Node or vice versa, which would cause the original code to throw an AttributeError since None does not have a method depth.

    Edit

    In response to the question about the <something> if <some condition> else <otherwise> block:

    The line gives <something> if <some condition> is true-y (treated as true), and <otherwise> if <some condition> is false-y (treated as false)

    0 讨论(0)
  • 2021-02-20 16:09

    The error is coming from this line:

    return max(depth(self.left), depth(self.right)) + 1
    

    You're using depth as a function and trying to apply it to the left and right nodes. Because the left and right nodes are also nodes, they have the depth method.

    You should be calling the depth method like this:

    return max(self.left.depth(), self.right.depth()) + 1
    

    The self parameter is implicitly passed to the depth method, but using it with the dot operator tells Python that this method belongs to a Node instance, and it is not some other function not bound to an object.

    0 讨论(0)
提交回复
热议问题