如何使树节点对象在打印时显示其内容?

时间:2015-08-29 04:20:28

标签: python class binary-search-tree

当我们调用查找方法时,会发生此错误。谁能说怎么能纠正呢?我无法使用在线可用文档进行调试。这是二叉树类的实现。我知道它与等价问题有关。

import deque

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

    def insert(self, data):    
        if data < self.data:
            if self.left is None:
                self.left = Node(data)
            else:
                self.left.insert(data)
        else:
            if self.right is None:
                self.right = Node(data)
            else:
                self.right.insert(data)  

    def lookup(self, data, parent=None):    
        if self.data == data:
            return self, parent
        if data < self.data:
            if self.left is None:
                return None
            else:
                return self.left.lookup(data, parent=self)
        else:
            if self.right is None:
                return None
            else:
                return self.right.lookup(data, parent=self)

    # aka bfs traversal
    def level_traversal(self):    
        root = self
        dq = deque()
        dq.append(root)
        while dq:
            root = dq.popleft()
            if root.left:
                dq.append(root.left)
            if root.right:
                dq.append(root.right)
            print (root.data)



    def delete(self, data):
        node, parent = self.lookup(data)
        if node.children_count() == 0:
            if parent.left == node:
                parent.left = None
            else:
                parent.right = None
            del node
        elif node.children_count() == 1:
            if node.left:
                n = node.left
            else:
                n = node.right
            if parent:
                if parent.left == node:
                    parent.left = n
                else:
                    parent.right = n
            del node
        else:
            # find the successor
            parent = node
            successor = node.right
            while successor.left:
                parent = successor
                successor = successor.left

            node.data = successor.data
            if parent.left == successor:
                parent.left = successor.right
            else:
                parent.right = successor.right

    def inorder(self):    
        if self.left:
            self.left.inorder()

        print (self.data)

        if self.right:
            self.right.inorder()

    def preorder(self):    
        print (self.data)
        if self.left:
            self.left.preorder()
        if self.right:
            self.right.preorder()

    def postorder(self):    
        if self.left:
            self.left.postorder()
        if self.right:
            self.right.postorder()
        print (self.data)


root = Node(8)
root.insert(3)
root.insert(10)
root.insert(1)
root.insert(6)
root.insert(4)
root.insert(7)
root.insert(14)
root.insert(13)

# look up
print (root.lookup(6))
# level traversal
root.level_traversal()
#mirror image
#root.mirror_image()
#root.delete(3)
#root.level_traversal()

# inorder
#root.inorder()
# pre order
#root.preorder()
# postorder
#root.postorder()
# size
#root.size()
#root.dfs()
#print root.height()

1 个答案:

答案 0 :(得分:3)

这根本不是错误。发生这种情况的原因是您从查找方法返回一个对象元组,这就是打印出来时对象的表示方式。如果您不喜欢这样,可以覆盖__repr__()方法。