0

So in my binary search tree, I'm trying to test my delete method to see if it removes a node from the BST. The problem is my test keeps saying that it didn't work. Here's how I'm testing my delete method

message = "Test 3: deleting 'word' -- ";
t = new BSTRefBased();
try {
    t.delete("word");
    result = t.getRootItem().getWord().equals(null);
} catch (Exception e) {
    result = false;
}
System.out.println(message + (result ? "passed" : "failed"));

Here's my delete method:

   public void delete(String word) {
        root = deleteItem(root, word);
    }


    protected TreeNode deleteItem(TreeNode r, String word) {
        if (r == null){
            return r;
        }
        if(word.compareTo(r.item.getWord()) < 0){
            return r;
        } else if (word.compareTo(r.item.getWord()) > 0) {
            return r;
        } else if(r.left != null && r.right != null)
        {
            return deleteItem(r, word);
        } else {
            return r;
        }
    return r;
}

So why does it keep saying that my delete method failed in my output? Is the problem with my test code or with the actual method? Also I did previously insert the word 'word' in my BST so it should be there. Here's a pseudo code version of what I want my delete method to do:

delete(treeNode ,searchitem)
targetNode = search(treeNode ,searchItem)
if targetNode is null
return

P = parent node of target Node

if targetNode has no children
update ref in P that leads to targetNode
return

if targetNode has only one child C update ref in P that leads 
to targetNode by overwriting that ref with C 
(either left- or right-ref in P)
return

M = targetNode's inorder successor (i.e., left-most in-order 
successor in targetNode's right subtree)
m = item in M
copy m into targetNode's item field 
delete (treeNode, M)
return 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
John Kyle
  • 117
  • 2
  • 8

2 Answers2

1

Assuming the code for your BST is written correctly, explain to me how you are actually deleting the node? When I look at your code, your call to deleteItem(root, word) doesn't do anything at all. Regardless of what happens, it will return the root, which will then be applied to root.

  • There's actually a part at the end that I didn't write yet. I updated my code with a pseudo code version of what I want the delete method to look like. – John Kyle Mar 31 '15 at 22:47
  • So I added a few more lines at the end of my code to see if that would work. – John Kyle Apr 01 '15 at 18:38
  • So I updated my code with another edit to the last few lines but it's still returning failed. What's wrong with my code? – John Kyle Apr 01 '15 at 23:17
  • The problem is, is that you still not technically doing anything. The problem is that when you get to the body of if (word.compareTo(r.item.getWord()) < 0), you should then call deleteItem(r.left, word) in the body so that you will then start the recursion move down the tree to the left child and then do the comparison. Same concept for if it is > 0. Also, the body of your else if(r.left != null && r.right != null) doesn't make sense. That basically says, if r has two children, check r again. That will give you an infinite loop. You need a terminating condition with word.compare(r) == 0 – Timothy Lee-o'connor Apr 02 '15 at 00:02
0

You deleteItem() method never removes the node from the tree. That's why it fails.

Check this answer and the Wikipedia article on Binary Search Trees to better understand how deletions should be done.

Community
  • 1
  • 1
Anderson Vieira
  • 8,919
  • 2
  • 37
  • 48
  • I updated my code the a pseudo code version of what I want the delete method to do and I added a few more lines at the end. Would that make the delete method actually delete the node? – John Kyle Apr 01 '15 at 18:38
  • Also with my new lines added on it stills says it failed. So what's wrong with my code? – John Kyle Apr 01 '15 at 19:15
  • An example of a problem in the code is what you do when `word.compareTo(r.item.getWord()) < 0`. You return `r` and do not keep searching for `word`. Instead, you should call `deleteItem()` on the left subtree. Check the answer I linked for a more thorough explanation. – Anderson Vieira Apr 01 '15 at 19:42
  • Ok so what I did is instead of returning r I said r.left = deleteItem(r.left, word); and then r.right if it's > 0 but it still fails so is the problem on those last few lines of the method? – John Kyle Apr 01 '15 at 20:00
  • I updated my code with changes to those last few lines and it's still giving me failed. What am I doing wrong? – John Kyle Apr 01 '15 at 23:17