0

We are writing a project where we used linked lists to add and subtract numbers within a BST. However, we are able to add numbers but unable to delete them. Could someone look at our deletion function to see what's wrong?

// 2016 Spring Data Structure BST Sample Code at MIU (For the project #2)
// You may add/modify the classes but should maintain the basic structure of the classes.
import java.util.*;
import java.util.*;
class Node { 
    int data; // data
    Node lc; //left child pointer
    Node rc; //right child pointer   
    Node(int data){     // constructor
        this.data=data;
        lc=null;
        rc=null;
    }
}
class BST {
    Node root;
    BST(){
        root=null;
    }
    void insert(int newData){ // This is not the full version
        Node newNode=new Node(newData);
        if(root==null) root=newNode; // root was null
        else { // root was not null, so find the place to attach the new node
            Node ptr=root; // Node pointer
            while(true){
                if(newNode.data==ptr.data) { 
                    System.out.println("The node already exist.");
                    break;
                }
                else if(newNode.data<ptr.data) {
                    if(ptr.lc!=null) ptr=ptr.lc;
                    else {
                        ptr.lc=newNode;
                        System.out.println(newNode.data+" was successfully inserted.");  
                        break;
                    }
                } 
                else if(newNode.data>ptr.data) {
                    if(ptr.rc!=null) ptr=ptr.rc;
                    else {
                        ptr.rc=newNode;
                        System.out.println(newNode.data+" was successfully inserted."); 
                        break;
                    }
                } 
            }
        }             
    }
    Node delete(Node root,int key){     // delete the nodes 
            if (root == null) return null;
            if (root.data > key) {
                root.lc = delete(root.lc, key);
                }
                else if (root.data < key) {
            root.rc = delete(root.rc, key);
        }
        return null;
    }
    boolean search(Node root,int key){ // if the search is successful return the Node or return null

        if(root==null){
            return false;
        }
        if(root.data== key) {
            return true;
        }
            if(root.data!= key){
                return false;
            }
        return true;
    }
    int number_nodes(Node n){ // the counting left child
        if(n == null)
        return 0;
        if(n.lc ==null && n.rc==null) 
        return 1;
        else
            return 1+number_nodes(n.lc);
    }
    int rnumber_nodes(Node n){ // the counting right child
        if(n == null)
        return 0;
        if(n.lc ==null && n.rc==null) 
        return 1;
        else
        return 1+number_nodes(n.rc);
    }
    void inorder(Node n){ // recursive inorder travelsal
        if(n==null) return;
        System.out.print("[");
        inorder(n.lc);
        System.out.print(n.data);
        inorder(n.rc);
        System.out.print("]");
    }
    void preorder(Node n){ // recursive preorder travelsal
        if(n==null) return;
        System.out.print("[");
        System.out.print(n.data);
        preorder(n.lc);        
        preorder(n.rc);
        System.out.print("]");
    }
    void postorder(Node n){ // recursive postorder travelsal
        if(n==null) return;
        System.out.print("[");      
        postorder(n.lc);        
        postorder(n.rc);
        System.out.print(n.data);
        System.out.print("]");
    }
}
public class BST_2015134 { // change the name into your IDs
    public static void main(String[] args) {
        BST bst=new BST();
        Scanner sScan=new Scanner(System.in); // menu
        Scanner iScan=new Scanner(System.in); // data
        while(true){
            System.out.print("\n(q)uit,(i)nsert,(d)elete,(s)earch,i(n)order,(p)reorder,p(o)storder,(h)ow many:");
            String uChoice=sScan.next();
            if(uChoice.equalsIgnoreCase("i")){
                System.out.print("Enter a number to insert:");
                int uData=iScan.nextInt();
                bst.insert(uData);
            }
            else if(uChoice.equalsIgnoreCase("d")){  // deletion
                System.out.print("enter the delete number");
                Scanner s=new Scanner(System.in); // to use new scanner
                int delete_num=s.nextInt(); // 
                bst.delete(bst.root,delete_num);
            }
            else if(uChoice.equalsIgnoreCase("s")){  // search
                System.out.print("enter the search number");
                Scanner s=new Scanner(System.in);
                int search_num=s.nextInt(); 
                    if(bst.search(bst.root,search_num)){
                        while(true){
                        System.out.println(" your number is found"); // to tell you your # is found or not found
                        break;
                        }
                    }
                    else{
                        System.out.println(" your number is not found");
                    }
            }
            else if(uChoice.equalsIgnoreCase("n")){  // in order
                bst.inorder(bst.root);
            }
            else if(uChoice.equalsIgnoreCase("p")){  // pre order
                bst.preorder(bst.root);
            }
            else if(uChoice.equalsIgnoreCase("o")){  // post order
                bst.postorder(bst.root);
            }
            else if(uChoice.equalsIgnoreCase("h")){  // how many
                int x,y;
                x=bst.number_nodes(bst.root);
                y=bst.rnumber_nodes(bst.root);
                int total=x+y;
                System.out.print(total);
            }
            if(uChoice.equalsIgnoreCase("q")) break; // quit
        }
    }
}
This guy
  • 31
  • 3
  • Not sure if it is directly related, but have a look at this question : https://stackoverflow.com/questions/19766566/java-multiple-scanners – Arnaud May 31 '17 at 07:21
  • First of all, can you tell us the expected behaviour and what it is doing right now? Secondly, your delete function always returns null. – Thijs Steel May 31 '17 at 07:23
  • 1
    When you remove a node, don't you have to set the remaining nodes lc and rc? You don't do any of that in your delete method, you just call it until your method returns null. – matt May 31 '17 at 07:27
  • You simply overlooked the case where `root.data == key`, isn't that the most important case to consider ? And you return `null`, always. Your delete function simply isn't finished to be written yet. – Jeremy Grand May 31 '17 at 07:56

1 Answers1

1

Just a recap of what you are doing with this function:

If the key is expected to be contained by the left subtree, remove if from the left subtree with recursion. If the key is expected to be contained by the right subtree, remove if from the left subtree with recursion.
So far so good. However, you return null every time, so all the work you do is actually lost.
You also need to handle the case where the data of the node is equal to the given key. In that case you need to restructure the tree somehow.

The code below should do what you want as long as the node you want to delete is not the root of the tree (you should add a special case for that)

Node delete(Node root,int key){     // delete the nodes 
    if (root == null) return null;
    if (root.data > key) {
        root.lc = delete(root.lc, key);
    }
    else if (root.data < key) {
        root.rc = delete(root.rc, key);
    }else{
        if(root.rc == null)
            return root.lc;
        if(root.lc == null)
            return root.rc;
        //if both the left and right child are effective, you need to restructure the tree
        int minData = findMinimum(root.rc);
        Node newNode = new Node(minData);
        newNode.lc = root.lc;
        newNode.rc = delete(root.rc, minData);
        return newNode;
    }
    return root;
}
private int findMinimum(Node root){
    if (root.lc == null)
        return root.data;
    else
        return findMinimum(root.lc);
}

Note that removing a node from a BST is not trivial. Have a look at this link for more info.

Also note that this tree wil not be very efficient. A balanced tree would we a lot better.

Thijs Steel
  • 1,100
  • 6
  • 14