[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

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
0
DAJones
Asked:
DAJones
1 Solution
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now