Created
May 12, 2016 01:51
-
-
Save mgarod/acc350155e8fa4d99a2fc0215b639436 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: | |
def __init__(self, val=None): | |
self.data=val | |
self.left=None | |
self.right=None | |
class BST: | |
def __init__(self): | |
self.root=None | |
self.head=None | |
def inorder(self): | |
if self.root is not None: | |
self.inorder_helper(self.root) | |
else: | |
print "Empty tree or we are currently a list" | |
def inorder_helper(self, nd): | |
if nd is None: | |
return | |
self.inorder_helper(nd.left) | |
print nd.data," " | |
self.inorder_helper(nd.right) | |
def insert(self, d): | |
if self.root is None: | |
self.root = Node(d) | |
else: | |
self.insert_helper(self.root, d) | |
def insert_helper(self, nd, d): | |
if d < nd.data: | |
if nd.left is None: | |
nd.left = Node(d) | |
else: | |
self.insert_helper(nd.left, d) | |
else: | |
if nd.right is None: | |
nd.right = Node(d) | |
else: | |
self.insert_helper(nd.right, d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment