Created
April 11, 2021 16:27
-
-
Save LPMatrix/6c02437ef086dafe2a8ff0e4aca71a70 to your computer and use it in GitHub Desktop.
This file contains hidden or 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, data): | |
self.left = None | |
self.right = None | |
self.data = data | |
# Insert method to create nodes | |
def insert(self, data): | |
if self.data: | |
if data < self.data: | |
if self.left is None: | |
self.left = Node(data) | |
else: | |
self.left.insert(data) | |
elif data > self.data: | |
if self.right is None: | |
self.right = Node(data) | |
else: | |
self.right.insert(data) | |
else: | |
self.data = data | |
# findval method to compare the value with nodes | |
def findval(self, lkpval): | |
if lkpval < self.data: | |
if self.left is None: | |
return str(lkpval)+" is not Found" | |
return self.left.findval(lkpval) | |
elif lkpval > self.data: | |
if self.right is None: | |
return str(lkpval)+" is not Found" | |
return self.right.findval(lkpval) | |
else: | |
return str(self.data) + " is found" | |
# Print the tree | |
def PrintTree(self): | |
if self.left: | |
self.left.PrintTree() | |
print(self.data), | |
if self.right: | |
self.right.PrintTree() | |
root = Node(27) | |
root.insert(14) | |
root.insert(35) | |
root.insert(31) | |
root.insert(10) | |
root.insert(19) | |
print(root.findval(7)) | |
print(root.findval(14)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment