Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this failed test comes up when I test get _ nth _ key: FAIL: Check visited nodes for get _ nth _ key ( 0

this failed test comes up when I test get_nth_key: FAIL: Check visited nodes for get_nth_key(0) call: Step 4: Implement ExtendedAVLTree and
ExtendedAVLNode
Each node in an ExtendedAVLTree must have a correct subtree_key_count after an
insertion or removal operation. Determine which methods in AVLTree and AVLNode
must be overridden in ExtendedAVLTree and ExtendedAVLNode to keep each node's
subtree_key_count correct. New methods can be added along with overridden
methods, if
Expected: [61,38,12,9]
Actual: [] and more. this is my extendedAVLtree.py. please change get nth_key ans get_nth_key_helper, so they both are able to correct this error:
from AVLTree import AVLTree
from ExtendedAVLNode import ExtendedAVLNode
class ExtendedAVLTree(AVLTree):
def __init__(self):
super().__init__()
def make_new_node(self, key):
return ExtendedAVLNode(key)
def insert_node(self, node):
super().insert_node(node)
# Update counts from node to root
while node:
node.update_subtree_key_count()
node = node.parent
def remove_node(self, node_to_remove):
parent = node_to_remove.parent if node_to_remove else None
successful = super().remove_node(node_to_remove)
# Update counts from parent to root after removal if successful
if successful and parent:
while parent:
parent.update_subtree_key_count()
parent = parent.parent
return successful
def get_nth_key(self, n):
if self.root is None:
return -1 # Tree is empty, so n is out of bounds
result, node, visited = self._get_nth_key_helper(self.root, n)
print("Visited nodes:", visited)
return result if node else -1 # Return -1 if the node is None (n is out of bounds)
def _get_nth_key_helper(self, node, n, visited=None):
if visited is None:
visited =[]
if not node:
print(f"Reached a None node, index {n}, visited nodes so far: {visited}") # Debug
return None, None, visited
left_size = node.left.subtree_key_count if node.left else 0
right_size = node.right.subtree_key_count if node.right else 0
print(f"Visiting node {node.key} with left_size {left_size}, right_size {right_size}, searching for index {n}") # Debug
visited.append(node.key) # Track visited nodes
if n < left_size:
return self._get_nth_key_helper(node.left, n, visited)
elif n == left_size:
print(f"Found node: {node.key} at index {n}, path taken: {visited}")
return node.key, node, visited
else:
return self._get_nth_key_helper(node.right, n - left_size -1, visited)
'''
def get_nth_key(self, n):
if self.root is None:
return -1 # Tree is empty, so n is out of bounds
result, node, visited = self._get_nth_key_helper(self.root, n)
# Now 'visited' contains the list of visited node keys
# Here, you could potentially handle the 'visited' list depending on your requirement:
# For example, you could print it or check it against expected values in tests
print("Visited nodes:", visited) # Example of usage
return result if node else -1 # Return -1 if the node is None (n is out of bounds)
'''
def _get_nth_key_helper(self, node, n, visited=None):
if visited is None:
visited =[]
if not node:
print("Reached a None node, returning.") # Debug
return None,None,visited
left_size = node.left.subtree_key_count if node.left else 0
right_size = node.right.subtree_key_count if node.right else 0
print(f"Visiting node {node.key} with left_size {left_size}, right_size {right_size}, searching for index {n}") # Debug
# Add the current node key to the visited list
visited.append(node.key)
if n < left_size:
return self._get_nth_key_helper(node.left, n, visited)
elif n == left_size:
return node.key,node,visited
else:
return self._get_nth_key_helper(node.right, n - left_size -1, visited)
'''
def get_nth_key(self, n):
# Check if the request is within the bounds of existing tree keys
if n <0 or n >=(self.root.subtree_key_count if self.root else 0):
return None # n is out of bounds
return self._get_nth_key_helper(self.root, n)
def _get_nth_key_helper(self, node, n):
if not node:
return None
left_size = node.left.subtree_key_count if node.left else 0
if n < left_size:
return self._get_nth_key_helper(node.left, n)
elif n > left_size:
return self._get_nth_key_helper(node.right, n - left_size -1)
else:
return node.key
'

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_2

Step: 3

blur-text-image_3

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

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions

Question

=+Construct a data- and research-driven SWOT analysis

Answered: 1 week ago

Question

=+Who are our customers?

Answered: 1 week ago

Question

=+What are our goals presently?

Answered: 1 week ago