1

I have created a Binary Search Tree and am trying to delete specific nodes after adding them. I can successfully delete about 5 Nodes but when I try to delete a Node with id 109 it just ignores it and nothing happens. I have tried many methods to delete it and it just does not work.

myBinaryTree.deleteNode(myBinaryTree.root, 109);

Here is the delete method in my Binary Tree.

public Node deleteNode(Node root, int ID){

    if (root == null)  return root;
    if (ID < root.ID)
        root.leftChild = deleteNode(root.leftChild, ID);
    else if (ID > root.ID)
        root.rightChild = deleteNode(root.rightChild, ID);

    else
    {
        if (root.leftChild == null)
            return root.rightChild;
        else if (root.rightChild == null)
            return root.leftChild;

        root.ID = minValue(root.rightChild);
        root.rightChild = deleteNode(root.rightChild, root.ID);
    }

    return root;
}


int minValue(Node root)
{
    int minv = root.ID;
    while (root.leftChild != null)
    {
        minv = root.leftChild.ID;
        root = root.leftChild;
    }
    return minv;
}

And my Node:

public class Node {
    int ID;
    Dancer.Gender gender;
    int height;

    Node leftChild;
    Node rightChild;

    Node(int ID, Dancer.Gender gender, int height) {

        this.ID = ID;
        this.gender = gender;
        this.height = ID;

    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

}

The ID works as intended meaning the method deleteNode gets correct ID, it just does not delete it.

Here is a picture of a the tree I am trying to delete from:

enter image description here

If more information on how I add the nodes etc is needed then I can provide that aswell. It is just so wierd that it all works perfectly until I try to delete node with ID = 109.

Richard
  • 1,087
  • 18
  • 52
  • Did you debug through it? I would suspect `ID > root.ID` is not true, or not going to the right-hand side. – achAmháin Oct 04 '18 at 10:20
  • Something that you will probably have to fix later but still : `this.height = ID;` it's from Node constructor. – dbl Oct 04 '18 at 10:22
  • @achAmháin it ends up in `else if (root.rightChild == null) return root.leftChild` – Richard Oct 04 '18 at 10:29
  • @dbl it is intended at the moment but will do later on – Richard Oct 04 '18 at 10:30
  • So that means it is incorrectly going past `else if (ID > root.ID)`. Can you provide the `Node` code you pass to `deleteNode`? – achAmháin Oct 04 '18 at 10:34
  • @achAmháin actually I think I got it by just adding this line `waitingLineWithFemales.root =...`, even though I am not sure why – Richard Oct 04 '18 at 10:43

1 Answers1

2

Your code looks fine.

By the way, how did you verify that the node was not deleted ? I just checked your code and printed inorder traversal. And it works fine.

// This is java code.
void inorder(Node root){
    if (root ==null)return;
    inorder(root.leftChild);
    System.out.print(root.ID + "  ");
    inorder(root.rightChild);
}

// verify deletion by printing inorder traversal before and after
public static void main(String[] args) {
    // creating the tree
    Node root = new Node(60);
    root.leftChild = new Node(40);
    root.rightChild = new Node(109);
    root.leftChild.leftChild = new Node(20);
    root.leftChild.rightChild = new Node(49);

    inorder(root); // Printing before deleting
    System.out.println();
    myBinaryTree.root = deleteNode(myBinaryTree.root, 109); // delete the node and collect the new reference of the root.
    inorder(root); // Printing after tree
    System.out.println();
}
Sumit Jha
  • 2,095
  • 2
  • 21
  • 36
  • By printing out the remaining Nodes and checking if 109 is still among them, I am not sure why but `waitingLineWithFemales.root =...` actually removed it from the tree, even tho it was not needed for the previous ones – Richard Oct 04 '18 at 10:38
  • could you show the code which for "printing out the remaining Nodes" ? – Sumit Jha Oct 04 '18 at 10:39
  • And by using your inorder it still leaves the 109 there, but the reference part worked – Richard Oct 04 '18 at 10:42
  • reference part worked, but just deleting without reference is still not working – Richard Oct 04 '18 at 10:46
  • But it is fine, I can use the reference, If you change your answer back to the reference part I can accept it – Richard Oct 04 '18 at 10:46