Last active
August 29, 2015 14:00
-
-
Save thisiswei/11273566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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