I am writing a code to search for elements in a binary search tree. I wrote the code inside the class BinarySearchTree but it returns None.
class Node:
def __init__(self, info):
self.info = info
self.left = None
self.right = None
self.level = None
class BinarySearchTree:
def __init__(self):
self.root = None
def search(self, key):
a = search(self.root)
if self.root is None or self.root == key:
return self.root
if key > self.root:
return self.search(self.root.right)
return self.search(self.root.left)
I tried storing self.search(self.root.right) and self.search(self.root.left) into variables and then returned them using or but still I get None as output.
class Node:
def __init__(self, info):
self.info = info
self.left = None
self.right = None
self.level = None
class BinarySearchTree:
def __init__(self):
self.root = None
def search(self, key):
a = search(self.root)
if self.root is None or self.root == key:
return self.root
if key > self.root:
a = self.search(self.root.right)
b = self.search(self.root.left)
return a or b
I do not understand what I am doing wrong here and why the output is coming as None.
(I am a beginner and this is my first time asking a question here so please forgive me if I have made any silly mistake.)
This was my input
r = Node(1)
r.left = Node(2)
r.right = Node(3)
r.left.left = Node(4)
r.left.right = Node(5)
r.right.right = Node(6)
r.right.left = Node(7)