interactive-coding-challenges/graphs_trees/bst/bst.py
Donne Martin de0e70de4f Polish bst challenge and solution
Add root is None input test case.  Update time and space complexity discussion.
2016-03-01 07:03:37 -05:00

30 lines
715 B
Python

class Node(object):
def __init__(self, data):
self.data = data
self.left = None
self.right = None
self.parent = None
def __str__(self):
return str(self.data)
def insert(root, data):
if root is None:
root = Node(data)
return root
if data <= root.data:
if root.left is None:
root.left = Node(data)
root.left.parent = root
return root.left
else:
return insert(root.left, data)
else:
if root.right is None:
root.right = Node(data)
root.right.parent = root
return root.right
else:
return insert(root.right, data)