-2
class Node:
    def __init__ (self, data):
        self.left = None
        self.data = data
        self.right = None
        
class BinaryTree:
    def __init__(self):
        self. i = -1
        self.newnode = None
    
    def Buildtree(self,nodes):
        self.i = self.i + 1     # Just an Incrementer 
        if (nodes[self.i] == -1):
            return None
        else:
            self.newnode = Node(nodes[self.i])
            self.newnode.left = self.Buildtree(nodes)
            self.newnode.right = self.Buildtree(nodes)
    
        return self.newnode
        
t = BinaryTree()
root = t.Buildtree([1,2,4,-1,-1,5,-1,-1,3,-1,6,-1,-1])
print(root)

Why is it printing some node address instead of root value which is '1'.??? Pls help

trincot
  • 317,000
  • 35
  • 244
  • 286

1 Answers1

0

You are printing the value of root, which is returned by:

return self.newnode

Root is a node object, and printing an object in python prints the object's address. If you want it to print the value of root, you must specify:

print(root.data)
J Lem
  • 47
  • 9