binary tree search

I know this is something simple im overlooking, bu i teed to count all like elements in the tree. I get only one of them, if it is not in the root, I can get at most two of them in my test I dont understand why my recursion isnt working


   public int countElem(char c)
    {
      //case for an empty tree
       int count = 0;      
      if(emptyTree())
        return 0;
      //case for when node has no children and thus a leaf
      if(getRoot() == c)
      {
       System.out.println(getRoot() + " Here's one!");
        return 1;
      }
      //recursive call to left subtree
      if(getLeft() != null)    
        count += getLeft().countElem(c);
        //recursive call to right subtree
      if(getRight() != null)    
        count += getRight().countElem(c);
      return count;
    }

test program:

public class treeTest
{
  public static void main(String[] args)
  {
    System.out.println("creating a binary tree...");
    BinaryTree t = new BinaryTree();
    System.out.println();
    System.out.println("inserting Nodes...");
    t.insertNode('w', 0);
    t.insertNode('C', 0);
    t.insertNode('b', 1);
    t.getLeft().insertNode('D', 0);
    t.getLeft().getLeft().insertNode('A', 0);
    t.getLeft().getLeft().insertNode('V', 1);
    t.getRight().insertNode('A', 1);
    t.getRight().getRight().insertNode('b', 1);
    System.out.println(t);
    t1.insertNode('a', 0);
    System.out.println();
//    System.out.println(t.countLeaves());
 //   System.out.println();
    System.out.println(t.countA('A'));
  }
}
 result:
inserting Nodes...
w ( C ( D ( A (   )   (   ) )   ( V (   )   (   ) ) )   (   ) )   ( b (   )   ( A (   )   ( A (   )   (   ) ) ) )
A is a leaf
V is a leaf
A is a leaf
3
A Here's one!
A Here's one!
2
DAJonesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DAJonesAuthor Commented:
the last line of test should be
System.out.println(t.countElem('A'));
0
petmagdyCommented:
ok here we go:

   public int countElem(char c)
    {
      //case for an empty tree
       int count = 0;      
      if(emptyTree())
        return 0;
      //case for when node has no children and thus a leaf
      if(getRoot() == c)
      {
       System.out.println(getRoot() + " Here's one!");
        count++;  // <--- This is the line changed
      }
      //recursive call to left subtree
      if(getLeft() != null)    
        count += getLeft().countElem(c);
        //recursive call to right subtree
      if(getRight() != null)    
        count += getRight().countElem(c);
      return count;
    }

try it and tell me
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.