Deleting a node from a Binary Search Tree using only a node's key

Viewed 62

I'm adding some functions/working with a pre-written program (a customer account database), which uses a Binary Search Tree, and I'm trying figure out how and why this remove method works:

    public void deleteCustomer()
    {
        System.out.println("Enter the account number of the customer to be removed.");
        String accountNumber = userInput.next();

        try {   
            records.remove(new CustomerAccount(accountNumber));           
        }

        catch(ElementNotFoundException e) {
           System.out.println("There is no account with that number.");
        }    
    }

Where CustomerAccount is an object that contains the accountNumber field, and records is a BST. The records' remove function looks like this:

    public boolean remove(E target) {
        return delete(target) != null;
    }

and recursively calls a standard looking delete function for a BST. But my question is, what is actually happening with this line?

records.remove(new CustomerAccount(accountNumber)); 

It's calling the method using a new CustomerAccount? What's actually happening under the hood here? And is this a standard way of dealing with this situation? I've never seen anything like it, but I'm newish to working with data structures so I'm not sure. Any help or clarification would be fantastic, thank you!

0 Answers
Related