Python 3.x Python二叉树

Python 3.x Python二叉树,python-3.x,binary-tree,Python 3.x,Binary Tree,我正在Python3中开发一个二叉树,到目前为止,几乎所有的东西都像预期的那样工作;然而,我有一个函数,该函数应该返回任何给定节点的所有子节点的列表,无论出于何种原因,我只获取对象地址的列表,而不调用我的重写\uuuuu str\uuuu(self)方法 from collections import deque # http://docs.python.org/3.1/tutorial/datastructures.html class BinaryNode: # binary tree

我正在Python3中开发一个二叉树,到目前为止,几乎所有的东西都像预期的那样工作;然而,我有一个函数,该函数应该返回任何给定节点的所有子节点的列表,无论出于何种原因,我只获取对象地址的列表,而不调用我的重写
\uuuuu str\uuuu(self)
方法

from collections import deque  # http://docs.python.org/3.1/tutorial/datastructures.html

class BinaryNode:  # binary tree functionality via iterative means

    def __init__(self, name, data):
        self.Left = None
        self.Right = None
        self.Parent = None
        self.Name = name
        self.Data = data
        return

    def AddNew(self, name, data):
        q = []
        q.append(self)
        while q:
            i = q.pop()
            if i.Name == name:
                i.Data = data
                return i
            elif name < i.Name:
                if i.Left:
                    q.append(i.Left)
                else:
                    i.Left = BinaryNode(name, data)
                    i.Left.Parent = i
                    return i.Left
            else:
                if i.Right:
                    q.append(i.Right)
                else:
                    i.Right = BinaryNode(name, data)
                    i.Right.Parent = i
                    return i.Right

    def Find(self, name):
        q = deque()
        q.append(self)
        '''if self.Left: q.append(self.Left)
        if self.Right: q.append(self.Right)'''
        while q:
            i = q.pop()
            print(i)
            if i.Name == name:
                return i
            elif name < i.Name:
                if i.Left: q.append(i.Left)
                else: return None
            else:
                if i.Right: q.append(i.Left)
                else: return None

    def Children(self):
        children = []
        q = deque()
        if self.Left: q.append(self.Left)
        if self.Right: q.append(self.Right)
        while q:
            i = q.popleft()
            if i.Left: q.append(i.Left)
            if i.Right: q.append(i.Right)
            children.append(i)
        return children

    def Parents(self):
        lst = []
        i = self.Parent
        while i is not None:
            lst.append(i)
            i = i.Parent
        return lst

    def __str__(self): return "{} : {}".format(self.Name, self.Data)
使用生成的控制台输出

Jesse : 21
David : 22
Marli : 23
[<__main__.BinaryNode object at 0x000000000333E160>, <__main__.BinaryNode object at 0x000000000333E1D0>, <__main__.BinaryNode object at 0x000000000333E198>]
David : 22
[<__main__.BinaryNode object at 0x000000000333E1D0>]
None

Python容器始终使用所包含对象的表示

实现一个
\uuuu repr\uuuu
方法,该方法将在打印列表时使用;如果您愿意,可以将其作为
\uuuu str\uuuu
的别名:

 __repr__ = __str__
或显式打印列表中的每个元素:

print(', '.join(map(str, test.Children())))

啊,谢谢你。我肯定不知道。下面是我最后做的
def\uu repr\uuuu(self):在一个不相关的注释上返回str(self)
,人们应该小心,我想我需要重做我的
AddNew
方法。
 __repr__ = __str__
print(', '.join(map(str, test.Children())))