Python树数据结构

时间:2017-03-22 10:10:25

标签: python data-structures binary-tree

我已经编写了这段代码来计算树中的节点数,但我没有得到正确的答案。

每次返回时都不会添加计数。我不知道为什么。我得到的答案是1+1=2。 只有一旦计数进入账户。

class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
    def size(a):
       count1 = 0
       count2 = 0
    if not a:
        return 0
    else:
       size(a.left)
       count1 +=1
       size(a.right)
       count2 +=1
   return count1+count2

   root1 = Node(1)
   root2 = Node(1)
   root1.left = Node(2)
   root1.right = Node(3)
   root1.left.left = Node(4)
   root1.left.right = Node(5)
   root1.left.right.left=Node(6)

   p=size(root1)
   print(p)

2 个答案:

答案 0 :(得分:0)

正如评论中所述,正确的缩进在Python中是至关重要的

除了缩进问题之外,您的size函数实际上对每个节点计数两次,但您不使用递归调用的返回值,因此它们不会被添加到计数中。

这是修复后的代码版本。

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

# Find the size of the tree rooted at `a`
def size(a):
    if not a:
        return 0
    # count is 1 for this node + the sizes of the subtrees
    count = 1 + size(a.left) + size(a.right)
    return count

# Build a tree
root1 = Node(1)
root1.left = Node(2)
root1.right = Node(3)
root1.left.left = Node(4)
root1.left.right = Node(5)
root1.left.right.left=Node(6)

# Print its size
p = size(root1)
print(p)

<强>输出

6

答案 1 :(得分:0)

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


class Tree:
    def __init__(self):
        self.root = None
        self.size = 0

    def add_node(self, data):
        self.root = self._add_node(data, self.root)
        self.size += 1  # we're about to add a new node, so we have one more element!

    def _add_node(self, data, node):
        node_to_add = Node(data)
        if not node:
            return node_to_add

        if node.data >= node_to_add.data:
            node.left = self._add_node(data, node.left)
        elif node.data < node_to_add.data:
            node.right = self._add_node(data, node.right)

        return node

    def print_tree(self):
        self._print_tree(self.root)

    def _print_tree(self, starting_node):
        if starting_node is None:
            return

        self._print_tree(starting_node.left)
        print starting_node.data,
        self._print_tree(starting_node.right)

    def count_number_nodes(self):
        return self._count_number_of_nodes(self.root)

    def _count_number_of_nodes(self, starting_node):

        num = 1
        if starting_node.left is not None:
            num += self._count_number_of_nodes(starting_node.left)

        if starting_node.right is not None:
            num += self._count_number_of_nodes(starting_node.right)

        return num


tree = Tree()
tree.add_node(10)
tree.add_node(42)
tree.add_node(1)
tree.add_node(-14)
tree.add_node(32)
tree.add_node(12)

print "printing in order traversal of tree (smallest to biggest)"
tree.print_tree()
print
print "size from member variable: " + str(tree.size)
print "size from method call: " + str(tree.count_number_nodes())

输出

printing in order traversal of tree (smallest to biggest)
-14 1 10 12 32 42
size from member variable: 6
size from method call: 6

在这个例子中,我包括两种检索大小的方法,一种是跟踪成员变量,另一种是根据树中节点的数量来计算大小。

成员变量方式更快且推荐,因为它是获取树大小的单个操作。使用该方法时,它会计算每个节点。这给出了O(n)时间复杂度与成员变量访问的O(1)。

但是,要使size变量准确,您必须在如何向树中添加新节点时保持一致,例如,如果您说tree.root.left = Node(6),则不会增加size变量,然后它将是不准确的。

此示例是BST(二叉搜索树)的基本实现,BST具有一个属性,该属性表示树中的节点必须具有0到2个子节点,左节点必须小于节点,并且右节点必须比节点更大。 less greater 比依赖于节点中的数据类型。

我希望这些例子有所帮助。