#Contains method and recursion

42 messages · Page 1 of 1 (latest)

analog valley
#

Struggling to get my contains method to work. It is supposed to compare and return true if an element can be found in a binary tree.

charred pumiceBOT
#

This post has been reserved for your question.

Hey @analog valley! Please use /close or the Close Post button 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.

analog valley
#
    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?

rotund tundra
#

Like this: return contains(n.left, x);

analog valley
#

I dont think so?

analog valley
#

I mean I am new to it but that is how I understand it

rotund tundra
#

Yes, but when your call finishes, it's result is just ignored

analog valley
#

hmm right

rotund tundra
analog valley
#

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

rotund tundra
analog valley
#

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; 
}```
rotund tundra
#

That seems more like it

analog valley
#

Exactly the same

rotund tundra
#

Can you provide full code?

analog valley
#

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

rotund tundra
#

Sorry, I am not able to help you right now. I hope I will get to my computer in 30 mins

placid sorrel
#

But comp is?

placid sorrel
#

Do this.data = data

#

If that fixes it, loll

rotund tundra
#

Probably

#

Just saw this line lol