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) |
Step 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