Skip to content

Instantly share code, notes, and snippets.

@rishi93
Created April 7, 2016 08:47
Show Gist options
  • Save rishi93/fb0c00acb3b8ba5e758eedcb4e839143 to your computer and use it in GitHub Desktop.
Save rishi93/fb0c00acb3b8ba5e758eedcb4e839143 to your computer and use it in GitHub Desktop.
Binary Search Tree - Python
class node():
def __init__(self,val):
self.left = None
self.right = None
self.value = val
def insert(root,val):
if root is None:
root = node(val)
else:
if val < root.value:
if root.left is None:
root.left = node(val)
else:
insert(root.left,val)
elif val > root.value:
if root.right is None:
root.right = node(val)
else:
insert(root.right,val)
def traverse(root):
if root is None:
return
else:
print(root.value)
traverse(root.left)
traverse(root.right)
r = node(5)
insert(r,3)
insert(r,8)
insert(r,1)
insert(r,6)
traverse(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment