Deletion in a Binary Search TreeWrite a program to delete an element from a Binary Search Tree (BST). The program should first allow the user to input elements to construct the BST until they enter -1 to stop. Then, it should prompt the user for an element to delete and print the inorder traversal of the BST after deletion.Constraints:NAExample:Sample Input:5030702040-130Sample Output:
Question
Deletion in a Binary Search TreeWrite a program to delete an element from a Binary Search Tree (BST). The program should first allow the user to input elements to construct the BST until they enter -1 to stop. Then, it should prompt the user for an element to delete and print the inorder traversal of the BST after deletion.Constraints:NAExample:Sample Input:5030702040-130Sample Output:
Solution
Sure, here is a Python program that implements deletion in a Binary Search Tree (BST).
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def inorder(root):
if root is not None:
inorder(root.left)
print(root.key),
inorder(root.right)
def insert(node, key):
if node is None:
return Node(key)
if key < node.key:
node.left = insert(node.left, key)
else:
node.right = insert(node.right, key)
return node
def minValueNode(node):
current = node
while(current.left is not None):
current = current.left
return current
def deleteNode(root, key):
if root is None:
return root
if key < root.key:
root.left = deleteNode(root.left, key)
elif(key > root.key):
root.right = deleteNode(root.right, key)
else:
if root.left is None:
return root.right
elif root.right is None:
return root.left
root.key = minValueNode(root.right).key
root.right = deleteNode(root.right, root.key)
return root
root = None
elements = list(map(int, input("Enter elements to construct BST: ").split()))
for element in elements:
if element == -1:
break
root = insert(root, element)
key = int(input("Enter element to delete: "))
root = deleteNode(root, key)
print("Inorder traversal of the BST after deletion: ")
inorder(root)
This program first constructs a BST from the user's input. It then deletes the specified element from the BST and prints the inorder traversal of the BST after deletion.
The deletion operation in a BST is more complicated than insertion. This is because you need to ensure that the tree remains a BST after deletion.
When a node to be deleted has both left and right children, it's replaced with its inorder successor or inorder predecessor node according to its specific situation. We have used inorder successor (smallest in the right subtree) here.
Similar Questions
BSTWrite a program to insert elements into a Binary Search Tree (BST). The program should allow the user to input elements until they enter -1 to stop. After the insertion, print the inorder traversal of the BST.Constraints:NAExample:Sample Input:5030702040-1Sample Output:20 30 40 50 70
Delete a node in BSTWrite a relevant code snippet to define the deleteNode function of the binary search tree Sample Input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 Sample Output:5 15 10 25 35 30 20 45 55 50 65 75 70 60 40Sample input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 60Sample outputoutput=5 15 10 25 35 30 20 45 55 50 65 75
write an alogrithm to delet a node from a binary search tree a proper format
Which function is used to delete elements found in a Binary Search Tree, or BST?Instruction: Choose the option that best answers the question. Answer Choicesdeleteremovereturnmain
Question6Max. score: 100.00Delete a node in BSTWrite a relevant code snippet to define the deleteNode function of the binary search tree Sample Input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 Sample Output:5 15 10 25 35 30 20 45 55 50 65 75 70 60 40Sample input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 60Sample outputoutput=5 15 10 25 35 30 20 45 55 50 65 75 70 4
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.