Question: I am so confused why is my code not working? Really been struggling this. Am I suppose to use a recursive function? Could anyone provide

I am so confused why is my code not working? Really been struggling this. Am I suppose to use a recursive function? Could anyone provide me some explanation and a solution on this? Thanks.

I am so confused why is my code not working? Really been

struggling this. Am I suppose to use a recursive function? Could anyone

IMPORTANT: For this exercise, you will be defining a function which USES the BinaryTree ADT. A BinaryTree implementation is provided to you as part of this exercise - you should not define your own BinaryTree class. Instead, your code can make use of any of the BinaryTree ADT methods: BinaryTree().get_data(), set_data(), get_left(), set_left(), get_right(), set_right(), insert_left() and insert_right(). Write a function named get_rightmost_data(b_tree) which is passed a BinaryTree object as a parameterYou can assume that the parameter tree is not None. The function returns the data in the rightmost node of the parameter tree. For example, given the Binary Tree objects, b_tree1, b_tree2, b_tree3 below: b treel: b tree2: b tree3: 8 41 41 20 65 20 65 6 9 11 29 51 91 11 29 51 91 7 4 32 72 99 the following code fragment: print("Rightmost data:", get_rightmost_data(b_treel)) print("Rightmost data:", get_rightmost_data(b_tree2)) print("Rightmost data:", get_rightmost_data(b_tree3)) prints Rightmost data: 9 Rightmost data: 91 Rightmost data: 99 For example: Test Result Rightmost data: 9 b_treel = create_b_tree1() print("Rightmost data:", get_rightmost_data(b_tree1)) Rightmost data: 8 b_tree = BinaryTree(8) print("Rightmost data:", get_rightmost_data(b_tree)) Answer: (penalty regime: 0, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 %) 1. def get_rightmost_data(b_tree): 2 list1 = [b_tree] 3 list2 = (1 4 while len(list1) !=0: 5 element = list1.pop() 6 if element.get_right() !=None: 7 list2.append(element.get_right() 8 return list2[-1].get_data() Precheck Check Test Expected Got Rightmost data: 9 Rightmost data: 9 b_tree1 = create_b_treel() print("Rightmost data:", get_rightmost_data(b_treel)) X Rightmost data: 91 Rightmost data: 65 X b_tree2 = create_b_tree2 print("Rightmost data:", get_rightmost_data(b_tree2)) b_tree3 = create_b_tree3 print("Rightmost data:", get_rightmost_data(b_tree3)) X Rightmost data: 99 Rightmost data: 65 x X b_tree = BinaryTree(8) print("Rightmost data:", get_rightmost_data(b_tree)) Rightmost data: 8 X ***Error*** Traceback (most recent call last): File "_tester__.python3", line 101, in print("Rightmost data:", get_rightmost_data(b_tree)) File "_tester__.python3", line 84, in get_rightmost_data return list2[-1].get_data() IndexError: list index out of range

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!