Python:递归:查找二叉树

时间:2015-10-25 06:01:48

标签: python recursion binary-tree

我正在尝试编写一个函数,通过合并我的BinaryTree类来计算二叉树的叶数:

这是我的BinaryTree类:

class BinaryTree:

def __init__(self, data):
    self.data = data
    self.left = None
    self.right = None

def insert_left(self, new_data):
    if self.left == None:
        self.left = BinaryTree(new_data)
    else:
        t = BinaryTree(new_data)
        t.left = self.left
        self.left = t

def insert_right(self, new_data):
    if self.right == None:
        self.right = BinaryTree(new_data)
    else:
        t = BinaryTree(new_data)
        t.right = self.right
        self.right = t

def get_left(self):
    return self.left

def get_right(self):
    return self.right

def set_data(self, data):
    self.data = data

def get_data(self):
    return self.data

这就是我写的功能:目前它没有输出正确的值。我认为我的递归有问题,但我无法弄清楚:

def num_leaves(my_tree):
    count = 0
    if my_tree.get_left() and my_tree.get_right() is None:
        count += 1
    if my_tree.get_left():
        num_leaves(my_tree.get_left())
    if my_tree.get_right():
        num_leaves(my_tree.get_right())

    return count

输入和输出的示例是:

a = BinaryTree(1)
a.insert_left(2)
a.insert_right(3)
print(num_leaves(a))

输出:

0 

而不是2。

我的函数背后的想法是它重复出现,直到它找到左和右子树为None的节点,然后它添加一个来计数。这样它就能找到每一片叶子。

我做错了什么?

3 个答案:

答案 0 :(得分:3)

你误解了递归调用的独立性。 num_leaves 不能返回除0或1之外的任何内容(如您所写)。 num_leaves 的每个实例都有自己的 count 的本地副本。 num_leaves 不会尝试将此作为运行总和,而是返回此点或此点之下的叶数。

另外,你在num_leaves的第一个if语句中误用了布尔表达式。您没有明确检查左侧树是否为None。虽然你所写的内容恰好以同样的方式运作,但在我看来,你并没有意识到自己在做什么。

def num_leaves(my_tree):
    if my_tree is None:
        return 0
    else:
        return num_leaves(my_tree.get_left()) +
               num_leaves(my_tree.get_right())

答案 1 :(得分:3)

乍一看,#include <stdio.h> void foo(); int gb,ga[6]; int main() { int b = 0; int a[6] = {1,2,3,4,5,6}; gb=b; for (int x = 0 ; x<6 ; x++) ga[x]=a[x]; foo(); return 0; } void foo() { int a=ga; int b[6]; for (int x = 0 ; x<6 ; x++) b[x]=gb[x]; //now you can use a and b here } 的代码应该是这样的:

num_leaves

我将对您的Tree代码发布一些更多改进。 你实现它的方式你的BinaryTree只是一个二叉树而不是二叉搜索树。所以我想如果你对上面的代码提出了简单的改变,它应该可以正常工作。

测试:

这正如预期的那样给出3:

def num_leaves(my_tree):
    count = 0
    if my_tree.get_left() is None and my_tree.get_right() is None:
        count += 1
    if my_tree.get_left():
        count += num_leaves(my_tree.get_left()) # added count +=
    if my_tree.get_right():
        count += num_leaves(my_tree.get_right()) # added count +=

    return count

这给出了2,如预期的那样:

a = BinaryTree(1)
a.insert_left(2)
a.insert_right(3)
a.insert_right(4)
a.get_right().insert_left(5)
print(num_leaves(a))

这给出了2,如预期的那样:

a = BinaryTree(1)
a.insert_left(2)
a.insert_right(3)
print(num_leaves(a))

答案 2 :(得分:3)

我建议将所有btree相关的函数保留在类中。那是OOP。

class BinaryTree:
    ... your code ...

    def is_leaf(self):
        return self.right is None and self.left is None

    def count_leaves(self):
        if self.is_leaf():
            return 1
        count = 0
        if self.right is not None:
            count += self.right.count_leaves()
        if self.left is not None:
            count += self.left.count_leaves()
        return count
相关问题