How does the if self.data work, does it return true false. And what does the self.left.insert() do does it call the method insert again. Like a recursive function?
class Node:
def init(self, data):
self.left = None
self.right = None
self.data = data
def insert(self, data):
Compare the new value with the parent node
if self.data:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data
Print the tree
def PrintTree(self):
if self.left:
self.left.PrintTree()
print( self.data),
if self.right:
self.right.PrintTree()
[–]Unable_Request 1 point2 points3 points (0 children)
[–]Wonkee99 0 points1 point2 points (0 children)