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 3271172
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:42:14+00:00 2026-05-17T18:42:14+00:00

I am trying to implement a method to count all nodes of a tree

  • 0

I am trying to implement a method to count all nodes of a tree from the root down. Basically I count the root then add the length of each of the roots child lists.

       public int size() 
       {
            int count = 1; //count the root node
            for (int i = 0; i < root.getChildren().size(); i++){
                count += (root.getChildren().get(i)).length() + 1;
            }
            return count;
        }

This is the solved solution.

  • 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-05-17T18:42:14+00:00Added an answer on May 17, 2026 at 6:42 pm

    You can implement the size() method as a member of ArrayTreeNode. Use recursion. The size for a node is 1 plus the sum of the children’s node sizes.

    So inside your size() method you have two cases:

    1. If the node is a leaf, return 1. Here is no recursive call.

    2. If the node has children, call the size() method of all the children, calculate the sum, add 1 for the node and return that value.

    Btw. why do you have both tree and root attributes in class ArrayTree? Isn’t a root node enough? Why do you have a separate class ArrayTree at all? An ArrayTreeNode for itself is already a tree.

    Where do you set the parent attribute of your ArrayTreeNode class? Wouldn’t it be best if you set the parent inside the addChild() method to ensure that parent is always valid?

    Update:

    Ok, you asked for an example. I think if you are not used to recursion it’s not so easy to get your head around it.

    This is a method of class ArrayTreeNode:

    public int size() {
      int sum = 1; // Count at least this node
    
      // Ask every child for its size. If this node is a leaf,
      // then no recursive call happens.
      // Otherwise call the size() method recursively for ervery 
      // child node. The child's size() method may also call its 
      // own childs size() method, adding another level of recursion.
      // But we can be sure that the recursion comes to an end because 
      // at every leaf the simple answer will be 1. 
      for(ArrayTreeNode<E> child: children) {
        sum += child.size();
      }
    
      // return our calculated size. 
      return sum;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a method for a binary tree which returns a stream.
I would like to implement the method User.calculate_hashed_password . I'm trying to use the
All I am currently trying implement something along the lines of dim l_stuff as
trying to implement a dialog-box style behaviour using a separate div section with all
Am trying to implement a generic way for reading sections from a config file.
I'm trying to implement generic method to put in a class a calculated value
I'm having trouble trying to implement a shared method/property between two classes created by
I am trying to implement a simple plist example from Beginning iPhone 3 Development
I'm trying to implement a simple char count of an UITextView while the user
I am trying to use implement the LSB lookup method suggested by Andrew Grant

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.