Knowee
Questions
Features
Study Tools

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

Question

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

🧐 Not the exact question you are looking for?Go ask a question

Solution

The question is asking to write a function to delete a node in a Binary Search Tree (BST). Here is a Python solution:

class Node:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.val = key

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

def inorder(root):
    if root:
        inorder(root.left)
        print(root.val),
        inorder(root.right)

# Driver code
root = Node(40)
root.left = Node(20)
root.right = Node(60)
root.left.left = Node(10)
root.left.right = Node(30)
root.right.left = Node(50)
root.right.right = Node(70)
root.left.left.left = Node(5)
root.left.left.right = Node(15)
root.left.right.left = Node(25)
root.left.right.right = Node(35)
root.right.left.left = Node(45)
root.right.left.right = Node(55)
root.right.right.left = Node(65)
root.right.right.right = Node(75)

print("Inorder traversal of the given tree")
inorder(root)

print("\nDelete 60")
root = deleteNode(root, 60)

print("Inorder traversal of the modified tree")
inorder(root)

This code first creates a BST with the given nodes. Then it deletes the node with value 60 and prints the inorder traversal of the modified tree. The deleteNode function works by searching for the node to be deleted, replacing it with the inorder successor if it has two children, and adjusting the links to maintain the BST property.

This problem has been solved

Similar Questions

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

Question4Max. score: 100.00Find Minimum and Maximum Values of BSTFind Minimum and MaximumWrite a relevant code snippet to print the maximum and minimum values of  the  binary search tree Sample Input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 Sample Output:Minimum is : 5Maximum is : 75Sample input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1Sample output

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

Question3Max. score: 100.00Level Order of TreeGiven an array of unique elements, construct a Binary Search Tree and print the Level Order of the tree.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.ConstraintsPrint the Level Order of the Binary Search Tree, separate each level by newline.Output Format1 <= N <= 10000 <= ar[i] <= 10000Sample input6 10 5 15 2 7 12Sample output10 5 15 2 7 12

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

1/3

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.