Question: python only 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
python only

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])
return root
def traverse_inorder(a_tree, a_list = []): if a_tree != None: traverse_inorder(a_tree.get_left()) a_list.append(a_tree.get_data()) traverse_inorder(a_tree.get_right()) return a_list
test_cases:
# The create_new_bst() function returns a new BSTree # with the node values from the Python list bs_tree = create_new_bst([55, 24, 8, 51, 25, 72, 78]) result = traverse_inorder(bs_tree) print('Inorder:', result) & t = create_new_bst([55, 24, 8, 51, 25, 72, 78]) result = traverse_inorder(t) print('Inorder:', result) |
Dectar 5 Not completo Mark 0.co out af 0.50 Define the traverse_inorder(bs_tree) function which takes a BinorySearchtree object as a parameter. The function returns a Python list of the nodes from the binary search tree, using an in-order traversal of the tree. For example, consider the following binary search trec: Given that the tree has been created using the create_new_bst() function, the following code: bs_tree = create_new_bst([55, 24, 8, 51, 25, 72, 78]) print('Inorder elements:', traverse_inorder(bs_tree)) prints: Inorder elements: [8, 24, 25, 51, 55, 72, 78] Complete the traverse inorder(bs tree) function. You can assume that the following methods of the Binary Search Tree class are available to you. You should not include the BinarySearch Tree class in your answer get_data().get_left().get_right The function header is: def traverse_inorder (bs_tree): For example: Test Result Inorder: [8, 24, 25, 51, 55, 72, 781 # The create_new_bst() function returns a new BS Tree # with the node values from the Python list bs_tree = create_new_bst([55, 24, 8, 51, 25, 72, 78]) result = traverse_inorder(bs_tree) print('Inorder:', result) Answer: (penalty regime: 0, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 %) 1 a_list = [] 2def traverse_inorder(a_tree): while a tree != None: traverse_inordera_tree.get_left() a_list.append(a_tree.get_data()) traverse_inorder(a_tree.get_right() return a list NOVAWN
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
