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

  • SEARCH
  • Home
  • 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 8160445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:14:36+00:00 2026-06-06T18:14:36+00:00

I wrote this n-array tree class. I want to write a method to add

  • 0

I wrote this n-array tree class. I want to write a method to add a child to a specific node in my tree.

First I should search my tree to find the father then add the child to that node children.
I don’t know how I should declare my method

public class FamilyNode {
    public String name;
    public String Family;
    public String sex;
    public FamilyNode Father;
    public FamilyNode Mother;
    public FamilyNode Spouse=null;
    public String status="alive";
    public int population;
    public ArrayList<FamilyNode> children=new ArrayList<FamilyNode>() ;


    public FamilyNode(String firstname,String lastname,String sex1){
        this.name=firstname;
        this.Family=lastname;
        this.sex=sex1;
        this.population=this.children.size()+1;
    }

    public void SetParents(FamilyNode father,FamilyNode mother){
        this.Father=father;
        this.Mother=mother;
    }

    public void SetHW(FamilyNode HW){
        this.Spouse=HW;
    }

    public int Number (){
        int number_of_descendants = this.population;

        if(this.Spouse!=null) number_of_descendants++;

        for(int index = 0; index < this.children.size(); index++)
            number_of_descendants = number_of_descendants+ this.children.get(index).Number();
            return number_of_descendants;
    }

    public void AddChild(FamilyNode Father,FamilyNode child){

        //the code here                                         
    }                                        
}
  • 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-06T18:14:38+00:00Added an answer on June 6, 2026 at 6:14 pm

    I answered one of your related questions yesterday so let’s continue with the code I posted 🙂

    public class FamilyNode {
        // ...
        // ...
        public FamilyNode findNodeByName(String nodeName){
           if(name.equals(nodeName)){
              // We found a node named nodeName, return it
              return this;
           } 
           // That's not me that you are looking for, let's see my kids
           for(FamilyNode child : children){
                if(child.findNodeByName(nodeName) != null) 
                    // We found what we are looking, just return from here
                    return child;
           }
           // Finished looping over all nodes and did not find any, return null
           return null;
        }
    
        public void addChild(FamilyNode child){
           children.add(child);
        }
    }
    

    Basically, you need to find the node you are looking for (by name in this case) and that can be done by the findNodeByName above. Once the node is found, add one child to it.

    Use this code like this:

    FamilyNode root = ...;
    FamilyNode node = root.findNodeByName("Parent");
    if(node != null) node.addChild(...);
    

    NOTE
    If you want to debug and visit all your tree nodes, use this method:

    public FamilyNode findNodeByName(String nodeName){
       System.out.println("Visiting node "+ name);
       // That's not me that you are looking for, let's see my kids
       for(FamilyNode child : children){
         child.findNodeByName(nodeName)
       }
       // Finished looping over all nodes and did not find any, return null
       return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write binary search tree's content to temporary array in order to
I wrote this function that uses a binary search to look for a specific
I am trying to write a class that implements a tree using an array
here's the stored procedure i wrote.In this proc p_subjectid is an array of numbers
I need to write a macro that operates on bitarray looking like this: array[0]
I wrote this method to check if a page exists or not: protected bool
I just wrote this exam, in which there was question: Consider a array of
I have wrote this code: $final = array( ($data[0] - $data[1]), ($data[1] - $data[2]),
How does unbindModel happen in cake? $this->User->unbindModel(array('hasAndBelongsToMany' => array('Friend'))); I wrote this in the
I wrote this code to scrape a sparse array from a series of dom

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.