#Contains method and recursion
42 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @analog valley! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
return contains(root, x);
}
private boolean contains(Node<E> n, E x) {
if (n == null) {
return false;
}
int compResult = comp.compare(n.data, x);
if (compResult == 0) {
return true;
} else if (compResult < 0) {
contains(n.left, x);
} else if (compResult > 0) {
contains(n.right, x);
}
return false;
}```
My method
private Node<E> root;
private Comparator<E> comp;
private int size;
// konstruktorer och övriga metoder
private static class Node<E> {
private E data;
private Node<E> left;
private Node<E> right;
private Node(E data) {
data = data;
left = right = null;
}
}
}```
The class
and what I am getting atm
I do understand that the last return statement is currently putting it to false every time
but I am not sure why it is not working with the true statement?
I think recursive methods' calls for n.left and n.right are supposed to return their result, aren't they?
Like this: return contains(n.left, x);
I dont think so?
I mean we do it recursively to go back to the top and check all the if's again, to see if they are equal at some point
I mean I am new to it but that is how I understand it
Yes, but when your call finishes, it's result is just ignored
hmm right
Try this
I did but it's still wrong because of my last return statement
I always get false currently. But If I remove it I am missing one return statement
You get false because when comparison happens, its result is ignored, no return was made and jvm proceeds further to return false statement
public boolean contains(E x) {
return contains(root, x);
}
private boolean contains(Node<E> n, E x) {
if (n == null) {
return false;
}
int compResult = comp.compare(n.data, x);
if (compResult == 0) {
return true;
} else if (compResult < 0) {
return contains(n.left, x);
} else if (compResult > 0) {
return contains(n.right, x);
}
return false;
}```
That seems more like it
Can you provide full code?
I have 😛
This is an "exercise" to help practise
private Node<E> root;
private Comparator<E> comp;
private int size;
// konstruktorer och övriga metoder
private static class Node<E> {
private E data;
private Node<E> left;
private Node<E> right;
private Node(E data) {
data = data;
left = right = null;
}
}
}```
return contains(root, x);
}
private boolean contains(Node<E> n, E x) {
if (n == null) {
return false;
}
int compResult = comp.compare(n.data, x);
if (compResult == 0) {
return true;
} else if (compResult < 0) {
return contains(n.left, x);
} else if (compResult > 0) {
return contains(n.right, x);
}
return false;
}```
And it says the contains methods have some faults
And I am supposed to correct them
Sorry, I am not able to help you right now. I hope I will get to my computer in 30 mins
data = data does nothing
Do this.data = data
If that fixes it, loll
Yes, it will
Probably
Just saw this line lol