私は以下のコードを持っています。リンクリストからデータを読み込み、そこからバイナリツリーを作成しています。ここにリストを使用して(q)、self.topを追加します私はqの値を表示するときに私にいくつかのアドレスを与えます。リンクリストノードをキューに追加する
私はそのアドレスは何ですか?
次に、キューをポップして親に割り当てて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()
を出力を取得します。最小限の例を実行するのに十分なコードを投稿できるとは思いませんか?特に言語の壁があるため、回答するための十分な情報がなくても、何を求めているのか分かりにくいです。 [mcve]で問題を切り分けようとしている間に、誤って問題を解決する可能性があります。 –
@KennyOstrom質問を更新し、完全なコードを追加しました – codaholic
素敵なアップデート。私はあなたが以前に何を求めているのか分からなかった。あなたはツリーと何が矛盾しているようですが、それは実際の質問とは関係ないので、なぜオブジェクトデータメンバーであるかを明確にするのではなく、無駄な情報をプリントアウトするのです。答えは、あなたのクラスはPythonにそのことを伝える必要があるということです。 –