Answered step by step
Verified Expert Solution
Question
1 Approved Answer
i have a data structures assignment in binary search trees please use my class methods and attributes and understand it then answer the questions kindly
i have a data structures assignment in binary search trees please use my class methods and attributes and understand it then answer the questions kindly write for each function a main program to test it after implementing each function
this is the class:
count
class BST:
class Node:
def initself data, leftNone, rightNone:
self.data data
self.left left
self.right right
self.depth
def strself:
return formatselfdata
def initself:
self.root None
def clearself:
self.root None
def isEmptyself:
return self.root is None
def rootValself:
if not self.isEmpty:
return self.root.data
else:
raise TypeErrorNone value"
def rootNodeself:
return self.root
def insertself val:
newNode self.Nodeval None, None
if self.isEmpty:
self.root newNode
return
p None
c self.root
while c is not None:
p c
if val cdata:
c cright
elif val cdata:
c cleft
else:
return
if val pdata:
pright newNode
else:
pleft newNode
def containsself val:
c self.root
while c is not None:
if val cdata:
return True
if val cdata:
c cleft
else:
c cright
return False
def containsRecself val:
return self.containsRecval self.root
def containsRecself val, c:
if c is None:
return False
if cdata val:
return True
if val cdata:
return self.containsRecval cright
else:
return self.containsRecval cleft
def minTself:
if self.isEmpty:
printEmpty # or raise exception
return
c self.root
while cleft is not None:
c cleft
return cdata
def maxTself:
if self.isEmpty:
printEmpty # or raise exception
return
c self.root
while cright is not None:
c cright
return cdata
def printInself:
self.printInselfroot
print
def printInself c:
if c is None:
return
self.printIncleft
printcdata, end
self.printIncright
def printPostself:
self.printPostselfroot
print
def printPostself c:
if c is None:
return
self.printPostcleft
self.printPostcright
printcdata, end
def printPreself:
self.printPreselfroot
print
def printPreself c:
if c is None:
return
printcdata, end
self.printPrecleft
self.printPrecright
def sumTself:
return self.sumTselfroot
def sumTself c:
if c is None:
return
return self.sumTcleftcdataself.sumTcright
def lenself:
return self.countselfroot
def countself c:
if c is None:
return
return self.countcleftself.countcright
#
def strself:
result DLL
self.elementsresult self.root
return strresult
def elementsself dll c:
if c is None:
return
self.elementsdll cleft
dlladdToTailcdata
self.elementsdll cright
def elementslevelorderedself:
dll DLL
if self.root is None:
return dll
q QueueSLL
qenqselfroot
while not qisEmpty:
x qdeq
dlladdToTailxdata # visiting function
if xleft is not None:
qenqxleft
if xright is not None:
qenqxright
return dll
def removeself val:
prev None
k self.root
while k is not None:
if kdata val:
break
elif kdata val:
prev k
k kleft
else:
prev k
k kright
if k is None:
return False
if kleft is None or kright is None:
self.delSinglek prev
else:
self.delDoublek
this is the question:
Count Single Nodes:
Write a recursive function called CountSingleNodesself which returns the number of nodes with only one child in the current tree self
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started