Question

show all steps-python

Review the following BST and create the inorder, preorder and postorder list.

Follow both algorithms.  If the node has a left child and a right child, 1) replace the node’s value with the largest value
in the left subtree and delete that value’s node from the left subtree.

Or 2) replace the node’s value with the smallest value in the right subtree and delete that value’s node from the right subtree.

-What are the two possible values for deleting the root value, 50?

-After deleting the value 72 in the following BST, list the values in preorder traversal.

50) (17 (72) 12 (23 (54) 76 9 14) (19) 67)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#Node class for implementing the BST
class Node: 
    def __init__(self,key):
        self.left = None
        self.right = None
        self.val = key

#Insert function to insert new node into BST
def insert(root,key):
    if root is None:
        return Node(key)
    if(root.val<key):
        root.right = insert(root.right,key)
    elif(root.val>key):
        root.left = insert(root.left,key)
    return root
#inorder tree traversal 
def inorder(root): 
    if root:
        inorder(root.left)
        print(root.val,end=" ")
        inorder(root.right)
#postorder tree traversal
def postOrder(root):
    if root:
        postOrder(root.left)
        postOrder(root.right)
        print(root.val,end=" ")
#preorder tree traversal
def preOrder(root):
    if root:
        print(root.val,end=" ")
        preOrder(root.left)
        preOrder(root.right)
#minimum value of the tree
def minimum(root):
    while(root.left!=None):
        root = root.left
    return root.val
#maximum value of the tree
def maximum(root):
    
    while(root.right!=None):
        root = root.right
    return root.val
#deleting the given value
def deleteNode(root, key):  
    if root is None: 
        return root
    #if the key value is less than root value then
    #it will lies in left subtree
    if key < root.val: 
        root.left = deleteNode(root.left, key) 
    # If the key value is greater than the root value
    # then it will lies in right subtree 
    elif(key > root.val): 
        root.right = deleteNode(root.right, key) 
    #or the key is root value 
    else: 
        # checking for root has one node or not
        if root.left is None : 
            temp = root.right  
            root = None 
            return temp  
              
        elif root.right is None : 
            temp = root.left  
            root = None
            return temp 
  
        #if root has two children
        #then find the minimum value from it's right subtree
        temp = minimum(root.right) 
  
        # Copy the inorder successor's content to this node 
        root.val = temp 
  
        # Delete the inorder successor 
        root.right = deleteNode(root.right , temp) 
    return root 
if __name__=="__main__": 
    
    root = None
    root = insert(root,50)
    root = insert(root,17)
    root = insert(root,12)
    root = insert(root,23)
    root = insert(root,9)
    root = insert(root,14)
    root = insert(root,19)
    root = insert(root,72)
    root = insert(root,54)
    root = insert(root,67)
    root = insert(root,76)
    print("post order:",end=" ")
    postOrder(root)
    print("\npre order:",end=" ")
    preOrder(root)
    print("\nin order:",end=" ")
    inorder(root)
    #finding minimum value of root 50
    print("\nminimum element is:",end=" ")
    print(minimum(root))
    #finding maximum value of root 50
    print("maximum element is:",end=" ")
    print(maximum(root))
    #deleting 72
    root = deleteNode(root,72)
    #After deleting the 72 preorder
    preOrder(root)

Follow the above indentation

The code:

#Node class for implementing the BST
class Node:
def __init__(self,key):
self.left = None
self.right = None
self.val = key

#Insert function to insert new node into BST
def insert(root,key):
if root is None:
return Node(key)
if(root.val<key):
root.right = insert(root.right,key)
elif(root.val>key):
root.left = insert(root.left,key)
return root
#inorder tree traversal
def inorder(root):
if root:
inorder(root.left)
print(root.val,end=" ")
inorder(root.right)
#postorder tree traversal
def postOrder(root):
if root:
postOrder(root.left)
postOrder(root.right)
print(root.val,end=" ")
#preorder tree traversal
def preOrder(root):
if root:
print(root.val,end=" ")
preOrder(root.left)
preOrder(root.right)
#minimum value of the tree
def minimum(root):
while(root.left!=None):
root = root.left
return root.val
#maximum value of the tree
def maximum(root):
  
while(root.right!=None):
root = root.right
return root.val
#deleting the given value
def deleteNode(root, key):
if root is None:
return root
#if the key value is less than root value then
#it will lies in left subtree
if key < root.val:
root.left = deleteNode(root.left, key)
# If the key value is greater than the root value
# then it will lies in right subtree
elif(key > root.val):
root.right = deleteNode(root.right, key)
#or the key is root value
else:
# checking for root has one node or not
if root.left is None :
temp = root.right
root = None
return temp
  
elif root.right is None :
temp = root.left
root = None
return temp
  
#if root has two children
#then find the minimum value from it's right subtree
temp = minimum(root.right)
  
# Copy the inorder successor's content to this node
root.val = temp
  
# Delete the inorder successor
root.right = deleteNode(root.right , temp)
return root
if __name__=="__main__":
  
root = None
root = insert(root,50)
root = insert(root,17)
root = insert(root,12)
root = insert(root,23)
root = insert(root,9)
root = insert(root,14)
root = insert(root,19)
root = insert(root,72)
root = insert(root,54)
root = insert(root,67)
root = insert(root,76)
print("post order:",end=" ")
postOrder(root)
print("\npre order:",end=" ")
preOrder(root)
print("\nin order:",end=" ")
inorder(root)
#finding minimum value of root 50
print("\nminimum element is:",end=" ")
print(minimum(root))
#finding maximum value of root 50
print("maximum element is:",end=" ")
print(maximum(root))
#deleting 72
root = deleteNode(root,72)
#After deleting the 72 preorder
preOrder(root)

Add a comment
Know the answer?
Add Answer to:
show all steps-python Review the following BST and create the inorder, preorder and postorder list. Follow...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • show all steps-python Review the following BST and create the inorder, preorder and postorder list. Try...

    show all steps-python Review the following BST and create the inorder, preorder and postorder list. Try to delete the value 17 and after deletion create the inorder, preorder and postorder list. Follow both algorithms.  If the node has a left child and a right child, 1) replace the node’s value with the largest value in the left subtree and delete that value’s node from the left subtree. Or 2) replace the node’s value with the smallest value in the right subtree...

  • show all steps Review the following BST and create the inorder, preorder and postorder list. Try...

    show all steps Review the following BST and create the inorder, preorder and postorder list. Try to delete the value 17 and after deletion create the inorder, preorder and postorder list. Follow both algorithms.  If the node has a left child and a right child, 1) replace the node’s value with the largest value in the left subtree and delete that value’s node from the left subtree. Or 2) replace the node’s value with the smallest value in the right subtree...

  • JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective:...

    JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective: javafx GUI program for BST Animation. BSTAnimation.java, AbstractTree.java and Tree.java. Modify the BSTAnimation.java  (1) Add Show Preoreder and Show Postorder button. Insert these two buttons next to the Show Inorder button to display tree in selected order. (2) Currently removing node method is to replace the removed node by the highest node on left-subtree. Modify the method by using lowest node on the right-subtree instead....

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • I need help to write this code in C++, I am struggling to find max node's parent's right child and max node&#39...

    I need help to write this code in C++, I am struggling to find max node's parent's right child and max node's left child. Node* maxValueNode(Node* node) {    Node* current = node;    while (current && current->right != NULL)        current = current->right;    return current; } void deleteNode(BST* tree, Node* node, Node* parent) {    //TODO - follow the lecture pseudocode to write the deleteNode function    // - returns true if the value was deleted, false if not    // - don't forget to...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • Write a C++ program to validate computer user-ids and passwords. A list of valid ids and...

    Write a C++ program to validate computer user-ids and passwords. A list of valid ids and passwords (unsorted) is read from a file and stored in a Binary Search Tree (BST) of UserInfo objects. When user-ids and passwords are entered during execution, this BST is searched to determine whether they are legal. Input (file): UserInfo records for valid users Input (keyboard): Ids and passwords of users logging in Output (screen): Messages indicating whether user-ids and passwords are valid, as well...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT