Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

( python ) # Hw 6 BST: Implement a Node class for Binary Search Tree class Node: def _ _ init _ _ ( self

(python)# Hw6 BST: Implement a Node class for Binary Search Tree
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
# Return the smallest (most left) value of the BST
def min(self):
node = self
while node and node.left:
node = node.left
return node
# Task 1: Return the largest (most right) value of the BST
def max(self):
# Print all elements in a list through pre order recursively
def preorder_traversal(self):
self.__listOfNodes =[]
self.__preorderTraversal(self)
print(self.__listOfNodes)
# Task 2 Fullfill the inorder traversal function
def inorder_traversal(self):
# Task 3 Fullfill the postorder traversal function
def postorder_traversal(self):
# Pre-Order Traversal of the Binary Search Tree
def __preorderTraversal(self, node):
if node != None:
self.__listOfNodes.append(node.value)
self.__preorderTraversal(node.left)
self.__preorderTraversal(node.right)
# In-Order Traversal of the Binary Search Tree
def __inorderTraversal(self, node):
#Post-Order Traversal of the Binary Search Tree
def __postorderTraversal(self, node):
# Insert value into node by following BST properties
def insert(self, value, node=None, root=True):
if root:
node = self
if node is None:
return Node(value)
if value < node.value:
node.left = self.insert(value, node.left, False)
elif value > node.value:
node.right = self.insert(value, node.right, False)
else:
# Duplicate value, ignore it
return node
return node
# Task 4 Create a function to do binary Search, return True/False
def search(self, value):
# Task 5 Run the following code to prove your program
val = int(input("Enter the first value (root) of your Binary Search Tree:"))
tree = Node(val)
while value !="stop":
tree.insert(int(val))
value = input("Enter another value of your Binary Search Tree:")
print("In-order traversal:")
tree.inorder_traversal()
print("Pre-order traversal:")
tree.preorder_traversal()
print("post-order traversal:")
tree.postorder_traversal()
val = int(input("Enter the value to search in the BST: "))
print(tree.search(value))
print("The minimum is", tree.min().value)
print("The maximum is", tree.max().value)

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

How To Build A Million Dollar Database

Authors: Michelle Bergquist

1st Edition

0615246842, 978-0615246840

More Books

Students also viewed these Databases questions

Question

Describe new developments in the design of pay structures. page 475

Answered: 1 week ago