Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-03-2009
Member
 
Join Date: Mar 2009
Posts: 89
How to Count nodes in Binary Tree ?

Hello friends,

I want to find the total number of nodes in a binary tree structure at the same time i also need the the leftside and right side separately.I try to find over the Internet but didn't got a Solution.

Can anyone help me out with this.

Intel Core 2 Duo 2.8 Ghz
G31 Asrock Motherboard with Intel chipset
2gb RAM
256 Nvidia Graphic Card
200gb HDD
Reply With Quote
  #2  
Old 25-03-2009
Member
 
Join Date: Aug 2008
Posts: 447
Re: How to Count nodes in Binary Tree ?

Sure i will help you out with this issue before going ahead may i know which programming language are you using it.
Reply With Quote
  #3  
Old 25-03-2009
Member
 
Join Date: Mar 2009
Posts: 89
Re: How to Count nodes in Binary Tree ?

I am using Java as my Programming language.


Intel Core 2 Duo 2.8 Ghz
G31 Asrock Motherboard with Intel chipset
2gb RAM
256 Nvidia Graphic Card
200gb HDD
Edit/Delete Message
Reply With Quote
  #4  
Old 25-03-2009
Member
 
Join Date: Aug 2008
Posts: 447
Re: How to Count nodes in Binary Tree ?

Try to use this code i assume that you node class will work.


Code:
public static <T> int nodesAtFirstKLevels(BTNode<T> root, int k)
{
    if (root == null || k<=0)
    {
      return 0;
    } else {
        return 1 + count(root.left,k-1) + count(root.right,k-1);
    }
  }
}
Reply With Quote
  #5  
Old 23-04-2009
Member
 
Join Date: Nov 2008
Posts: 48
Re: How to Count nodes in Binary Tree ?

If you want it very simple you can use this too.

Code:
public static <T> int nodesAtFirstKLevels(BTNode<T> root, int k)
{
        return (root == null || k<=0) ? 0: (1 + count(root.left,k-1) + count(root.right,k-1));
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to Count nodes in Binary Tree ?"
Thread Thread Starter Forum Replies Last Post
Algorithm for counting number of nodes in a binary tree anithachacko Software Development 1 20-04-2010 01:20 PM
JAVA binary tree Daren Software Development 4 29-09-2009 12:17 AM
Count all files in a directory tree including subdirectories in Windows XP !const Operating Systems 3 29-06-2009 06:58 PM
Dictionary with a binary tree Lord Software Development 4 01-05-2009 08:25 PM
Algorithm to count in binary Zool Software Development 3 28-04-2009 01:22 PM


All times are GMT +5.5. The time now is 09:20 AM.