Created
May 11, 2018 03:53
-
-
Save Unitoi01/5b42ad1dd142409269b9e59634dba1d9 to your computer and use it in GitHub Desktop.
BST tree
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 BST: | |
#1 | |
def __init__(self): | |
self.root = None | |
#2 | |
def setRoot(self, val): | |
self.root = Node(val) | |
#3 | |
def insert(self, val): | |
if(self.root is None): | |
self.setRoot(val) | |
else: | |
self.insertNode(self.root, val) | |
#4 | |
def insertNode(self, currentNode, val): | |
if(val <= currentNode.val): | |
if(currentNode.leftChild): | |
self.insertNode(currentNode.leftChild, val) | |
else: | |
currentNode.leftChild = Node(val) | |
elif(val > currentNode.val): | |
if(currentNode.rightChild): | |
self.insertNode(currentNode.rightChild, val) | |
else: | |
currentNode.rightChild = Node(val) | |
#5 | |
def find(self,currentNode, val): | |
return self.findNode(self.root,currentNode, val) | |
#6 | |
def findNode(self, currentNode, val): | |
if(currentNode is None): | |
return False | |
elif(val == currentNode.val): | |
return True | |
elif(val < currentNode.val): | |
return self.findNode(currentNode.leftChild, val) | |
else: | |
return self.findNode(currentNode.rightChild, val) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment