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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:06:29+00:00 2026-05-18T00:06:29+00:00

I have a Person class, and I want to create a tree. Here is

  • 0

I have a Person class, and I want to create a tree. Here is the contsructor for the Person class.

public Person(String name, int age, char gender, Person c1, Person c2)

c1 is the child on the left, and c2 is the child on the right. And so say I create three Persons like so:

Person c = new Person("Carl", 50, 'M', null, f);

Person b = new Person("Barbara", 52, 'F', d, e);

Person a = new Person("Adam", 75, 'M', b, c);

So here you say Adam is the root node, and Adam’s left child is b, which is Barbara and his right c which is Carl, and so on.

So what I want to do is write a count method, that counts the number of children including this. So a.count() would return 6 (if Person f doesnt have any children).

And so here’s the code I have:

public int count() // total person count including this object
{
    if(child1==null)
        return 0; //I tried return 1 for this too didnt work
    if (child2==null)
        return 0; //also tried 1 for this
    return 1+this.child1.count() +1+this.child2.count();
}

I ran this on paper several times, and it should come up with the correct result, but it’s off by a few for some reason when I actually run it.

  • 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-18T00:06:30+00:00Added an answer on May 18, 2026 at 12:06 am

    Your code returns 0 if one of the children are null. This is incorrect because you don’t account for the other child, or this. The count should always be >= 1 because you always have at least one node in the tree.

    Also, you can’t return right away if you see that one child is null. You need to count the other child too (if it exists).

    Here is how I would implement it:

    public int count() // total person count including this object
    {
        int count = 1; // we count this node as 1 
        if (child1 != null) // if we have a left child, count its size
            count += child1.count();
        if (child2 != null) // if we have a right child, count its size
            count += child2.count()
        return count;
    }
    

    You need to account for both children, even if one of them is null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like this: class person { public string Name{get;set;} public byte[]
Let's say I have a class Person: public class Person { public string Name
Example-I have a person class Public Class Person Private _fname As String Public Property
I have a class Person i.e. the definition of Person with name and age.
Given the classes: public class Person { public string Name { get; set; }
I have a domain class: class Person { String name Boolean likesGrails Boolean isSmart
Say i have the following: public class Person{ public int ID {get; set;} public
I have the following classes public class Person { public virtual int Id {
I have the following classes public class Person { public virtual int Id {
I have a class with primitive fields like the following: public class Person{ String

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.