11 Replies - 417 Views - Last Post: 29 November 2017 - 08:10 AM Rate Topic: -----

#1 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Mistake in my Count method

Posted 28 November 2017 - 04:42 PM

Hello!

I have written a count method (along with a helper count method). The method count accepts a String word,and must return the number of occurrences of that String in the binary search tree data structure. I have finally figured out how to use JUnit testing using my IDE. By creating a test class and then test methods. Anyway, after adding "hello" three times to my binary tree, and then calling the count method on it, it returned 1. I'm glad that I figured out that my count method isn't working. My method are below, I would really appreciate if someone could help me to see where I've gone wrong!! Thanks!

public int count(String word) 
 {
    if(rootNode == null)
    {
        //If root == null, tree is empty and nothing to count
        return 0;
    } 
    else 
    {
    //If the root has child nodes, call countHelper, passing the root node, 
    //the word to be found
    //and the initial number of word
    int n = countHelper(rootNode, word, 0);
    return n;
    }
}

private int countHelper(Node node, String word, int num)
{    
    if(node == null)
    {
        return num;
    } 
    else
    {
        //If the data is equal to 
        if(node.getData().equals(word)) 
        {
            //System.out.println(node.data);
            num++;
        }
        return countHelper(node.getLeft(), word, countHelper(node.getRight(), word, num));
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Mistake in my Count method

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: Mistake in my Count method

Posted 28 November 2017 - 06:31 PM

Can you confirm that there are three nodes in the BST? That should be the first assumption you check, by printing out a representation of the BST after performing the insertion operation.

After that, add some println() statements into your count() methods to get a better feel for what is going on at each recursive call.

It’s high time to learn some debugging techniques, which you will use for the rest of your career. ;)

By the way- you shouldn’t have to traverse the entire tree to count the number of instances of the String. A BST is ordered. Use that ordering to determine if you need to traverse the left child or the right child node.

This post has been edited by macosxnerd101: 28 November 2017 - 06:36 PM

Was This Post Helpful? 1
  • +
  • -

#3 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 849
  • View blog
  • Posts: 3,410
  • Joined: 13-June 14

Re: Mistake in my Count method

Posted 28 November 2017 - 10:34 PM

Can we see the test case too, please?
Was This Post Helpful? 0
  • +
  • -

#4 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Re: Mistake in my Count method

Posted 29 November 2017 - 02:51 AM

Hi,

I created a test unit class. Here is the test case/method I used to test the count()
    @Test
    public void testingCountd()
    {
        storingWords sw = new storingWords(4);
        wordStor1.add("H");
        assertEquals(, wordStor1.count("H"));
        wordStor1.add("H");
        assertEquals(, wordStor1.count("H"));
    }


So I added "H" twice, and then called count, and then count returned 1 and not 2.
Was This Post Helpful? 0
  • +
  • -

#5 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Re: Mistake in my Count method

Posted 29 November 2017 - 03:12 AM

View Postmacosxnerd101, on 28 November 2017 - 06:31 PM, said:

Can you confirm that there are three nodes in the BST? That should be the first assumption you check, by printing out a representation of the BST after performing the insertion operation.


Hi Macos,

Yes, I can confirm that when I tested it, I tested it with three nodes/three String literals!
I doubt I will ever have an career in programming :), all the same I will do my best to become better at debugging as I have a
feeling it will be saving my time greatly! Still going to keep testing my count method, not giving up yet!
Was This Post Helpful? 0
  • +
  • -

#6 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Re: Mistake in my Count method

Posted 29 November 2017 - 04:20 AM

Hey!

After numerously testing my count method with different String literals I went back and looked at my add method, along with its helper method. And realised that in the add method I was using the compare to method < > , so I changed it to >= <= as my binary tree has a method called count which returns the occurrences of a word in a tree (thus in this case must accept duplicate Strings).

My test case is:
    @Test
    public void testingCountMethod()
    {
        //Added hh twice, and count returns twice
        WordStore wordStor1 = new WordStore(3);
       [b] assertEquals(,wordStor1.count("hh"));[/b]
        wordStor1.add("hhh");
        wordStor1.add("hhhhh");
        wordStor1.add("hh");
        wordStor1.add("h");
        wordStor1.add("hh");
       [b] assertEquals(,wordStor1.count("hh"));[/b]
    }

After adding "hh" twice, it returns twice. However, this this test method is in unit test class which doesn't compile.
When I end the running test, count returns twice, which is great, I know the count method works. However, I want my test class to compile.

The compiler errors occur in the lines that I have made bold. Compile error for the bold lines "Illegal start of expression"
However, I created the test method above by right clicking on the test class, I didn't type the lines of code above, it was down automatically by me right clicking and keep on adding different string words. I would appreciate an insight into why the test class is giving my compiler error. Much appreciated.
Was This Post Helpful? 0
  • +
  • -

#7 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 849
  • View blog
  • Posts: 3,410
  • Joined: 13-June 14

Re: Mistake in my Count method

Posted 29 November 2017 - 05:18 AM

Your asserts are missing a value. You need to supply the value you expect, of course!

This post has been edited by ndc85430: 29 November 2017 - 05:19 AM

Was This Post Helpful? 1
  • +
  • -

#8 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Re: Mistake in my Count method

Posted 29 November 2017 - 05:22 AM

Hi!

I know that usually binary search tree should not allow duplicate values. In the method count that I have to implement it says:
"Returns the number of occurrences a String is in the collection" this insinuates that the same string is allowed to be multiple times in the binary tree structure.

I think I need to change my add method so that I can add the same word multiple times, I'm struggling with this. Please see my code below:
private void traverseAndAdd(Node node, Node nodeToAdd)
 {
     if(nodeToAdd.getData().compareTo(node.getData()) [b]<= 0)[/b]
     {
         //If the String we want to add is smaller in length to 
         //node already there, check and see if he left node is empty
         if(node.getLeftChild()==null)
         {
             //The node we want to add into the tree, add the nodeToAdd node
             //Adding the reference to the parent
             nodeToAdd.setParent(node);
             //Call the setter method to add the node
             node.setLeftChild(nodeToAdd);
         }
         else
         {//If the left node is not 
            traverseAndAdd(node.getLeft(), nodeToAdd);
         }
     }
     else if(nodeToAdd.getData().compareTo(node.getData())[b] >= 0)[/b]
     {
         if(node.getRight()==null)
         {
             //Keeping track of Parent node
             nodeToAdd.setParent(node);
             //Add the node on the right node
             node.setRight(nodeToAdd);
         }
         else
        {
            traverseAndAddNode(node.getRight(), nodeToAdd);
        }
     }      
 }
 

I'm thinking that the problem may lie with the inequality. I have spent so much time thinking and testing,
and when I test the code I'm getting java.lang.StackOverflowError after I add a couple of words.
I cannot tell you how much I would apppreciate some help that you!

View Postndc85430, on 29 November 2017 - 05:18 AM, said:

Your asserts are missing a value. You need to supply the value you expect, of course!


Yeah but that line is generated in the test class, I didn't write that line of code myself...
Was This Post Helpful? 0
  • +
  • -

#9 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 849
  • View blog
  • Posts: 3,410
  • Joined: 13-June 14

Re: Mistake in my Count method

Posted 29 November 2017 - 08:05 AM

View PostchloeCodes, on 29 November 2017 - 12:22 PM, said:

Yeah but that line is generated in the test class, I didn't write that line of code myself...


First of all, when you're learning stuff, you should probably avoid automatically generated code. That way, you get into the habit of seeing how things work without it all being done for you. In the specific case of the asserts, think about it: you need two things to compare for equality, don't you?
Was This Post Helpful? 0
  • +
  • -

#10 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Re: Mistake in my Count method

Posted 29 November 2017 - 08:08 AM

Yeah, I'm new to using my IDE. It was asking for the value I was expecting, it works now. Finally mastered how to use JUnit for testing. I was wondering if you could take a look at my above count/count helper method? Can you see any visible mistakes? I sometimes get errors? That would be brilliant. Thank you!
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: Mistake in my Count method

Posted 29 November 2017 - 08:09 AM

Again- try to debug your code. The first step is adding println() statements to help you see what is going on in your method, including where you are recursively invoking your method.
Was This Post Helpful? 0
  • +
  • -

#12 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Re: Mistake in my Count method

Posted 29 November 2017 - 08:10 AM

Thanks! I will keep going!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1