计算二进制搜索树中的节点数

时间:2013-10-04 18:05:00

标签: python python-2.7

我正在尝试打印二叉搜索树的大小。但是,我的代码现在正在做的是打印每个节点所处的级别。我觉得好像把我的count += 1放在了错误的地方,或者可能是其他东西。我想知道是否有人可以成为我的额外一双眼睛并给我一个暗示我如何解决这个问题。

输出:

3
3
2
3
3
2
1

预期产出:

7

我的代码:

def BST_size(root, count = 0):
    if root is None:
        print "size -1 (Null value in root)"
    if root is not None:
        count += 1
        if root.left is not None:
            BST_size(root.left, count)
        if root.right is not None:
            BST_size(root.right, count)
    print count 

额外部分:

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)

r = Node(4)
# left
a = Node(2)
b = Node(1)
c = Node(3)
# right
d = Node(8)
e = Node(6)
f = Node(10)

BST_Insert(r, a)
BST_Insert(r, b)
BST_Insert(r, c)
BST_Insert(r, d)
BST_Insert(r, e)
BST_Insert(r, f)

6 个答案:

答案 0 :(得分:3)

def BST_size(root, count = 0):
    if root is None:
        return count

    return BST_size(root.left, BST_size(root.right, count + 1))

答案 1 :(得分:3)

有两种方法可以做到这一点。


简单的方法是return来自每次递归调用的计数,而不只是print,并增加:

def BST_size(root):
    if root is None:
        return -1
    if root is not None:
        if root.left is not None:
            return 1 + BST_size(root.left)
        if root.right is not None:
            return 1 + BST_size(root.right)

def print_BST_size(root):
    size = BST_size(root)
    if size == -1:
        print "size -1 (Null value in root)"
    else:
        print "size", size

然而,这仍然无法奏效,因为您只是在左侧,而不是两者。你想要的是:

    count = -1
    if root is not None:
        if root.left is not None:
            count += BST_size(root.left)
        if root.right is not None:
            count += BST_size(root.right)
    return count

然而,看起来你正试图以尾递归的方式使用累加器。在Python中实现这一点真的没有意义,因为Python并不优化尾调用,但它是学习其他语言和理论原因的有用技能,所以...

这里的问题是你需要所有递归调用共享相同的值,这意味着你需要一个可变值。因此,您可以从[0]开始,而不是0,而count[0] += 1代替count += 1

或者您可以重新安排代码,以便它不会发送count,而是将更新后的计数从一侧传递到另一侧。


无论哪种方式,您的代码都有其他问题。您的递归基本情况是rootNone。但是你也把它作为一个特例,打印-1而不是0。你不能双管齐下。

答案 2 :(得分:2)

def BST_size(root):
    if root is None:
        return 0
    else:
        return BST_size(root.left) + BST_size(root.right) + 1

print "Size is: " + str(BST_size(r))

答案 3 :(得分:1)

if root is not None:
        count = 1
        if root.left is not None:
            count += BST_size(root.left, count)
        if root.right is not None:
            count += BST_size(root.right, count)
return count

答案 4 :(得分:1)

这是一个简短的回答

你必须进行树遍历,其中函数返回低于+1的节点数(本身)

伪代码:

def counter(t):
  rCount = counter(t.right)
  lCount = counter(t.left)
  return 1 + rCount + lCount

答案 5 :(得分:0)

您可以使用以下内容:

def countNodes(root):
    count = 1
    if root is None:
        return -1
    if root is not None:
        if root.left is not None:
            count += countNodes(root.left)
        if root.right is not None:
            count +=  countNodes(root.right)
    return count

并按如下方式调用它:

print ("\nCount of Nodes :" + str(countNodes(root)))
相关问题