Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use the picture provided to add the following methods to the BinarySearchTree class in python: # Each node of the tree stores data, a

Please use the picture provided to add the following methods to the BinarySearchTree class in python:
# Each node of the tree stores data, a left child, and a right child.
# The children are initialized to None when the node is first created.
class Node:
def __init__(self, new_data):
self.data = new_data
self.left = None
self.right = None
class BinarySearchTree:
# The tree keeps track of its root node. An empty tree has root pointing
# to None.
def __init__(self):
self.root = None
# Wrapper method for an in-order traversal - this just calls the recursive
# helper method starting from the root of the tree
def in_order_traversal(self):
print('In-order traversal:')
self.in_order_traversal_helper(self.root)
# Recursive helper method for in-order traversal
def in_order_traversal_helper(self, which_node):
# Implicit base case is when which_node is None - do nothing in that
# case
if which_node:
self.in_order_traversal_helper(which_node.left)
print(which_node.data) # Visit which_node
self.in_order_traversal_helper(which_node.right)
# Returns a visual representation of the tree
# (This is a wrapper for the recursive str_helper method)
def __str__(self):
return self.str_helper(self.root, '-')
# This method returns a visual representation of the subtree whose root is
# at which_node. The indent parameter keeps track of how much to indent
# each level.
def str_helper(self, which_node, indent):
if not which_node:
return 'None'
else:
# The backslashes at the end of each line allow us to split this
# long expression over multiple lines
return str(which_node.data)+'
'+\
indent + self.str_helper(which_node.left, indent +'-')+'
'+\
indent + self.str_helper(which_node.right, indent +'-')
# Add new_data to the tree
def add(self, new_data):
# Create a new node that contains new_data
new_node = Node(new_data)
# If the tree is currently empty, make new_node the root
if not self.root: # Same thing as self.root == None
self.root = new_node
# If tree is not empty, start from the root and search down the tree
# until an available spot is found
else:
temp = self.root
while True:
if new_data temp.data: # Go left down the tree
# If there's space on the left, insert new_node to the left
if not temp.left: # Same thing as temp.left == None
temp.left = new_node
break
# If there's not space on the left, move left
else:
temp = temp.left
else: # Go right down the tree
# If there's space on the right, insert new_node to the right
if not temp.right: # Same thing as temp.right == None
temp.right = new_node
break
# If there's not space on the right, move right
else:
temp = temp.right
# Searches the tree for a target. Returns the target from the tree if it
# exists, or None if the target doesn't exist. This is a wrapper for the
# recursive find_helper below.
def find(self, target):
return self.find_helper(target, self.root)
# Searches the subtree whose root is which_node for the target. Returns
# the target if it exists in that subtree, or None if the target doesn't
# exist.
def find_helper(self, target, which_node):
# Base case - the target can't exist in an empty subtree
if not which_node:
return None
# Base case - target found!
elif target == which_node.data:
return target
# Recursively search which_node's left subtree
elif target which_node.data:
return self.find_helper(target, which_node.left)
# Recursively search which_node's right subtree
else:
return self.find_helper(target, which_node.right)
image text in transcribed

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

Step: 3

blur-text-image

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

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

11. Identify the apotheosis in Indiana Jones and the Last Crusade.

Answered: 1 week ago