Question

In: Computer Science

Question 3: Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...

Question 3:
Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree.
a) Discuss and justify your approach to address each possible case.
b) Is the new tree (with the deleted node removed) unique? Discuss your answer.
Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify its purpose.
Illustrate the performance of the deleteNode method. Delete 71, 400, 271 938, and 69 from the tree in the same order. For all illustrated examples, enumerate the trees (before and after deletion) using your orderTraversal and justify the correctness of your delete method.

Solutions

Expert Solution

So, in this problem we have the function for deleting the node for binary search tree and after that their desendants will adjust accordingly as you will get clear picture after you go through this function.

// Deleting a node for BST
   public static Node deleteNode(Node root, int key)
   {
       // initializing pointer to zero
       Node parent = null;

       // start with root node
       Node curr = root;

       // search key in BST and set its parent pointer
       while (curr != null && curr.data != key)
       {
           // setting parent as current
           parent = curr;

           // if key is less than curr node then go for left sub tree
           // else go for right sub tree
           if (key < curr.data) {
               curr = curr.left;
           }
           else {
               curr = curr.right;
           }
       }

       // returning the root if key is not found
       if (curr == null) {
           return root;
       }

       // Case 1: if the node is a leaf node
       if (curr.left == null && curr.right == null)
       {
           // if node to be deleted is not a root node, then set its
           // parent left/right child to null
           if (curr != root) {
               if (parent.left == curr) {
                   parent.left = null;
               } else {
                   parent.right = null;
               }
           }
           //given tree has only one node then set it to null
           else {
               root = null;
           }
       }

       // Case 2: if the node has 2 children
       else if (curr.left != null && curr.right != null)
       {
           // find its in-order successor node
           Node successor = minimumKey(curr.right);

//function for minimumkey a helper function

//public static Node minimumKey(Node curr)
// {
// while (curr.left != null) {
// curr = curr.left;
//}
//return curr;
//}


           int val = successor.data;

           // recursively delete the successor. Note that the successor
           // will have at-most one child (right child)
           deleteNode(root, successor.data);

           // Copy the value of successor to current node
           curr.data = val;
       }

       // Case 3: if the node has on child
       else
       {
           // find child node
           Node child = (curr.left != null)? curr.left: curr.right;


           if (curr != root)
           {
               if (curr == parent.left) {
                   parent.left = child;
               } else {
                   parent.right = child;
               }
           }

           // if root to be deleted then set it to the child
           else {
               root = child;
           }
       }

       return root;
   }

most probably we covered it in three cases which will give correct output

-->69,71,400,271938

-->69,400,271938

-->69,271938

-->271938


Related Solutions

PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
Question - 1 Using the structural node and methods discussed in Binary Search Tree lectures, create...
Question - 1 Using the structural node and methods discussed in Binary Search Tree lectures, create a method for the Binary Search Tree that takes an unsorted input list and constructs a Binary Search Tree based on its values. Any duplicate value will only appear once on the tree. This method outputs a Binary Search Tree structure (not an enumeration of the tree). Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly...
Create a binary search tree of stacks where each node contains a key and a stack.
IN C++   Create a binary search tree of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key, and a empty...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
In java, create a binary search tree (without using additional libraries) of stacks where each node...
In java, create a binary search tree (without using additional libraries) of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key,...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT