Dec-09-2017, 08:58 PM
Hey all!
This is probably very basic for some people , but i really cant get my head around this.
The code below performs binary tree insertion. The thing i cant understand is, you'll notice in the _insert function return root. I really dont see the point of that return. However with out that return root. The tree doesn't parse. But however in the calling function insert i dont assign that returned value from _insert to anything. Yet its required. Please help me understand this?
This is probably very basic for some people , but i really cant get my head around this.
The code below performs binary tree insertion. The thing i cant understand is, you'll notice in the _insert function return root. I really dont see the point of that return. However with out that return root. The tree doesn't parse. But however in the calling function insert i dont assign that returned value from _insert to anything. Yet its required. Please help me understand this?
def insert(self, val):
if self.root is None:
self.root = Node(val)
else:
self._insert(self.root, val)
def _insert(self, root, val):
print(id(root))
if root is None:
return Node(val)
if val <= root.val:
root.left = self._insert(root.left, val)
else:
root.right = self._insert(root.right, val)
return root
