I am puzzled by the way a certain function (tree_insert) is invoked in Python code. It appears to me that its signature is being violated in the invocation. Can someone clarify? To be specific, tree_insert has arguments self and data. But, in the invocation, the first argument is data and the second is self.left. There appears to be an inconsistency, and yet the code works.
Python not defined recursive function?
class BinaryTree():
def __init__(self, data):
self.left = None
self.right = None
self.data = data
def tree_insert(self, data):
if (data < self.data):
if (self.left != None):
self.tree_insert(data, self.left)
else:
self.left = BinaryTree(data)
else:
if (self.right != None):
self.tree_insert(data, self.right)
else:
self.right = BinaryTree(data)
The function works correctly despite an apparently incorrect way of invocation. There is some Python peculiarity at play here..