Question: cclass BTreeNodeWithInsert ( BTreeNodeBase ) : def _ _ init _ _ ( self , keys = None, ptrs = None, is _ root =

cclass BTreeNodeWithInsert(BTreeNodeBase):
def __init__(self, keys=None, ptrs=None, is_root=False, d=10):
if keys is None:
keys =[]
if ptrs is None:
ptrs =[]
self.keys = keys
self.pointers = ptrs
self.is_root = is_root
self.d = d
self.parent = None # No parent initially
self.right_sibling = None # Right sibling placeholder
self.left_sibling = None # Left sibling placeholder
def create_new_instance(self, keys, ptrs, is_root, d):
return BTreeNodeWithInsert(keys, ptrs, is_root, d)
def insert(self, new_key):
if len(self.keys)<2* self.d -1:
self._insert_key(new_key)
return self # No split, just return the current node (updated)
mid_key, left_child, right_child = self._split_node(new_key)
return mid_key, left_child, right_child
def insert_helper(self, key):
if len(self.keys)>2* self.d:
self.handle_full_node()
def _insert_key(self, new_key):
self.keys.append(new_key)
self.keys.sort() # Ensure keys remain sorted
def insert_key_into_list(self, new_key):
assert self.is_leaf()
n = len(self.keys)
assert new_key not in self.keys, f'key {new_key} already exists {self.keys}'
self.keys.append(new_key)
i = n
while i >=1 and self.keys[i]< self.keys[i-1]:
# Swap keys if necessary
(self.keys[i-1], self.keys[i])=(self.keys[i], self.keys[i-1])
i = i -1
def insert_key_and_ptr(self, mid_key, node_left, node_right, i):
n = len(self.keys)
assert i >=0 and i <= n
node
def insert_into_node(self, new_key):
self.keys.append(new_key) # Temporarily add the key
self.keys.sort() # Sort the keys to maintain the order
return None
def fix_parent_pointers_for_children(self):
for j, child_node in enumerate(self.pointers):
child_node.set_parent(self, j)
def _split_node(self, new_key):
mid_index = len(self.keys)//2
mid_key = self.keys[mid_index] # The median key
left_child = BTreeNodeWithInsert(self.d)
right_child = BTreeNodeWithInsert(self.d)
left_child.keys = self.keys[:mid_index]
right_child.keys = self.keys[mid_index+1:]
if new_key < mid_key:
left_child.insert(new_key)
else:
right_child.insert(new_key)
return mid_key, left_child, right_child
def handle_full_node(self):
if self.right_sibling and len(self.right_sibling.keys)<2* d:
parent_index = self.parent.children.index(self)
self.parent.keys.insert(parent_index, self.keys.pop())
self.right_sibling.keys.insert(0, self.parent.keys[parent_index])
if not self.is_leaf:
fix_pointers_for_children(self, self.right_sibling)
return
elif self.left_sibling and len(self.left_sibling.keys)<2* d:
parent_index = self.parent.children.index(self)-1
self.parent.keys.insert(parent_index, self.keys.pop(0))
self.left_sibling.keys.append(self.parent.keys[parent_index])
if not self.is_leaf:
fix_pointers_for_children(self.left_sibling, self)
return
else:
self.split_node()
lst =[1,5,2,4,-4,-3,-7,12]
b = BTreeNodeWithInsert(d=2,is_root=True)
for k in lst:
b = b.insert(k)
b.rep_ok()
print('Starting tree is : ')
draw_btree_graph(b)
## 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
print('Inserting -8')
b = b.insert(-8)
b.rep_ok()
draw_btree_graph(b)
assert len(b.keys)==1, f'root must have just one key. Your root has {len(b.keys)}'
assert b.keys[0]==1, f'root must have a single key : [1]. Your root has {b.keys}'
c0= b.pointers[0]
c1= b.pointers[1]
assert c0.is_leaf()
assert c1.is_leaf()
assert c0.keys ==[-8,-7,-4,-3], f'Left child of root must have keys [-8,-7,-4,-3]. Your left child has keys {c0.keys}'
assert c1.keys ==[2,4,5,12], f'Right child of root must have keys [2,4,5,12]. Your right child has keys {c1.keys}'
this is the error i get:
AttributeError Traceback (most recent call last) Cell In[33], line 64 b = BTreeNodeWithInsert(d=2,is_root=True)5 for k in lst: ---->6 b = b.insert(k)7 b.rep_ok()8 print('Starting tree is : ') AttributeError: 'tuple' object has no attribute 'insert'
i'm 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 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 Programming Questions!