David Cosman
David Cosman

Reputation: 433

Node deletion in a Binary Search Tree

I am attempting to delete a node from a Binary Search Tree that stores words like a Dictionary. The DictEntry element contains the word, the definition and a number for the type of definition that will be displayed (string, image, etc). The DictionaryException is thrown when the word isn't found.

The user must be able to delete the entry by entering the word in the method ONLY. These are the following methods used for removing;

public void remove(String word) throws DictionaryException {
    if (root!=null) {
        //checks if the word is equal to the entrie's word
        if (word.equals(root.getElement().word()))
            root = replacement(root);
        else {
            // parent is the node above current
            BinaryTreeNode<DictEntry> current, parent = root;
            boolean found = false;
            // if lexicographically smaller than the root's word
            if (word.compareTo(root.getElement().word()) < 0)
                current = root.getLeft();
            // if lexicographically higher than the root's word
            else
                current = root.getRight();
            while (current != null && !found) {
                if (word.equals(current.getElement().word())) {
                    found = true;
                    if (current == current.getLeft())
                        parent.setLeft(replacement(current));
                    else
                        parent.setRight(replacement(current));
                } else {
                    parent = current;

                    if (word.compareToIgnoreCase(current.getElement()
                            .word()) < 0)
                        current = current.getLeft();
                    else
                        current = current.getRight();
                }// end if else
            }// end while
            if (!found)
                throw new DictionaryException("The entry was not found");
        }
    }
}

private BinaryTreeNode<DictEntry> replacement(BinaryTreeNode<DictEntry> node) {
    BinaryTreeNode<DictEntry> found = null;
    // check if both sides are empty
    if (node.getLeft() == null && node.getRight() == null)
        found = null;
    else if (node.getLeft() != null && node.getRight() == null)
        found = node.getLeft();
    else if (node.getLeft() == null && node.getRight() != null)
        found = node.getRight();
    // if both sides have an entry
    else {
        //helper positions
        BinaryTreeNode<DictEntry> current = node.getRight();
        BinaryTreeNode<DictEntry> parent = node;

        //moving positions
        while (current.getLeft() != null) {
            parent = current;
            current = current.getLeft();
        }// end while

        if (node.getRight() == current)
            current.setLeft(node.getLeft());
        else {
            parent.setLeft(current.getRight());
            current.setRight(node.getRight());
            current.setLeft(node.getLeft());
        }
        found = current;
    }// end if else
    return found;
}

My problem is that the node isn't removed whenever I try and test it like so, where dictionary represents the BinarySearchTree;

// Insert and remove a word
try {
    dictionary.insert("course","A series of talks or lessons",1);
    dictionary.remove("course");
    res = dictionary.findWord("course");
    if (res == "") {System.out.println("Remove test passed");}
    else {System.out.println("Remove test failed");}
}
catch(DictionaryException e) {
    System.out.println("Remove test 4 failed");
}

I've tried looking and playing with my insert method, but I got nothing, so I'm assuming the problem is somewhere in my logic on removing.

Upvotes: 0

Views: 713

Answers (1)

readikus
readikus

Reputation: 366

From an initial look, the replacement method is doing Node comparisions using the == operator. This only compares the memory address of the node. If you want to do the comparison based on the value of the node, you will need to use the .equals() method.

Upvotes: 1

Related Questions