#What you want is for the user to just access these functions by providing some data values so you can not traverse through the tree as self is itself
and along with self you are just passing the data as a positional argument
so for that you need a helper function along with it that takes two arguments root and data and then you call that helper function in main function
`The code starts from class Binary tree
class BinaryTree:
def init(self,data):
self.data=data
self.left=None
self.right=None
import queue
class BST:
def init(self):
self.root=None
self.numnodes=0
def printtreehelper(self,root):
q=queue.Queue()
if root==None:
return
q.put(root)
while(not(q.empty())):
curr_node=q.get()
print(curr_node.data,end=':')
if curr_node.left!=None:
q.put(curr_node.left)
print("L",curr_node.left.data,end=",")
else:
print("L",-1,end=",")
if curr_node.right!=None:
q.put(curr_node.right)
print("R",curr_node.right.data,end=" ")
else:
print("R",-1,end="")
print()
def printtree(self):
return self.printtreehelper(self.root)
def ispresenthelper(self,root,data):
if root==None:
return False
if root.data==data:
return True
if root.data>data:
return self.ispresenthelper(root.left,data)
else:
return self.ispresenthelper(root.right,data)
def ispresent(self,data):
return self.ispresenthelper(self.root,data)
def inserthelper(self,root,data):
if root==None:
node=BinaryTree(data)
return node
if root.data>data:
root.left=self.inserthelper(root.left,data)
return root
if root.data<data:
root.right=self.inserthelper(root.right,data)
return root
def insert(self,data):
self.numnodes+=1
self.root=self.inserthelper(self.root,data)
def mini(self,root):
if root==None:
return 100000
if root.left==None:
return root.data
return self.mini(root.left)
def deletehelper(self,root,data):
if root==None:
return False,None
if root.data>data:
deleted,newleftnode=self.deletehelper(root.left,data)
root.left=newleftnode
return deleted,root
if root.data<data:
deleted,newrightnode=self.deletehelper(root.right,data)
root.right=newrightnode
return deleted,root
if root.data==data:
#leafNode
if root.left==None and root.right==None:
return True,None
#if one child
if root.left==None:
return True,root.right
if root.right==None:
return True,root.left
#if both child
replacement=self.mini(root.right)
root.data=replacement
deleted,newrightnode=self.deletehelper(root.right,replacement)
root.right=newrightnode
return deleted,root
def delete(self,data):
deleted,newroot=self.deletehelper(self.root,data)
if deleted:
self.numnodes-=1
self.root=newroot
return deleted
def count(self):
return self.numnodes`