-1

Ok, so I wanted to write a delete method that works for any "delete-case" for Binary Search Trees and this is my attempt:

 public void delete(int key) {
 if (root.value == key) {
   root = root.right;
   root.left = root.right.left;
   return;
 }
 else {
   deleteRecursive(key, root);  ***LINE 58***
 }

 }

 public void  deleteRecursive(int key, BinaryNode node) {
 if (node == null) {
   System.out.println("ERROR");
   return;
 }
 else {
   if (key < node.value) {
     // continue search on left
     if (node.left.value == key) {
       if (node.left.left == null && node.left.right == null) {
         node.left = null;     
       }
       else if (node.left.left == null){
         node.left = node.left.right;
       }
       else if (node.left.right == null){
         node.left = node.left.left;
       }
       else{
         node.left = node.left.right;
         node.left.left = node.left.right.left;  ***LINE 83***
       }
     }
     else {
       deleteRecursive (key, node.left);
     }
   }
   else if (key > node.value){
     // continue search on right
     if (node.right.value == key) {
       if (node.right.left == null && node.right.right == null) {
         node.right = null;
       }
       else if (node.right.left == null){
         node.right = node.right.right;
       }
       else if (node.right.right == null){
       }
       else if (node.left.left != null && node.left.right != null){
         node.right = node.right.right;
         node.right.left = node.right.right.left;
       }
     }
     else {
       deleteRecursive (key, node.right);
     }
   }
 }
}

But when I actually use the delete method in Main its gives out a NullPointerException.

The error-message reads :

Exception in thread "main" java.lang.NullPointerException
        at BinaryTree.deleteRecursive(BinaryTree.java:83)
        at BinaryTree.delete(BinaryTree.java:58)
        at Main.main(Main.java:14)

So I assume that it is in line 83 and 58 (marked in code).

I've been sitting here for the last hour trying to figure it out and can't seem to get it.

I'm not the best in Java so I thought i could look for some help here! :)

Here are all the files to run the program (everything except for the delete method was already given) : https://www.dropbox.com/sh/r1bt2880hnn6tjm/AADsRsOOzuiNKHp-ZC-IrvVta?dl=0

Rathan Naik
  • 993
  • 12
  • 25
Heavenpad
  • 1
  • 3
  • Which line invokes the error? That's important info. – Neo Oct 21 '17 at 20:15
  • The error-message reads : Exception in thread "main" java.lang.NullPointerException at BinaryTree.deleteRecursive(BinaryTree.java:83) at BinaryTree.delete(BinaryTree.java:58) at Main.main(Main.java:14) So I assume that it is in line 83 and 58 – Heavenpad Oct 21 '17 at 20:16
  • Put that in the question itself so it'll be easy to access. – Neo Oct 21 '17 at 20:18
  • Oh, we don't have your line numbering. Mark which lines are these. – Neo Oct 21 '17 at 20:20

1 Answers1

0

Since you are trying to remove from Binary Search Tree, better make it totally recursive rather than partially.

private BinaryNode deleteRecursive( int data,BinaryNode node) {
  if(node==null){
      return null;
  }
  if(data==node.value)
  {
      if(node.left!=null && node.right!=null){
          BinaryNode minNode = getHighestNodeFromRight(node.right);
          node.value = minNode.value;
          node.right = deleteRecursive(minNode.value, node.right);

          //System.out.println(minNode);
      }
      else if(node.left==null){
      return node.right;
      }
      else{
      return node.left;
      }
  }
  else if(data>node.value){
  node.right = deleteRecursive( data,node.right);
  }
  else{
      node.left = deleteRecursive(data, node.left);
  }
  return node;
}


public BinaryNode getHighestNodeFromRight(BinaryNode node){
      if(node.left==null){
          return node;
      }
      else{
          BinaryNode n = getHighestNodeFromRight(node.left);
          return n;
      }
}
Rathan Naik
  • 993
  • 12
  • 25
  • Thanks for the answer and effort! But unfortunately it still doesnt work and the same error message appears. I'm gonna try some more around and see if i get it down now with the new knowledge I've aquired. Thanks for the help everyone! I'll update if and when I get it working! – Heavenpad Oct 21 '17 at 20:36
  • @Heavenpad Can you post entire code along with data ? – Rathan Naik Oct 21 '17 at 20:39
  • ok I'm going to post all the files in the main question as a link. – Heavenpad Oct 21 '17 at 20:47
  • @Heavenpad I rewrote the code for you, let me know if you face any difficulty in understanding. – Rathan Naik Oct 21 '17 at 21:34
  • @Heavenpad Also what you are asking is Binary Search Tree, its not same as binary tree, so correct it. Binary Search tree has additional constraint than Binary tree – Rathan Naik Oct 21 '17 at 21:37
  • I don't know what happened but it still displays the same error?! could this be a problem with my editor or place i saved it at? I do understand what a NullPointerException is but I doubt it has anything to do with my editor program or the place of the file. – Heavenpad Oct 21 '17 at 22:03
  • @Heavenpad Definitely editor/build issue. Which IDE are you using ? – Rathan Naik Oct 21 '17 at 22:05
  • "Java-Editor" by Gerhard Röhner Version 9.16.0.997 – Heavenpad Oct 21 '17 at 22:16
  • @Heavenpad Well I am not familiar with it, but this should definitely work, delete all class files and jar files and then build/run – Rathan Naik Oct 21 '17 at 22:17
  • @Heavenpad Any response? – Rathan Naik Oct 25 '17 at 12:58