Answered step by step
Verified Expert Solution
Question
1 Approved Answer
( python ) # Hw 6 BST: Implement a Node class for Binary Search Tree class Node: def _ _ init _ _ ( self
python# Hw BST: Implement a Node class for Binary Search Tree
class Node:
def initself value, leftNone, rightNone:
self.value value
self.left left
self.right right
# Return the smallest most left value of the BST
def minself:
node self
while node and node.left:
node node.left
return node
# Task : Return the largest most right value of the BST
def maxself:
# Print all elements in a list through pre order recursively
def preordertraversalself:
self.listOfNodes
self.preorderTraversalself
printselflistOfNodes
# Task Fullfill the inorder traversal function
def inordertraversalself:
# Task Fullfill the postorder traversal function
def postordertraversalself:
# PreOrder Traversal of the Binary Search Tree
def preorderTraversalself node:
if node None:
self.listOfNodes.appendnodevalue
self.preorderTraversalnodeleft
self.preorderTraversalnoderight
# InOrder Traversal of the Binary Search Tree
def inorderTraversalself node:
#PostOrder Traversal of the Binary Search Tree
def postorderTraversalself node:
# Insert value into node by following BST properties
def insertself value, nodeNone, rootTrue:
if root:
node self
if node is None:
return Nodevalue
if value node.value:
node.left self.insertvalue node.left, False
elif value node.value:
node.right self.insertvalue node.right, False
else:
# Duplicate value, ignore it
return node
return node
# Task Create a function to do binary Search, return TrueFalse
def searchself value:
# Task Run the following code to prove your program
val intinputEnter the first value root of your Binary Search Tree:"
tree Nodeval
while value "stop":
tree.insertintval
value inputEnter another value of your Binary Search Tree:"
printInorder traversal:"
tree.inordertraversal
printPreorder traversal:"
tree.preordertraversal
printpostorder traversal:"
tree.postordertraversal
val intinputEnter the value to search in the BST:
printtreesearchvalue
printThe minimum is tree.minvalue
printThe maximum is tree.maxvalue
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