Question: 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
note i have a remove function that delete any node and deals with all special cases this is the question :
Remove Min
(20 points)
Write a function removeMin2(self) that will remove the minimum and the second minimum from
the current tree (if it exists).
and this is the class:
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 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 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)
return True
def _delSingle(self, ptr, prev):
if ptr is self._root:
if self._root._left is None:
self._root = self._root._right
else:
self._root = self._root._left
elif ptr is prev._left:
if ptr._left is None:
prev._left = ptr._right
else:
prev._left = ptr._left
else:
if ptr._left is None:
prev._right = ptr._right
else:
prev._right = ptr._left
def _delDouble(self, c): # predecessor
prev = c
rep = c._left
while rep._right is not None:
prev = rep
rep = rep._right
c._data = rep._data
self._delSingle(rep, prev)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!