遍历二叉搜索树时出错 - Python

时间:2016-02-13 21:00:35

标签: python binary-tree traversal

我试图运行树遍历的经典算法。我创建了一个示例树,并测试了预订 inorder postorder 算法是否正常工作。但在那次测试后我得到的结果不正确。当我从节点 A 开始运行预订算法时,我得到这个序列{A,C,H,D,G},当我应该得到{A,C ,d,H,G}。

我不认为我在代码的遍历部分遗漏了一些东西,但也许我在树的定义中。我自己也看不到错误。谢谢你提前。

class Node:
    id = 0
    def __init__(self,value):
        self.id = id
        Node.id += 1
        self.value = value
        self.left_child = None
        self.right_child = None

    def insert_left_child(self, new_node):
        if self.is_leaf():
            self.insert_left_child(new_node);
        else:
            # As there is already a left child, the pointers must be
            # redistributed to include the new child. 
            current_left_child = self.left_subtree()
            self.insert_left_child(new_node)
            new_node.insert_left_child(current_left_child)

    def insert_right_child(self, new_node):
        if self.is_leaf():
            self.insert_right_child(new_node);
        else:
            # As there is already a right child, the pointers must be
            # redistributed to include the new child.
            current_right_child = self.right_subtree()
            self.insert_right_child(new_node)
            new_node.insert_right_child(current_right_child)

    def left_subtree(self):
        return self.left_child

    def right_subtree(self):
        return self.right_child

    def insert_left_child(self,child_node):
        self.left_child = child_node

    def has_left_children(self):
        return self.left_child != None

    def insert_right_child(self,child_node):
        self.right_child = child_node

    def has_right_children(self):
        return self.right_child != None

    def is_leaf(self):
        return (not self.has_left_children()) & (not self.has_right_children())


class BinaryTree:
    def __init__(self):
        self.root = Node('root')

    def preorder(self,current_node = None):
        if current_node is None:
            current_node = self.root

        print current_node.value
        if current_node.has_left_children():
            self.inorder(current_node.left_subtree())
        if current_node.has_right_children():
            self.inorder(current_node.right_subtree())

    def inorder(self,current_node = None):
        if current_node is None:
            current_node = self.root

        if current_node.has_left_children():
            self.inorder(current_node.left_subtree())
        print current_node.value
        if current_node.has_right_children():
            self.inorder(current_node.right_subtree())

    def postorder(self,current_node = None):
        if current_node is None:
            current_node = self.root

        if current_node.has_left_children():
            self.inorder(current_node.left_subtree())
        if current_node.has_right_children():
            self.inorder(current_node.right_subtree())
        print current_node.value

# Main routine

#           root
#       A           B
#     C   D       E   F
#        H G

tree = BinaryTree()

A = Node('A')
B = Node('B')
C = Node('C')
D = Node('D')
E = Node('E')
F = Node('F')
G = Node('G')
H = Node('H')

tree.root.insert_left_child(A)
tree.root.insert_right_child(B)
A.insert_left_child(C)
A.insert_right_child(D)
B.insert_left_child(E)
B.insert_right_child(F)
D.insert_left_child(H)
D.insert_right_child(G)

tree.preorder(A)

3 个答案:

答案 0 :(得分:3)

您从inorder方法调用preorder方法。因此,当您深入了解树时,您可以更改算法:

    if current_node.has_left_children():
        self.inorder(current_node.left_subtree())
    if current_node.has_right_children():
        self.inorder(current_node.right_subtree())

应该是:

    if current_node.has_left_children():
        self.preorder(current_node.left_subtree())
    if current_node.has_right_children():
        self.preorder(current_node.right_subtree())

答案 1 :(得分:1)

有关此代码的 lot 。你应该摆脱所有多余的访问者方法,并立即访问左右儿童。另外,考虑制作例如is_leaf属性而不是方法。但可能导致错误的原因如下:

rake assets:clean; rake assets:precompile

按照您的想法行事。它将内置函数“id”分配给您的id,从而使所有节点具有相同的id。

要解决这个问题,要么使用Node.id,要么更好,使用它:

class Node:
    id = 0
    def __init__(self,value):
        self.id = id
        Node.id += 1

答案 2 :(得分:0)

在此处复制粘贴错误,您错误地在localStorage(和inorder)中调用了preorder