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:20 40 50 70 Explanation:Input: 50 30 70 20 40 -1Delete: 30Output: 20 40 50 70Public Test Cases:# INPUT EXPECTED OUTPUT1 5030702040-13020 40 50 70
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:20 40 50 70 Explanation:Input: 50 30 70 20 40 -1Delete: 30Output: 20 40 50 70Public Test Cases:# INPUT EXPECTED OUTPUT1 5030702040-13020 40 50 70
Solution
Here is a Python solution for the problem:
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
def inorder(root):
if root:
inorder(root.left)
print(root.val),
inorder(root.right)
def insert(root, key):
if root is None:
return Node(key)
else:
if root.val < key:
root.right = insert(root.right, key)
else:
root.left = insert(root.left, key)
return root
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.val:
root.left = deleteNode(root.left, key)
elif(key > root.val):
root.right = deleteNode(root.right, key)
else:
if root.left is None:
return root.right
elif root.right is None:
return root.left
root.val = minValueNode(root.right).val
root.right = deleteNode(root.right, root.val)
return root
r = None
while True:
i = int(input())
if i == -1:
break
r = insert(r, i)
key = int(input())
r = deleteNode(r, key)
inorder(r)
This program first creates a binary search tree from the user's input. It then deletes the specified node and prints the inorder traversal of the tree. The deletion is handled by finding the inorder successor (smallest in the right subtree) of the node to be deleted and replacing the node's value with the successor's value. Then the successor node is deleted. If the node to be deleted has only one child or no children, then the child or NULL is returned to the parent of the node to be deleted.
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
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
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
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.