Skip to content

Instantly share code, notes, and snippets.

@thisiswei
Last active August 29, 2015 14:00
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 thisiswei/11273566 to your computer and use it in GitHub Desktop.
Save thisiswei/11273566 to your computer and use it in GitHub Desktop.
class Node(object):
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def __str__(self):
s = ''
if self.left is not None:
s += str(self.left)
s += str(self.val) + ' '
if self.right is not None:
s += str(self.right)
return s
def __repr__(self):
return str(self)
def insert(n, val):
if val > n.val:
if n.right is None:
n.right = Node(val)
else:
insert(n.right, val)
else:
if n.left is None:
n.left = Node(val)
else:
insert(n.left, val)
node = Node(5)
insert(node, 8)
insert(node, 3)
insert(node, 4)
insert(node, 6)
insert(node, 7)
insert(node, 0)
print node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment