Results 1 to 5 of 5

Thread: How to Count nodes in Binary Tree ?

  1. #1
    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

  2. #2
    Join Date
    Aug 2008
    Posts
    452

    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.

  3. #3
    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

  4. #4
    Join Date
    Aug 2008
    Posts
    452

    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);
        }
      }
    }

  5. #5
    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));
    }

Similar Threads

  1. Algorithm for counting number of nodes in a binary tree
    By anithachacko in forum Software Development
    Replies: 1
    Last Post: 20-04-2010, 01:20 PM
  2. JAVA binary tree
    By Daren in forum Software Development
    Replies: 4
    Last Post: 29-09-2009, 12:17 AM
  3. Replies: 3
    Last Post: 29-06-2009, 06:58 PM
  4. Dictionary with a binary tree
    By Lord in forum Software Development
    Replies: 4
    Last Post: 01-05-2009, 08:25 PM
  5. Algorithm to count in binary
    By Zool in forum Software Development
    Replies: 3
    Last Post: 28-04-2009, 01:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,711,928.62608 seconds with 16 queries