I implemented recursive algorihtm for deleting nodes in BST, but it seems not to work properly in case in which the node to be deleted has two children. Here is a code for method used to delete nodes:
public boolean delete(int val)
{
Node nodeToBeDeleted = find(val);
if(nodeToBeDeleted != null)
{
//case 1: node has no children
if(nodeToBeDeleted.leftChild == null && nodeToBeDeleted.rightChild == null)
deleteCase1(nodeToBeDeleted);
//case 3: node has two children
else if(nodeToBeDeleted.leftChild != null && nodeToBeDeleted.rightChild != null)
{
deleteCase3(nodeToBeDeleted);
}
//case 2: node has one child
else if(nodeToBeDeleted.leftChild != null)
{
//case 2 where left child should be deleted
deleteCase2(nodeToBeDeleted);
}
else if(nodeToBeDeleted.rightChild != null)
{
//case 2 where right child should be deleted
deleteCase2(nodeToBeDeleted);
}
return true;
}
else
return false;
}
And here deleteCase1, deleteCase2 and deleteCase3 methods:
private void deleteCase1(Node nodeToBeDeleted)
{
//check if node to be deleted is a left or a right child of the parent of the node to be deleted
if(nodeToBeDeleted.parent.leftChild == nodeToBeDeleted)
{
nodeToBeDeleted.parent.leftChild = null;
}
else if(nodeToBeDeleted.parent.rightChild == nodeToBeDeleted)
{
nodeToBeDeleted.parent.rightChild = null;
}
}
Here find method:
public Node find(int val)
{
if(root != null)
{
return findNode(root, new Node(val));
}
return null;
}
private Node findNode(Node search, Node node)
{
if(search == null)
return null;
if(search.data == node.data)
{
return search;
}
else
{
Node returnNode = findNode(search.leftChild, node);
if(returnNode == null)
{
returnNode = findNode(search.rightChild, node);
}
return returnNode;
}
}
minLeftTreversal method:
private Node minLeftTreversal(Node node)
{
if(node.leftChild == null)
return node;
return minLeftTreversal(node.leftChild);
}
Structure of the tree looks like this:
enter image description here
Algorithm works if I delete 75, but if I try to delete 25 it messes up.
Thank you in advance!
Your first if statement in public boolean delete(int val) is missing the { and }
//case 1: node has no children
if(nodeToBeDeleted.leftChild == null && nodeToBeDeleted.rightChild == null)
{ // <---- ADD THIS
deleteCase1(nodeToBeDeleted);
} // <---- AND ADD THIS
//case 3: node has two children
else if(nodeToBeDeleted.leftChild != null && nodeToBeDeleted.rightChild != null)
{
deleteCase3(nodeToBeDeleted);
}
Related
Could you tell me why this code wont delete a node in a BST? Is there any logical error in my code?
// To delete a node from the BST
public Node deleteNode(Node myRoot, int toDel) {
if (myRoot == null) return null;
else if (toDel < myRoot.data) myRoot.left = deleteNode(myRoot.left, toDel);
else if (toDel > myRoot.data) myRoot.right = deleteNode(myRoot.right, toDel);
else {
// Leaf node
if (myRoot.right == null && myRoot.left == null) {
myRoot = null;
} else if (myRoot.left == null) { // No left child
myRoot = myRoot.right;
} else if (myRoot.right==null){ // No right child
myRoot = myRoot.left;
}
}
return myRoot;
}
NOTE :- This code only deletes the nodes with one child or no child. I am currently working on deleting a node with 2 children so please dont solve that for me.
If 0 children, simply delete node (return null for it).
If there is 1 child, simply replace the node with the child that is not null.
public Node deleteNode(Node myRoot, int toDel) {
if (myRoot == null) {
return null;
} else if (myRoot.data == toDel) {
if (myRoot.left != null) {
return myRoot.left;
} else if (myRoot.right != null) {
return myRoot.right;
} else {
return null;
}
}
...
return myRoot;
}
so currently I’m trying to follow a tutorial from FreeCodeCamp on implementing a Binary tree, but I’m having trouble with adding to and traversing through my tree.
For some reason, it seems that I’m able to add nodes to my tree, but when I try to traverse through my tree via iterative preorder traversal, it only picks up my root node. Its as if my nodes aren’t pointing to each other.
I have a feeling that the problem either lies with my add method or my traversal method, both of which are below. Any help would be greatly appreciated.
Add method:
public boolean add(T thing)
{
if(contains(thing))
{
return false;
} else {
root = add(root,thing);
count++;
return true;
}
}
private Node add(Node node,T thing)
{
if(node == null)
{
node = new Node(thing,null,null);
} else
{
if(thing.compareTo(node.value) <0)
{
if(node.left == null)
{
node.left = node = new Node(thing,null,null);
} else{
node.left =add(node.left,thing);
}
}
else
{
if(node.right == null)
{
node.right = node = new Node(thing,null,null);
}else {
node.right = add(node.right,thing);
}
}
}
return node;
}
Traversal:
public void traverse()
{
preorder(root);
}
private void preorder(Node node)
{ int iteration=0;
java.util.Stack<Node> stack = new java.util.Stack<Node>();
System.out.println( "root is: " +node.value);
stack.push(node);
while(stack.empty() == false)
{
Node current = stack.pop();
System.out.println("in preorder: "+current.value);
if(current.right != null)
{
stack.push(current.right);
}
if(current.left != null)
{
stack.push(current.left);
}
iteration++;
}
System.out.println("iteration: "+iteration);
}
You are not traversing your tree while adding in the tree. Check my tree insert method to get the idea:-
void insert(Node temp,int value) {
if(temp==null){
temp=new Node(value,null,null);
this.root=temp;
}
else{
Queue<Node> q = new LinkedList<>();
q.add(temp);
while (!q.isEmpty()) {
temp = q.peek();
q.remove();
if (temp.left == null) {
temp.left = new Node(value, null, null);
break;
} else
q.add(temp.left);
if (temp.right == null) {
temp.right =new Node(value, null, null);
break;
} else
q.add(temp.right);
}
}
}
So I have a binary tree with data and I have currently written a remove method for the binary tree that should remove a node while still maintaining the binary tree as a balanced tree.
Currently I am doing the remove like this:
public Node remove(K key) {
return removeHelper(key, root, null);
}
public Node removeHelper(K key, Node targetNode, Node parent) {
if (targetNode == null) {
return targetNode;
} else {
if (key.compareTo(targetNode.key) == 0) {
// we have found the item we are looking for!
// CASE #1 - The target node does not have any children
if (targetNode.left == null && targetNode.right == null) {
if (parent != null) {
if (parent.left.equals(targetNode)) {
parent.left = null;
} else if (parent.right.equals(targetNode)) {
parent.right = null;
}
}
// CASE #2. target node only has a one child, right or left.
} else if (targetNode.left == null) {
targetNode = targetNode.right;
} else if (targetNode.right == null) {
targetNode = targetNode.left;
// CASE #3 - It has 2 children.
} else {
Node temp = targetNode;
Node rightMin = FindMin(temp.right);
targetNode.key = rightMin.key;
targetNode.value = rightMin.value;
}
} else if (key.compareTo(targetNode.key) < 0) {
return removeHelper(key, targetNode.left, targetNode);
} else if (key.compareTo(targetNode.key) > 0) {
return removeHelper(key, targetNode.right, targetNode);
} else {
return targetNode;
}
}
return targetNode;
}
and here is the FindMin method:
//Finding the minimum node in this subtree passed in
private Node FindMin(Node rootNode) {
if (rootNode.left == null) {
return rootNode;
} else {
return FindMin(rootNode.left);
}
}
And then I am trying to test the removal of nodes like such:
BinarySearchTree<Integer, String> tree = new BinarySearchTree<Integer, String>();
tree.put(40, "Forty");
tree.put(20, "Twenty");
tree.put(60, "Sixty");
tree.put(10, "Ten");
tree.put(30, "Thirty");
tree.put(50, "Fifty");
tree.put(70, "Seventy");
tree.put(4, "four");
tree.remove(70);
tree.remove(60);
System.out.println(tree);
And the output from this is:
(4:four)(10:Ten)(20:Twenty)(30:Thirty)(40:Forty)(50:Fifty)(60:Sixty)
As you can see, the 70 is being remove successfully but the 60 is not being removed for some reason. I tried changing around my code a bit but I usually run into a stackoverflow error when doing so.
Anyone see an issue or case I am missing here?
Hi I'm having trouble getting this code working correctly. It seems to be jumping out of the stack when it recurses all the way down the leftmost edge of the tree. I just can't seem to figure this one out.
public static Node lookup(Node node, int lookupValue) {
if (node == null) {
return null;
} else {
if (node.value == lookupValue) {
System.out.println("Found");
return node;
} else if(node.left != null) {
return lookup(node.left, lookupValue);
} else if(node.right != null) {
return lookup(node.right, lookupValue);
} else {
return null;
}
}
}
You return whatever is returned form the left sub-tree (if present) without checking the right one. A lot of the else branching isn't necessary when there is a return statement in the if block. Change as follows:
public static Node lookup(Node node, int lookupValue) {
if (node == null)
return null;
if (node.value == lookupValue)
// System.out.println("Found");
return node;
Node rval = lookup(node.left, lookupValue);
// only return if found in left sub-tree
return (rval != null) ? rval : lookup(node.right, lookupValue);
}
Your else if are not correct you chould check the left and right everytimes:
if (node == null) return null;
if (node.value == lookupValue) {
System.out.println("Found");
return node;
}
Node found = lookup(node.left, lookupValue);
if(found != null) {
return found;
}
return lookup(node.right, lookupValue);
I am doing an assignment, implementing own Binary Search Tree. The thing is, we have our own implementation of Node its parent is not directly accessible.
I have searched for answers, but I do not want to copy the solution entirely and I still don't seem to get it right, though. I miss some cases when the element is not removed.
Can you please help what am I doing wrong?
This is the remove method:
void remove(E elem) {
if(elem != null){
if (root != null && contains(elem)) {
removeFromSubtree(elem, root, null);
}
}
}
void removeFromSubtree(E elem, Node<E> current, Node<E> parent) {
if(elem.less(current.contents)){
if(current.left == null) return ;
removeFromSubtree(elem, current.left, current);
} else if(elem.greater(current.contents)){
if(current.right == null)return;
removeFromSubtree(elem, current.right, current);
} else {
if(current.left != null && current.right != null){
//both children
if(parent == null){
Node<E> n = new Node<>(null, null);
n.left = root;
removeFromSubtree(root.contents, n, null);
root = n.left;
root.setParent(null);
}
E min = subtreeMin(current.right);
current.contents = min;
removeFromSubtree(min, current.right, current);
} else if(current.left != null){
//left child
if (parent == null) {
root = current.left;
current.left.setParent(null);
return ;
}
setParentChild(current, parent, current.left);
} else if(current.right != null){
//right child
if (parent == null) {
root = current.right;
current.right.setParent(null);
return ;
}
setParentChild(current, parent, current.right);
} else {
if (parent == null) {
root = null;
return ;
}
setParentChild(current, parent, null);
}
}
}
Nodes use generic interface
class Node<E extends DSAComparable<E>>
which has just methods for comparation. It looks like this
interface DSAComparable<E extends DSAComparable<E>> {
boolean less(E other);
boolean greater(E other);
boolean equal(E other);
}
I use another methon inside remove that sets node's parent's child, depending if its left child or right child.
void setParentChild(Node<E> node, Node<E> parent,Node<E> value){
if(parent!= null){
if (parent.left == node) {
parent.left = value;
} else {
parent.right = value;
}
if(value!= null) value.setParent(parent);
}
}
Method subtreeMin(Node node) finds the smallest value in a subtree (the most left one)
Understanding your code is not so easy, since it still lacks of details.
I would refer to such an implementation of the Binary Search Tree that you can find online.
See for instance the one from Algorithms, 4th Ed..