Skip to content

Instantly share code, notes, and snippets.

@17twenty
Created March 18, 2014 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 17twenty/9624834 to your computer and use it in GitHub Desktop.
Save 17twenty/9624834 to your computer and use it in GitHub Desktop.
Got asked about a Binary Search Tree so this is my lazy implementation
#!/usr/bin/env python
class BST():
value = None
left = None
right = None
def __init__(self, value=None):
self.value = value
def insert(self, value):
if self.value == None:
self.value = value
return
if value > self.value:
if self.right:
self.right.insert(value)
else:
self.right = BST(value)
elif value < self.value:
if self.left:
self.left.insert(value)
else:
self.left = BST(value)
def show(self):
myStr = str(self.value)
if self.left:
myStr += "[" + self.left.show() +"]"
if self.right:
myStr += "[" + self.right.show() +"]"
return myStr
foo = BST()
foo.insert(10)
foo.insert(20)
foo.insert(5)
foo.insert(15)
foo.insert(30)
print foo.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment