python中不可预测的输出二进制搜索树

时间:2010-02-01 04:36:10

标签: python binary-tree

class Node(): 
    def __init__(self,data, left=None, right=None): 
        self.data = data 
        self.left = left
        self.right = right 
class BSTree(): 
    def __init__(self): 
        self.root = None
    def add(self,data):
        if self.root is None:
            self.root = Node(data)
            self.reset()
        else:
            while self.curNode is not None:
                if data < self.curNode.data:
                    self.curNode = self.curNode.left
                elif data > self.curNode.data:
                    self.curNode = self.curNode.right
            self.curNode=Node(data)
            self.reset()
    def pprint(self,Node,indent): 
        if Node is not None:
            self.pprint(Node.left, indent+1) 
            print indent*"     ",Node.data
            self.pprint(Node.right, indent+1)  
if __name__=="__main__": 
    y = BSTree() 
    for pres in ["OBAMA","BUSHW","CLINTON","BUSHG","REGAN","CARTER","FORD","NIXON","JOHNSON"]:
        y.add(pres) 
    y.pprint(y.root,0)

此代码运行时没有错误,但我的输出是

 OBAMA

我无法弄清楚为什么上面的代码在运行时没有错误但只添加第一个节点到树

2 个答案:

答案 0 :(得分:3)

 def __add(self,node,data): 
        if node is None: 
            return Node(data) 
        else: 
            if data < node.data: 
                node.left = self.__add(node.left,data) 
            elif data > node.data: 
                node.right = self.__add(node.right,data) 

此功能不正确。它总是覆盖根节点的第一个左子节点或右子节点,除非根是None

由于这是作业,我不会为你写正确的版本,但这里有一个提示 - 首先找到应该添加新节点的位置,然后分配给左或右孩子。

修改:响应您的更新 - 您现在非常接近。您的上一个错误在于您实际上并未将新节点附加到任何内容。相反,您将其分配给curNode,而{{1}}不是树结构的一部分。您希望将其作为右子或左子项链接到父节点。

答案 1 :(得分:0)

我想出了答案。感谢danben为您提供指导!这是他所说的和我自己看其他一些实现的组合。这是我想出来的,以防有人想知道。

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

    def __add(self,node,data):
        if self.root is None:
            self.root = Node(data)
        if node is None:
            return Node(data)
        else:
            if data < node.data:
                node.left = self.__add(node.left,data)
            elif data > node.data:
                node.right = self.__add(node.right,data)
            return node

    def add(self,data):
        self.__add(self.root,data)

    def __preorder(self,node): 
        if node is not None:
            print node.data 
            self.__preorder(node.left) 
            self.__preorder(node.right)

    def preorder(self):
        self.__preorder(self.root)

    def __inorder(self,node): 
        if node is not None:
            self.__inorder(node.left)
            self.__inorder(node.right)
            print node.data

    def inorder(self):
        self.__inorder(self.root)

    def __postorder(self,node): 
        if node is not None:
            self.__postorder(node.left)
            print node.data
            self.__postorder(node.right)

    def postorder(self):
        self.__postorder(self.root)

    def pprint(self,Node,indent): 
        if Node is not None:
            self.pprint(Node.right, indent+1) 
            print indent*"     ",Node.data
            self.pprint(Node.left, indent+1) 
    def leafcount(self,Node): 
        if Node is None: 
            return 0 
        if self.atLeaf(Node): 
            return 1 
        else: 
            return self.leafcount(Node.left)+self.leafcount(Node.right) 

if __name__=="__main__": 

    y = BSTree()

    for pres\
        in ["OBAMA","BUSHW","CLINTON","BUSHG","REGAN","CARTER","FORD","NIXON","JOHNSON"]:
        y.add(pres)

    y.pprint(y.root,0)