简体   繁体   English

在二叉搜索树中打印最大深度

[英]printing max depth in Binary search tree

I am trying to obtain the max depth of a binary search tree however I believe the tree is counting wrong. 我正在尝试获取二叉搜索树的最大深度,但是我认为该树计数错误。

my code: 我的代码:

    def BST_maxdepth(root):
        curdepth = [1]
        maxdepth = [1]
        if root is None:
            return -1
        else:
            curdepth[0] = 1
            maxdepth[0] = 1
            if root.left is not None or root.right is not None:
                curdepth[0] += 1
                if curdepth[0] > maxdepth[0]:
                    maxdepth[0] = curdepth[0]
                BST_maxdepth(root.left)
                BST_maxdepth(root.right)
        return maxdepth[0]

classes & BST: 课程和BST:

class Node:
    def __init__(self,value):
        self.right = None
        self.left = None
        self.value = value


def BST_Insert(root, node):     # root --> root of tree or subtree!
    if root.value is None:
        root = node             # beginning of tree
    else:
        if root.value > node.value:     # go to left
            if root.left is None:
                root.left = node
            else:
                BST_Insert(root.left, node)

        if root.value < node.value:    # go to right
            if root.right is None:
                root.right = node
            else:
                BST_Insert(root.right, node)

tests: 测试:

r = Node(8)


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

output: 输出:

2

expected output: 预期输出:

4

Why not something like... 为什么不像...

def BST_maxdepth(root, depth=0):
    if root is None:
        return depth
    return max(BST_maxdepth(root.left, depth+1),
               BST_maxdepth(root.right, depth+1))

You aren't updating maxdepth more than once. 您不会maxdepth更新maxdepth Perhaps something like this: 也许是这样的:

left_depth = BST_maxdepth(root.left)
right_depth = BST_maxdepth(root.right)
maxdepth[0] = max(left_depth, right_depth) + 1

You're not carrying curdepth and maxdepth with you as you recurse, and they're not global. 递归时,您不会随身携带curdepthmaxdepth ,它们也不是全局的。 At each call to BST_maxdepth , you declare a new curdepth and maxdepth . 在每次调用BST_maxdepth ,都声明一个新的curdepthmaxdepth

This means that the regardless of how deep your tree is, maxdepth will only ever be 2 (or 1 if the root has no children). 这意味着无论您的树有多深, maxdepth只会是2(如果根没有子级, maxdepth 1)。

You could try either using an accumulator, or returning a value from each recursive call and building maxdepth that way. 您可以尝试使用累加器,或者从每个递归调用中返回一个值,并maxdepth方式构建maxdepth

Your maxdepth from each recursive step is not being passed to the parent step. 每个递归步骤的maxdepth不会传递到父步骤。

The information from 来自的信息

BST_maxdepth(root.left)
BST_maxdepth(root.right)

needs to be returned to the parent. 需要返回给父母。

You are re-instantiating them at each level of the search: 您将在搜索的每个级别重新实例化它们:

 curdepth = [1]
 maxdepth = [1]

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

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