#Deleting Nodes in a Binary Tree

1 messages · Page 1 of 1 (latest)

visual perch
#

Hi, I'm in a data structures course, not very good with java, and I'm having issues with one of my assignments. I have to delete a node from this tree of animals, but my previous code doesn't seem to update the original tree with the new tree, I went ahead and deleted it and filled the deleteNode method with the professor's comments to help but I'm still very confused. Note that the method is required to be return a boolean so I really have no idea what to do.

         BTNode<String> leaf = find(root,toDelete);
         BTNode<String> grandparent;
         BTNode<String> parent;
         if (leaf != null)
         {
        //find grandparent of given node     (leaf.getParent().getParent())
         //is the original node to the left or right of grandparent?
         //if leaf.getParent().getData().equals(grandparent.getLeft.getData()), then we are left, else we are right
         //if parent.right.data = me, i am right, else I am left - sibling is the other
         //set grandparent(branch l/r) = sibling (my parent.(other branch))
         //set sibling.parent = grandparent
         return true;
         } else {
         System.out.println("Animal not found");
         return false;
         }
    }```
valid hareBOT
# visual perch Hi, I'm in a data structures course, not very good with java, and I'm having iss...

Detected code, here are some useful tools:

Formatted code
public static boolean deleteNode(BTNode<String> root, String toDelete) {
  BTNode<String> leaf = find(root, toDelete);
  BTNode<String> grandparent;
  BTNode<String> parent;
  if (leaf != null ) {
    //find grandparent of given node     (leaf.getParent().getParent())
    //is the original node to the left or right of grandparent?
    //if leaf.getParent().getData().equals(grandparent.getLeft.getData()), then we are left, else we are right
    //if parent.right.data = me, i am right, else I am left - sibling is the other
    //set grandparent(branch l/r) = sibling (my parent.(other branch))
    //set sibling.parent = grandparent
    return true;
  }
  else {
    System.out.println("Animal not found");
    return false;
  }
}
valid hareBOT
#

<@&987246717831381062> please have a look, thanks.

valid hareBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

I'm sorry to hear that you're having trouble with your assignment. I'd be happy to help you with deleting a node from a binary tree in Java.

To delete a node from a binary tree, there are a few steps you need to follow:

  1. Start by finding the node you want to delete in the tree. You can do this by traversing the tree using either depth-first search (DFS) or breadth-first search (BFS) until you find the desired node.

  2. Once you've found the node, determine its parent node. This is necessary for updating the parent's reference to the deleted node.

  3. Next, consider three cases for deleting the node:

    • If the node has no children (i.e., it is a leaf), simply remove it by updating its parent's reference to null.
    • If the node has only one child, update its parent's reference to point directly to that child.
    • If the node has two children, find either its in-order predecessor or successor (i.e., the closest value that is smaller or larger than the value of the current node). Replace the value of the current node with that of its predecessor/successor and then recursively delete that predecessor/successor.

Here's an example implementation of deleting a node from a binary tree in Java:

#
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode(int val) {
        this.val = val;
        left = null;
        right = null;
    }
}

public class BinaryTree {
    private TreeNode root;

    public void deleteNode(int key) {
        root = deleteNode(root, key);
    }

    private TreeNode deleteNode(TreeNode root, int key) {
        if (root == null) {
            return null;
        }

        if (key < root.val) {
            root.left = deleteNode(root.left, key);
        } else if (key > root.val) {
            root.right = deleteNode(root.right, key);
        } else {
            if (root.left == null) {
                return root.right;
            } else if (root.right == null) {
                return root.left;
            }

            TreeNode successor = findSuccessor(root.right);
            root.val = successor.val;
            root.right = deleteNode(root.right, successor.val);
        }

        return root;
    }

    private TreeNode findSuccessor(TreeNode node) {
        while (node.left != null) {
            node = node.left;
        }
        return node;
    }
}
#

In this example, the deleteNode method is used to delete a node with a given key from the binary tree. The deleteNode method is called recursively to traverse the tree and find the desired node. The findSuccessor method is used to find the in-order successor of a given node.

I hope this helps you with your assignment! Let me know if you have any further questions.