Question: cclass BTreeNodeWithInsert ( BTreeNodeBase ) : def _ _ init _ _ ( self , keys = None, ptrs = None, is _ root =
cclass BTreeNodeWithInsertBTreeNodeBase:
def initself keysNone, ptrsNone, isrootFalse, d:
if keys is None:
keys
if ptrs is None:
ptrs
self.keys keys
self.pointers ptrs
self.isroot isroot
self.d d
self.parent None # No parent initially
self.rightsibling None # Right sibling placeholder
self.leftsibling None # Left sibling placeholder
def createnewinstanceself keys, ptrs isroot, d:
return BTreeNodeWithInsertkeys ptrs isroot, d
def insertself newkey:
if lenselfkeys self.d :
self.insertkeynewkey
return self # No split, just return the current node updated
midkey, leftchild, rightchild self.splitnodenewkey
return midkey, leftchild, rightchild
def inserthelperself key:
if lenselfkeys self.d:
self.handlefullnode
def insertkeyself newkey:
self.keys.appendnewkey
self.keys.sort # Ensure keys remain sorted
def insertkeyintolistself newkey:
assert self.isleaf
n lenselfkeys
assert newkey not in self.keys, f'key newkey already exists selfkeys
self.keys.appendnewkey
i n
while i and self.keysi self.keysi:
# Swap keys if necessary
selfkeysi self.keysiselfkeysi self.keysi
i i
def insertkeyandptrself midkey, nodeleft, noderight, i:
n lenselfkeys
assert i and i n
node
def insertintonodeself newkey:
self.keys.appendnewkey # Temporarily add the key
self.keys.sort # Sort the keys to maintain the order
return None
def fixparentpointersforchildrenself:
for j childnode in enumerateselfpointers:
childnode.setparentself j
def splitnodeself newkey:
midindex lenselfkeys
midkey self.keysmidindex # The median key
leftchild BTreeNodeWithInsertselfd
rightchild BTreeNodeWithInsertselfd
leftchild.keys self.keys:midindex
rightchild.keys self.keysmidindex:
if newkey midkey:
leftchild.insertnewkey
else:
rightchild.insertnewkey
return midkey, leftchild, rightchild
def handlefullnodeself:
if self.rightsibling and lenselfrightsibling.keys d:
parentindex self.parent.children.indexself
self.parent.keys.insertparentindex, self.keys.pop
self.rightsibling.keys.insert self.parent.keysparentindex
if not self.isleaf:
fixpointersforchildrenself self.rightsibling
return
elif self.leftsibling and lenselfleftsibling.keys d:
parentindex self.parent.children.indexself
self.parent.keys.insertparentindex, self.keys.pop
self.leftsibling.keys.appendselfparent.keysparentindex
if not self.isleaf:
fixpointersforchildrenselfleftsibling, self
return
else:
self.splitnode
lst
b BTreeNodeWithInsertdisrootTrue
for k in lst:
b binsertk
brepok
printStarting tree is :
drawbtreegraphb
## Note: the insertions above will not trigger any calls to the student's code unless student has modified instructor's code
# TEST lending key to right sibling
printInserting
b binsert
brepok
drawbtreegraphb
assert lenbkeys f'root must have just one key. Your root has lenbkeys
assert bkeys f'root must have a single key : Your root has bkeys
c bpointers
c bpointers
assert cisleaf
assert cisleaf
assert ckeys f'Left child of root must have keys Your left child has keys ckeys
assert ckeys f'Right child of root must have keys Your right child has keys ckeys
this is the error i get:
AttributeError Traceback most recent call last Cell In line b BTreeNodeWithInsertdisrootTrue for k in lst: b binsertk brepok printStarting tree is : AttributeError: 'tuple' object has no attribute 'insert'
im not sure where to go from here
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
