2

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()
user7422128
  • 902
  • 4
  • 17
  • 41
  • You have top, head and root. I don't suppose you can post enough code to run a minimal example? It's hard to tell what you are asking without enough info to answer, especially with the language barrier. You might accidentally solve it while trying to isolate the problem in a [mcve] – Kenny Ostrom May 02 '17 at 20:59
  • @KennyOstrom Updated the question and added the full code – user7422128 May 03 '17 at 17:13
  • Nice update. I had no idea what you were asking before. You seem to be inconsistent on what is a tree and what is a node, but that's not relevant to the actual question, which is why does python print out useless info instead of making it clear what the object data members are. And the answer is, your class has to tell python how to do that. – Kenny Ostrom May 03 '17 at 20:39

1 Answers1

0

You are printing a list. If this list were ["this", "is", "a", "list", "of", "strings"] then you would see exactly what you expect. But because it is a list of class objects, it has to print them somehow. Therefore it defaults to printing the name of the class, and the address of the instance.

You probably should have had "print q" in the code, since that's what you are asking about. I added that after every time q changed, and got this:

30
20
10
[]
[<__main__.createbinarytree instance at 0x02CB9A58>]
[]
[<__main__.createbinarytree instance at 0x02CB9A30>]
[<__main__.createbinarytree instance at 0x02CB9A30>, <__main__.createbinarytree instance at 0x02CB9A08>]

If your class provides a way to convert to a string, then you can make it print more useful info. See What is the purpose of __str__ and __repr__ in Python?

class createbinarytree:
    def __init__(self,data):
        self.root=data
        self.left=None
        self.right=None      ##Creation of binary tree nodes
    def __repr__(self):
        return "<Node value=" + str(self.root) + ">"

then I get output like this

30
20
10
[]
[<Node value=30>]
[]
[<Node value=20>]
[<Node value=20>, <Node value=10>]
Community
  • 1
  • 1
Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
  • I found another way .Your's were also correct but I found the one explained here easy.For ex: **for obj in q: print obj.root **.It had me printed all the root values pushed. Anyways thanks for explaining the concept so deeply. – user7422128 May 05 '17 at 18:25