My remove function is not working properly. When I try to remove the root node it removes another. Please help! I've bolded the remove function.    // remove the node with the given key    public void remove(Node root, Key key)    {        Node parent = null;        Node cur = root;        while (cur != null)        {        if (cur.key == key)        {        if (cur.left == null && cur.right == null)        {        if (parent == null)        {        root = null;        }        else if (parent.left == cur)        {        parent.left = null;        }        else        {        parent.right = null;        }        }        else if (cur.right == null)        {        if (parent == null)        {        root = cur.left;        }        else if (parent.left == cur)        {        parent.left = cur.left;        }        else        {        parent.right = cur.left;        }        }        else if (cur.left == null)        {        if (parent == null)        {        root = cur.right;        }        else if (parent.left == cur)        {        parent.left = cur.right;        }        else        {        parent.right = cur.right;        }        }        else        {        Node suc = cur.right;        while (suc.left != null)        {        suc = suc.left;        }        Node sucData = suc;        cur = sucData;        remove(root, suc.key);        }        return;        }        else if (cur.key.compareTo(key) < 0)        {        parent = cur;        cur = cur.right;        }        else        {        parent = cur;        cur = cur.left;        }        }        return;    }       public static void main(String[] args)    {        Scanner input = new Scanner(System.in);        BST bst = new BST();              System.out.println("Enter key value pairs for the BST (-1 to end):");        while(input.hasNext())        {            int key = input.nextInt();            if(key == -1) break;                          String value = input.next();            bst.insert(key, value);        }        System.out.println(bst.root.key);        System.out.println("The size of the BST is:" + bst.size());        System.out.println("The height of the tree is:" + bst.height(bst.root));        System.out.println("The node with the minimum key is:" + bst.getMin(bst.root));        System.out.println("The value stored at the minimum key is: " + bst.get(bst.getMin(bst.root)));        System.out.println("The node with the maximum key is:" + bst.getMax(bst.root));        System.out.println("Removing the maximum key: ");        bst.remove(bst.root, bst.getMax(bst.root));               System.out.println("This is the root: " + bst.root.key);//        System.out.println("The height of the tree is:" + bst.height(bst.root));        System.out.println("Printing the tree nodes using inorder traversal");        bst.inorder(bst.root);        System.out.println("The size of the BST after removal is:" + bst.size());        System.out.println("Removing the root node: ");               System.out.println("This is the root: " + bst.root.key);//        bst.remove(bst.root, bst.root.key);        System.out.println("The size of the BST after removal is:" + bst.size());        System.out.println("The height of the tree after removing the root is:" + bst.height(bst.root));        bst.inorder(bst.root);        System.out.println("Removing the right child of the root node: ");        bst.remove(bst.root, bst.root.right.key);        bst.inorder(bst.root);        System.out.println("Size of the BST after removal is:" + bst.size());        System.out.println("This is the root: " + bst.root.key);//    } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

My remove function is not working properly. When I try to remove the root node it removes another. Please help!

I've bolded the remove function.



   // remove the node with the given key
   public void remove(Node root, Key key)
   {
       Node parent = null;
       Node cur = root;
       while (cur != null)
       {
       if (cur.key == key)
       {
       if (cur.left == null && cur.right == null)
       {
       if (parent == null)
       {
       root = null;
       }
       else if (parent.left == cur)
       {
       parent.left = null;
       }
       else
       {
       parent.right = null;
       }
       }
       else if (cur.right == null)
       {
       if (parent == null)
       {
       root = cur.left;
       }
       else if (parent.left == cur)
       {
       parent.left = cur.left;
       }
       else
       {
       parent.right = cur.left;
       }
       }
       else if (cur.left == null)
       {
       if (parent == null)
       {
       root = cur.right;
       }
       else if (parent.left == cur)
       {
       parent.left = cur.right;
       }
       else
       {
       parent.right = cur.right;
       }
       }
       else
       {
       Node suc = cur.right;
       while (suc.left != null)
       {
       suc = suc.left;
       }
       Node sucData = suc;
       cur = sucData;
       remove(root, suc.key);
       }
       return;
       }
       else if (cur.key.compareTo(key) < 0)
       {
       parent = cur;
       cur = cur.right;
       }
       else
       {
       parent = cur;
       cur = cur.left;
       }
       }
       return;
   }

  

   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);
       BST<Integer, String> bst = new BST<Integer, String>();      
       System.out.println("Enter key value pairs for the BST (-1 to end):");
       while(input.hasNext())
       {
           int key = input.nextInt();
           if(key == -1) break;              

           String value = input.next();
           bst.insert(key, value);
       }
       System.out.println(bst.root.key);
       System.out.println("The size of the BST is:" + bst.size());
       System.out.println("The height of the tree is:" + bst.height(bst.root));
       System.out.println("The node with the minimum key is:" + bst.getMin(bst.root));
       System.out.println("The value stored at the minimum key is: " + bst.get(bst.getMin(bst.root)));
       System.out.println("The node with the maximum key is:" + bst.getMax(bst.root));
       System.out.println("Removing the maximum key: ");
       bst.remove(bst.root, bst.getMax(bst.root));
      
       System.out.println("This is the root: " + bst.root.key);//
       System.out.println("The height of the tree is:" + bst.height(bst.root));
       System.out.println("Printing the tree nodes using inorder traversal");
       bst.inorder(bst.root);
       System.out.println("The size of the BST after removal is:" + bst.size());
       System.out.println("Removing the root node: ");
      
       System.out.println("This is the root: " + bst.root.key);//
       bst.remove(bst.root, bst.root.key);
       System.out.println("The size of the BST after removal is:" + bst.size());
       System.out.println("The height of the tree after removing the root is:" + bst.height(bst.root));
       bst.inorder(bst.root);
       System.out.println("Removing the right child of the root node: ");
       bst.remove(bst.root, bst.root.right.key);
       bst.inorder(bst.root);
       System.out.println("Size of the BST after removal is:" + bst.size());
       System.out.println("This is the root: " + bst.root.key);//
   }
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Types of trees
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education