Python 将链接列表节点追加到队列

Python 将链接列表节点追加到队列,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,下面是我的代码,我从一个链表中读取数据,并从中生成一个二叉树。我在这里使用一个列表(q)并在它后面附加self.top。当我打印q的值时,它会给我一些地址 我不知道那个地址是什么 接下来,当我弹出队列并将其分配给parent和print parent.root时,它会将值打印给我 class createnode: def __init__(self,val): self.data=val self.next=None ##Creation of

下面是我的代码,我从一个链表中读取数据,并从中生成一个二叉树。我在这里使用一个列表(q)并在它后面附加self.top。当我打印q的值时,它会给我一些地址

我不知道那个地址是什么

接下来,当我弹出队列并将其分配给parent和print parent.root时,它会将值打印给我

class createnode:
    def __init__(self,val):
        self.data=val
        self.next=None     ##Creation of Node in link list
class createbinarytree:
    def __init__(self,data):
        self.root=data
        self.left=None
        self.right=None      ##Creation of binary tree nodes
class createlist:
    def __init__(self, data = None):
        self.head = None    
        self.top = None       
    def push(self,val):        
        node=createnode(val)
        if self.head is None:
            self.head=node
        else:
            node.next=self.head  ##Pushing the node to a link list
            self.head=node
    def convertedtree(self):
        q=[]
        if self.head is None:   ##Function to convert link list to binary tree
            self.top = None
            return
        self.top=createbinarytree(self.head.data)
        q.append(self.top)  ##Printing  q here gives some kind off address
        self.head=self.head.next
        while(self.head):
         self.parent=q.pop(0)
         self.Leftchild=None
         self.Rightchild=None
         self.Leftchild=createbinarytree(self.head.data)
         q.append(self.Leftchild)
         self.head=self.head.next
         if(self.head ):
          self.Rightchild=createbinarytree(self.head.data)
          q.append(self.Rightchild)
          self.head=self.head.next
         self.parent.left=self.Leftchild
         self.parent.right=self.Rightchild

      def printlist(self):
        temp=self.head
        while(temp):
          print(temp.data)
          temp=temp.next

conv=createlist();
conv.push(10)
conv.push(20)
conv.push(30)
conv.printlist()
conv.convertedtree()

您正在打印列表。如果这个列表是[“this”、“is”、“a”、“list”、“of”、“strings”],那么您将确切地看到您所期望的内容。但是因为它是类对象的列表,所以它必须以某种方式打印它们。因此,它默认打印类的名称和实例的地址

您可能应该在代码中包含“printq”,因为这就是您要问的问题。我补充道,每次q改变后,我都会得到:

30
20
10
[]
[<__main__.createbinarytree instance at 0x02CB9A58>]
[]
[<__main__.createbinarytree instance at 0x02CB9A30>]
[<__main__.createbinarytree instance at 0x02CB9A30>, <__main__.createbinarytree instance at 0x02CB9A08>]
30
20
10
[]
[]
[]
[]
[, ]
如果您的类提供了一种转换为字符串的方法,那么您可以让它打印更多有用的信息。看

class CreateBaryTree:
定义初始化(自身,数据):
self.root=数据
self.left=无
self.right=None##创建二叉树节点
定义报告(自我):
返回“”
然后我得到这样的输出

30
20
10
[]
[<Node value=30>]
[]
[<Node value=20>]
[<Node value=20>, <Node value=10>]
30
20
10
[]
[]
[]
[]
[, ]

您有顶部、头部和根部。我不认为您可以发布足够的代码来运行一个最小的示例?如果没有足够的信息来回答,很难说出你在问什么,尤其是在语言障碍的情况下。在@KennyOstrom更新问题并添加完整的codeNice更新时,您可能在尝试隔离问题时意外地解决了该问题。我以前不知道你在问什么。您似乎在什么是树和什么是节点上不一致,但这与实际问题无关,这就是为什么python打印无用的信息,而不是明确对象数据成员是什么。答案是,你的类必须告诉python如何做到这一点。我找到了另一种方法。你的方法也正确,但我发现这里解释的方法很简单。例如:*对于q:print obj.root**中的obj。它让我打印了所有推送的根值。无论如何,谢谢你对这个概念的深刻解释。
30
20
10
[]
[<Node value=30>]
[]
[<Node value=20>]
[<Node value=20>, <Node value=10>]