Reputation: 51
I can't figure out the type of error that I'm getting.
Traceback (most recent call last): File "C:/Users/Eli/.PyCharmCE2019.2/config/scratches/BinarySearchTree.py", line 43, in bt.printTreePreOrder(bt.Root()) TypeError: 'NoneType' object is not callable
class BinaryTreeNode:
def __init__(self,key, data = None):
self.key = key
self.value = data
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self):
self.Root = None
self.size = 0
def __len__(self):
return self.size
def root (self):
return self.Root
def insert(self,key,data):
new_node = BinaryTreeNode(key,data)
x = self.Root
y = None
while x != None:
y = x
if key < y.key:
x = x.left
else:
x = x.right
if y == None:
self. key < y.key
y.left = new_node
else:
y.right = new_node
self.size +=1
def printTreePreOrder(self,node):
print(node.key)
if node.left:
self.printTreePreOrder(node.left)
if node.right:
self.printTreePreOrder(node.right)
if __name__ == "__main__":
bt = BinarySearchTree()
bt.insert(12,'bill')
bt.insert(6,'Tom')
bt.insert(14,'jill')
bt.insert(3,'guy')
bt.printTreePreOrder(bt.Root())
Upvotes: 0
Views: 136
Reputation: 512
You're attempting to call Root
as if it were a function, but it's only a member variable of class BinaryTreeNode, therefore is not callable.
Furthermore, it is returning as NoneType
because you initialize it to None
and it is never assigned to any other value.
The way you have it implemented, you should do bt.printTreePreOrder(bt.Root)
Upvotes: 2