Answered step by step
Verified Expert Solution
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 initself newdata:
self.data newdata
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 initself:
self.root None
# Wrapper method for an inorder traversal this just calls the recursive
# helper method starting from the root of the tree
def inordertraversalself:
printInorder traversal:
self.inordertraversalhelperselfroot
# Recursive helper method for inorder traversal
def inordertraversalhelperself whichnode:
# Implicit base case is when whichnode is None do nothing in that
# case
if whichnode:
self.inordertraversalhelperwhichnode.left
printwhichnode.data # Visit whichnode
self.inordertraversalhelperwhichnode.right
# Returns a visual representation of the tree
# This is a wrapper for the recursive strhelper method
def strself:
return self.strhelperselfroot,
# This method returns a visual representation of the subtree whose root is
# at whichnode. The indent parameter keeps track of how much to indent
# each level.
def strhelperself whichnode, indent:
if not whichnode:
return 'None'
else:
# The backslashes at the end of each line allow us to split this
# long expression over multiple lines
return strwhichnode.data
indent self.strhelperwhichnode.left, indent
indent self.strhelperwhichnode.right, indent
# Add newdata to the tree
def addself newdata:
# Create a new node that contains newdata
newnode Nodenewdata
# If the tree is currently empty, make newnode the root
if not self.root: # Same thing as self.root None
self.root newnode
# 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 newdata temp.data: # Go left down the tree
# If there's space on the left, insert newnode to the left
if not temp.left: # Same thing as temp.left None
temp.left newnode
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 newnode to the right
if not temp.right: # Same thing as temp.right None
temp.right newnode
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 findhelper below.
def findself target:
return self.findhelpertarget self.root
# Searches the subtree whose root is whichnode for the target. Returns
# the target if it exists in that subtree, or None if the target doesn't
# exist.
def findhelperself target, whichnode:
# Base case the target can't exist in an empty subtree
if not whichnode:
return None
# Base case target found!
elif target whichnode.data:
return target
# Recursively search whichnode's left subtree
elif target whichnode.data:
return self.findhelpertarget whichnode.left
# Recursively search whichnode's right subtree
else:
return self.findhelpertarget whichnode.right
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