Answered step by step
Verified Expert Solution
Link Copied!

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 =0
class BST:
class _Node:
def __init__(self, data, left=None, right=None):
self._data = data
self._left = left
self._right = right
self._depth =0
def __str__(self):
return '({0})'.format(self._data)
def __init__(self):
self._root = None
def clear(self):
self._root = None
def isEmpty(self):
return self._root is None
def rootVal(self):
if not self.isEmpty():
return self._root._data
else:
raise TypeError("None value")
def rootNode(self):
return self._root
def insert(self, val):
newNode = self._Node(val, None, None)
if self.isEmpty():
self._root = newNode
return
p = None
c = self._root
while c is not None:
p = c
if val > c._data:
c = c._right
elif val < c._data:
c = c._left
else:
return
if val > p._data:
p._right = newNode
else:
p._left = newNode
def __contains__(self, val):
c = self._root
while c is not None:
if val == c._data:
return True
if val < c._data:
c = c._left
else:
c = c._right
return False
def containsRec(self, val):
return self._containsRec(val, self._root)
def _containsRec(self, val, c):
if c is None:
return False
if c._data == val:
return True
if val > c._data:
return self._containsRec(val, c._right)
else:
return self._containsRec(val, c._left)
def minT(self):
if self.isEmpty():
print("Empty") # or raise exception
return
c = self._root
while c._left is not None:
c = c._left
return c._data
def maxT(self):
if self.isEmpty():
print("Empty") # or raise exception
return
c = self._root
while c._right is not None:
c = c._right
return c._data
def printIn(self):
self._printIn(self._root)
print()
def _printIn(self, c):
if c is None:
return
self._printIn(c._left)
print(c._data, end=",")
self._printIn(c._right)
def printPost(self):
self._printPost(self._root)
print()
def _printPost(self, c):
if c is None:
return
self._printPost(c._left)
self._printPost(c._right)
print(c._data, end=",")
def printPre(self):
self._printPre(self._root)
print()
def _printPre(self, c):
if c is None:
return
print(c._data, end=",")
self._printPre(c._left)
self._printPre(c._right)
def sumT(self):
return self._sumT(self._root)
def _sumT(self, c):
if c is None:
return 0
return self._sumT(c._left)+c._data+self._sumT(c._right)
def __len__(self):
return self._count(self._root)
def _count(self, c):
if c is None:
return 0
return self._count(c._left)+1+self._count(c._right)
# %%
def __str__(self):
result = DLL()
self._elements(result, self._root)
return str(result)
def _elements(self, dll, c):
if c is None:
return
self._elements(dll, c._left)
dll.addToTail(c._data)
self._elements(dll, c._right)
def elements_level_ordered(self):
dll = DLL()
if self._root is None:
return dll
q = QueueSLL()
q.enq(self._root)
while not q.isEmpty():
x = q.deq()
dll.addToTail(x._data) # visiting function
if x._left is not None:
q.enq(x._left)
if x._right is not None:
q.enq(x._right)
return dll
def remove(self, val):
prev = None
k = self._root
while k is not None:
if k._data == val:
break
elif k._data > val:
prev = k
k = k._left
else:
prev = k
k = k._right
if k is None:
return False
if k._left is None or k._right is None:
self._delSingle(k, prev)
else:
self._delDouble(k)
this is the question:
Count Single Nodes:
Write a recursive function called CountSingleNodes(self), 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Organizing Information Digital And Information Literacy

Authors: Greg Roza

1st Edition

1448805929, 978-1448805921

More Books

Students also viewed these Databases questions