Task: Add a delete method and print the output in "in order traversal". Please add the new method in this code I made:  .......... class BinarySearchTreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def add_child(self, data): if data == self.data: return # node already exist if data < self.data: if self.left: self.left.add_child(data) else: self.left = BinarySearchTreeNode(data) else: if self.right: self.right.add_child(data) else: self.right = BinarySearchTreeNode(data) # function to build the BST def build_tree(elements): print("INPUT NUMBERS:", elements) root = BinarySearchTreeNode(elements[0]) for i in range(1, len(elements)): root.add_child(elements[i]) return root # function to find maximum value from the tree def getMaxFromBST(root): # storing value in temporary variable temp = root # looping over the right subtree while(temp.right): temp = temp.right # returning the rightmost leaf data return temp.data # function to find minimum value from the tree def getMinFromBST(root): # storing value in temporary variable temp = root # looping over the left subtree while(temp.left): temp = temp.left # returning the leftmost leaf data return temp.data   # Function to find sum of all the element def addBTS(root): if (root == None): return 0 return (root.data + addBTS(root.left) + addBTS(root.right)) # Function that performs pre order traversal def preOrder(root): # if root is null if not root: # return empty list return [] # otherwise first visit root then go left then right return [root.data] + preOrder(root.left)+preOrder(root.right) # Function that performs in order traversal def inOrder(root): # if root is null if not root: # return empty list return [] # otherwise first go to left then root then right return inOrder(root.left) + [root.data] + inOrder(root.right) # Function that performs post order traversal def postOrder(root): # if root is null if not root: # return empty list return [] # otherwise first go left then right then root return postOrder(root.left) + postOrder(root.right) + [root.data] if __name__ == '__main__': numbers_tree = build_tree([15, 12, 7, 14, 27, 20, 23, 88]) # calling the function to get the max and min value min = getMinFromBST(numbers_tree) max = getMaxFromBST(numbers_tree) # printing the result print("MAX: ", max) print("MIN: ", min) print("SUM: ",addBTS(numbers_tree)) print("PRE ORDER: ",preOrder(numbers_tree)) print("IN ORDER: ",inOrder(numbers_tree)) print("POST ORDER: ",postOrder(numbers_tree))

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

Task: Add a delete method and print the output in "in order traversal". Please add the new method in this code I made: 

..........

class BinarySearchTreeNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None

def add_child(self, data):
if data == self.data:
return # node already exist

if data < self.data:
if self.left:
self.left.add_child(data)
else:
self.left = BinarySearchTreeNode(data)
else:
if self.right:
self.right.add_child(data)
else:
self.right = BinarySearchTreeNode(data)


# function to build the BST
def build_tree(elements):
print("INPUT NUMBERS:", elements)
root = BinarySearchTreeNode(elements[0])
for i in range(1, len(elements)):
root.add_child(elements[i])
return root

# function to find maximum value from the tree
def getMaxFromBST(root):
# storing value in temporary variable
temp = root
# looping over the right subtree
while(temp.right):
temp = temp.right
# returning the rightmost leaf data
return temp.data

# function to find minimum value from the tree
def getMinFromBST(root):
# storing value in temporary variable
temp = root
# looping over the left subtree
while(temp.left):
temp = temp.left
# returning the leftmost leaf data
return temp.data
 
# Function to find sum of all the element
def addBTS(root):
if (root == None):
return 0
return (root.data + addBTS(root.left) +
addBTS(root.right))

# Function that performs pre order traversal
def preOrder(root):
# if root is null
if not root:
# return empty list
return []
# otherwise first visit root then go left then right
return [root.data] + preOrder(root.left)+preOrder(root.right)

# Function that performs in order traversal
def inOrder(root):
# if root is null
if not root:
# return empty list
return []
# otherwise first go to left then root then right
return inOrder(root.left) + [root.data] + inOrder(root.right)

# Function that performs post order traversal
def postOrder(root):
# if root is null
if not root:
# return empty list
return []
# otherwise first go left then right then root
return postOrder(root.left) + postOrder(root.right) + [root.data]


if __name__ == '__main__':
numbers_tree = build_tree([15, 12, 7, 14, 27, 20, 23, 88])
# calling the function to get the max and min value
min = getMinFromBST(numbers_tree)
max = getMaxFromBST(numbers_tree)
# printing the result
print("MAX: ", max)
print("MIN: ", min)
print("SUM: ",addBTS(numbers_tree))
print("PRE ORDER: ",preOrder(numbers_tree))
print("IN ORDER: ",inOrder(numbers_tree))
print("POST ORDER: ",postOrder(numbers_tree))
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning