I have my code below,I'm reading the data from a linked list and making a binary tree from it.I'm Using a list here (q) and appending the self.top to it .When i print the value of q it gives me some address.
I don't know what that address is??
Next,when i pop the queue and assign it to parent and print parent.root it prints me the value.How things are working here?
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()