Question
class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None def insert(self, new_data): if new_data == self.data: return elif new_data def
class BinarySearchTree:
def __init__(self, data): self.data = data self.left = None self.right = None
def insert(self, new_data): if new_data == self.data: return elif new_data
def get_right(self): return self.right
def set_left(self, tree): self.left = tree
def set_right(self, tree): self.right = tree
def set_data(self, data): self.data = data
def get_data(self): return self.data
def create_string(self, spaces): info = ' ' * spaces + str(self.data) if self.left != None: info += ' (l)' + self.left.create_string(spaces+4) if not self.right == None: info += ' (r)' + self.right.create_string(spaces+4) return info
def __str__(self): representation = self.create_string(0) return representation
def create_new_bst(tree_list): root = BinarySearchTree(tree_list[0]) for i in range(1, len(tree_list)): root.insert(tree_list[i])
test case:
t = create_new_bst([55, 24, 8, 51, 25, 72, 78]) result = get_sum_beneath(t, 72) print('Sum beneath 72 =', result) print() t = create_new_bst([55, 24, 8, 51, 25, 72, 78]) result = get_sum_beneath(t, 100) print('Sum beneath 100 =', result)
Quector 8 Precheck results Marked out of Define the get_sum_beneath function which takes two parameters: . a binary search tree (where the data value at each node is an integer) - you can assume all node values are distinct an integer The get_sum_beneath() function returns the total of all of the data values in the tree that lie beneath the node containing the specified value (i.e. either in the left or right subtrees of that nodc). For example, consider the following binary search trec: (35 This tree is created using the create_new_bst() function which creates a new tree with root node 55, and then inserts all the remaining values in order into the BST: t = create_new_bst(155, 24, 8, 51, 25, 72, 78]) The following code: result = get_sum_beneath(t, 24) print('Sum beneath 24 =', result) prints (the total of all of the nodes lying beneath the node "24" is 84, i.e. 8+51 +25). Sum beneath 24 = 84 Complete the gel_sum beneath function. You can assume that the following Binary Search Tree class is available and you should not include this class in your answer class BinarySearchTree: def __init__(self, data): selt.data - data self, left = None self.right - None def search(self, find_data): if self.data == find_data; return self elif find data self data and self.right != None: return self.right.search(find_data) else: return None def get_left(self): return self.lelt def get_right(self): return self.right def set_left(self, tree): self.left - tree def set_right(self, tree): self.right = tree def set_data(self, data): self.data - data def get_data(self): return self.data HINT: you may find it useful to use the search method of the BinarySearch Tree class. NOTE: If the value specified in the get sum beneath call does not exist in the tree, then the function should return 0. The function header is: def get_sum_beneath(bs_tree, value): For example: Test Result t = create new_bst([55, 24, 8, 51, 25, 72, 781) Sum beneath 72 = 78 result - get_Sum_beneath(t, 72) print('Sum beneath 72 =', result) t = create_new_b5t([55, 24, 8, 51, 25, 72, 78]) Sum beneath 140 = result - get_sum_beneath(t, 188) print('Sum beneath 100 =', result) Answer: (penalty regime: 0.0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 %) def get_sum_beneath(bs_tree, value): if bs_tree != None: if bs_tree.get_data() == value: if bs tree.get left() != None and bs tree.get right() = return bs_tree.get_left().get data() + get sum bene: elif bs tree.get left() == None and bs tree.get right() return bs_tree.get_right().get_data() + get_sum_beng elif bs tree.get left() != None and bs tree. get right() return bs_tree.get_left().get_data%) + bs_tree.get_ elif bs tree.get data() > value: get_suni_beneath(bs_tree.get_left(), value) elif bs tree.get_data() result = get_sum_beneath(t, 72) File "_tester_.python3", line 83, in get_sum_beneath get_sum_beneath(bs_tree.get_right), value) File "_tester_.python3", line 75, in get_sum_beneath return bs_tree_get_right().get_data() + gel_sum_beneath(bs_tree.gel_right(), value) TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' - Lab18 Test2 Revision Jump to... Lab20 Priority QueuesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started