Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8682427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:46:16+00:00 2026-06-12T21:46:16+00:00

I’m writing a binary tree class, and I’m stuck on a levelCount method, where

  • 0

I’m writing a binary tree class, and I’m stuck on a levelCount method, where I need to count the number of nodes on a level of the tree. The class and method look something like this:

public class ConsTree<T> extends BinaryTree<T>
{
   BinaryTree<T> left;
   BinaryTree<T> right;
   T data;

   public int levelCount(int level) 
   {
   }
}  

So the idea is that each tree has a tree to its left, a tree to its right, and data. There is an abstract class binarytree and subclasses ConsTree and EmptyTree.

I think I need to use a breadth first search and count the number of nodes once I get to that level, but I’m stuck on how to start. Any guidance here would be helpful. I can provide any other info necessary.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:46:18+00:00Added an answer on June 12, 2026 at 9:46 pm

    Here’s the general approach.

    You traverse the tree exactly as you would normally (depth-first, in-order) but you simply pass down the desired and actual level as well, with pseudo-code such as:

    def getCountAtLevel (node, curr, desired):
        # If this node doesn't exist, must be zero.
        if node == NULL: return 0
    
        # If this node is at desired level, must be one.
        if curr == desired: return 1
    
        # Otherwise sum of nodes at that level in left and right sub-trees.
        return getCountAtLevel (node.left,  curr+1, desired) +
               getCountAtLevel (node.right, curr+1, desired)
    
    #######################################################################
    # Get number of nodes at level 7 (root is level 0).
    nodesAtLevel7 = getCountAtLevel (rootNode, 0, 7)
    

    It doesn’t actually traverse the entire tree since, once it gets to the desired level, it can just ignore everything underneath that. Here’s a complete C program that shows this in action:

    #include <stdio.h>
    
    typedef struct _sNode { struct _sNode *left, *right; } tNode;
    
    // Node naming uses (t)op, (l)eft, and (r)ight.
    tNode TLLL = {NULL,  NULL    }; // level 3
    tNode TLLR = {NULL,  NULL    };
    tNode TRLL = {NULL,  NULL    };
    tNode TRLR = {NULL,  NULL    };
    tNode TRRR = {NULL,  NULL    };
    tNode TLL  = {&TLLL, &TLLR   }; // level 2
    tNode TRL  = {&TRLL, &TRLR   };
    tNode TRR  = {NULL,  &TRRR   };
    tNode TL   = {&TLL,  NULL    }; // level 1
    tNode TR   = {&TRL,  &TRR    };
    tNode T    = {&TL,   &TR     }; // level 0 (root)
    
    static int getCAL (tNode *node, int curr, int desired) {
        if (node == NULL) return 0;
        if (curr == desired) return 1;
        return getCAL (node->left,  curr+1, desired) +
               getCAL (node->right, curr+1, desired);
    }
    
    int main (void) {
        for (int i = 0; i < 5; i++) {
            int count = getCAL(&T, 0, i);
            printf ("Level %d has %d node%s\n", i, count, (count == 1) ? "" : "s");
        }
        return 0;
    }
    

    It builds a tree of the following form (where T means top, L is a left branch and R is a right branch):

                ______T______               (1 node)
               /             \
             TL               TR            (2 nodes)
            /                /  \
         TLL              TRL    TRR        (3 nodes)
        /   \            /   \      \
    TLLL     TLLR    TRLL     TRLR   TRRR   (5 nodes)
    
                                            (0 nodes)
    

    If you compile and run that code, you’ll see it gives the correct node count at each level:

    Level 0 has 1 node
    Level 1 has 2 nodes
    Level 2 has 3 nodes
    Level 3 has 5 nodes
    Level 4 has 0 nodes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.