I am using a binary tree structure here. I am getting a "NullPointerException" from the line containing the while statement. I am completely confused about why that would be.
BinaryTreeNode<CharData> currNode = theTree.findValue(data);
// Move up the Binary Tree to create code.
while(currNode.getParent() != null) {
// The loop does some stuff that doesn't
// affect what is assigned to currNode.
// Move to the parent node for the next iteration.
currNode = currNode.getParent();
} // End the while loop.
return code; // Return the string of binary code.
Find value is a method from my BinaryTree class that searches for and finds the node containing specific data. I know this works from testing it separately outside of this implementation.
The only reason why the while-loop statement can throw a NPE is, when currNode is null. I suspect findValue() returned null.
I guess one fix (when you care about the topmost node) would be:
while(currentNode != null) {
rootNode = currentNode;
currentNode = currentNode.getParent();
}
Or the typical pattern which relies on boolean shortcut evaluation:
while(curentNode != null && currentNode.getParent() != null)
Or my prefered solution using guards:
if (currentNode == null)
throw NotFound(); // or return something
while(curentNode.getParent() != null) {
If you see the code:
BinaryTreeNode<CharData> currNode = theTree.findValue(data);
I guess, currNode is getting some value if findValue() able to search data else it is returning NULL values.
When it returns a NULL value it will throw NPE.
To avoid it, you can modify your code a little bit.
while(currNode != null && currNode.getParent != null) {
// your code here
}
Related
My problem is my delete method isn't deleting the node I want to delete and giving me a infinite loop.
public void delete(String name){
Node current = head;
boolean checker = false;
while(current != null && current.name != name && checker != true){
try{
if(current.name.equals(name)){
Node p = current.previous;
Node q = current.next;
/*The code can somehow get through only above this line, below here its not anymore*/
p.next = q;
q.previous = p;
System.out.println("Item successfully deleted.");
checker = true;
}else if(!current.name.equals(name) && current == tail){
System.out.println("Item not found.");
}
current = current.next;
} catch(NullPointerException e){}
}
}
Im here to asking for a hint or tip about my problem
(Sorry for my bad english)
You are checking if you have reached the end of the list current == tail but not breaking out of it. You can add a break statement inside your else if.
Other than that, you are using == to compare strings. I'm not sure why you added that there and it can be removed. Also, you must (almost always) never catch a NullPointerException.
"infinite loop" means your loop condition is incorrect, you are not making progress in each iteration, or there is a cycle your data. You use both current == null and current == tail to signify that it's the last element. Choice one way. Suggest you rewrite your loop condition to only deal with iteration, and have a conditional with a break if you have a match in the body:
for(current = head; current; current = current.next) {
if(current.name.equals(name)) {
if(current == head)
head = current.next
else
current.previous.next = current.next;
if(current == tail)
tail = current.previous;
else
current.next.previous = current.previous;
break;
}
// if tail.next is not initialized to null
// if(current == tail) break;
}
I see a potential infinite loop with no side effect here. If your list contain a node with node.name set to null then the invocation of current.name.equals(name) results in a NullPointerException. If you are at either end of the list the next or previous pointers will be null which will also result in the same exception. This exception is caught and discarded. Note that this prevents the advance of the current pointer which causes the same iteration to occur. At the very least make sure to print out the exception even if you're not taking any other action. It'll help with debugging.
Your while loop condition is overly complicated. while(current != null) should suffice given that:
Using if(current.name.equals(name)) removes the need for current.name != name. Also, don't use == or != for string comparison. It is a pointer comparison. Most equals methods take care of pointer comparisons.
Use a break or return for flow control here and remove checker boolean. The tail.next should always point to null to signify the end of the list. The only reason I see to have the checker boolean is if delete should remove all matching nodes and you want to know if it happened at least once. From what I see in the code that is not the case.
I would rewrite this as:
public void delete(String name){
Node current = head;
while(current != null){
try{
if(current.name.equals(name)){
...
return;
// Do not advance current here. Refer to finally block below.
}
} catch(NullPointerException e){
e.printStackTrace();
return; // If function should stop on error.
} finally {current = current.next;} // This prevents the repeat as it always happens.
}
System.out.println("Item not found.");
}
Note if you use "break" instead of "return" then the "Item not found." line will always print. You'd have to guard it with an if statement and a flag.
public void delete(String name){
Node current = head;
while(current != null){
if(current.name.equals(name)){
if(current.prev != null){
current.prev.next = current.next
}
if(current.next != null){
current.next.prev = current.prev
}
System.out.println("Removed node")
break;
}
current = current.next;
}
}
You could use this logic to delete the node that matches the name(given name is always present) if node is non null.
I am trying to write a "contains" method for a splay tree to figure out if a node is already in the tree. I give this method a node to start searching and a string key to use find the corresponding node. I think I have a pretty good handle on recursion, but I am stumped by this. I've bolded the two lines that are causing the infinite recursion, but I'm stuck because, unless you somehow have a tree with an infinite number of elements, wouldn't the left and/or right elements have to be null at some point? They cannot be != to null forever! I might be losing my mind but I would very much appreciate any clarification on how to create a stronger base case.
tldr: how is it possible for this function to recurse infinitely when we have to run into null at some point?!
public BST_Node containsNode(BST_Node node, String s) {
BST_Node result = null;
if (node == null) {
return null;
}
if (node.data.compareTo(s) == 0) {
splay(node);
return node;
}
if (node.left != null) {
result = containsNode(node.left, s); //recursion here
}
if (result == null && node.right != null) {
result = right.containsNode(node.right, s); //recursion here
}
return result;
}
}
I have two pieces of code that in my mind do the same thing but it doesn't.
I am trying to create an iterator for my custom set tree. Here's the code.
public LinkedList<AnyType> traverse (TheNode<AnyType> node,LinkedList<AnyType> theList){
if (node.left != null)
return traverse (node.left,theList);
theList.push(node.element);
if (node.right != null)
return traverse (node.right,theList);
return theList;
}
public void traverseNrTwo (TheNode<AnyType> node){
if (node.left != null){
traverseNrTwo (node.left);
}
list.push(node.element);
if (node.right != null){
traverseNrTwo (node.right);
}
}
traverse only goes through the left side of the tree and adds it to the list but traveseNrTwo goes through the whole tree. So, my question is, why do they do two different things?
You shouldn't return the result of the recursive calls, since it causes the recursion to visit just the left side of the tree.
public LinkedList<AnyType> traverse (TheNode<AnyType> node,LinkedList<AnyType> theList){
if (node.left != null)
traverse (node.left,theList); // if you return traverse(node.left,theList) here,
// you end the recursion without adding the current
// node and visiting the right sub-tree
theList.push(node.element);
if (node.right != null)
traverse (node.right,theList);
return theList;
}
Also note that since you are passing the LinkedList<AnyType> as an argument to your method (i.e. you are not creating a new LinkedList instance inside your method), you don't have to return it. You can simply change the return type to void.
I tried to do this using this logic but I am getting error
public static void removeLeaves(BinaryTreeNode<Integer> root) {
BinaryTreeNode<Integer> temp = root;
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
root = null;
}
removeLeaves(temp.left);
removeLeaves(temp.right);
}
Whole tree is getting printed as it is. Please help me out without changing my logic.
In your if statement
if(root.left==null && root.right==null)
{
BinaryTreeNode<Integer> temp = null;
root = temp;
}
You are declaring root as temp, which is null, then pass that temp into the removeLeaves function. So you are passing null into that function when the if statement is true.
BinaryTreeNode<Integer> temp = null;
root = temp;
basically sets root to null and then you call root.left => NPE
You might want to add the necessarily null checks, and probably rethink your function as a whole.
You need to check each root individually for null. Your if statement
if(root.left==null && root.right==null)
{
BinaryTreeNode<Integer> temp = null;
root = temp;
}
only checks if both roots are null. But say there is no left leaf (root.left is null) and there is only a right leaf, you still execute on the left side
removeLeaves(root.left);
However, we already know that root.left is null. I suggest adding individual checks before each call to removeLeaves() or modifying your existing if statement.
I'm not convinced this isn't a homework assignment, but here goes.
The null exception is because you are still calling removeLeaves() even when both the left and right sides are null. Have you tried putting an else after your main IF()?
public static void removeLeaves(BinaryTreeNode<Integer> root)
{
if(root == null)
{
return; // enclosing conditionals in braces is better form, saves lots of headaches later
}
if(root.left==null && root.right==null)
{
BinaryTreeNode<Integer> temp = null;
root = temp;
}
else //at least one is not null
{
removeLeaves(root.left);
removeLeaves(root.right);
}
}
I need to make a method that removes the last element of a LinkedList using recursion.
This is what I have so far but it doesn't seem to be removing the node...when i call list.size() it is still the same size with the same values. What am I doing wrong here?
This is for Java by the way
public void removeLastElement(Node curr){
if (curr == null)
return;
else{
if(curr.next == null)
curr = null;
else
removeLastElement(curr.next);
}
}
In a LinkedList to remove the last element you have to get the penultimate element and set
curr.next = null
You're in the right way to get the recurrent function to remove the last node. The problem is you're identifying the penultimate node with curr.next == null, if you got it, you nullify it, but that's your actual input! So, you must check if the actual node is the antepenultimate node on the list:
if (curr.next.next == null) {
curr.next = null; //Now you're modifying the data in your input.
}
With this change, there are more basic cases to check, but that's up to you, my friend.
Boolean deleteLast(Node n)
{
if(n.next == null)
return true;
if(deleteLast(n.next))
{
n.next = null;
return false;
}
return false;
}
Node deleteLast(Node n) {
if (n.next == null)
return null;
n.next = deleteLast(n.next);
return this;
}
The general idea is you ask the next node "hey, can you tell me where you are, and delete your last node?" The last node can then just say "I'm nowhere" and it'll all fall into place.
This is very similar to Aadi's answer, just using Nodes instead of booleans.