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

The Archive Base Latest Questions

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

i have written this tree class for a familytree now i need a search

  • 0

i have written this tree class for a familytree

now i need a search method to find a node in my tree

its a n-ary tree that each node can have 0 to n children

the search method can search for a node or for two names includes node name and his/her father name

plz help me

public class FamilyNode {
 public String name;
 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 name1,String sex1){
this.name=name1;
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 void AddChild(FamilyNode child){
    child.SetParents(this.Father, this.Spouse);
    this.children.add(child);
    this.Spouse.children.add(child);
}

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

}
  • 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:13:33+00:00Added an answer on June 6, 2026 at 6:13 pm

    Have a look at Tree Traversal algorithms. This article covers them pretty well (using recursion and dequeues)
    Here is one way of traversing your tree

    public class FamilyNode {
        // ...
        // ...
        public void traverseNode(){
           if(name.equals("NameWeAreLookingFor")){
              // We found a node named "NameWeAreLookingFor"
              // return here if your are not interested in visiting children
           } 
           for(FamilyNode child : node.children){
                child.traverseNode();
           }
        }
    }
    

    To traverse all your tree, do the following:

    FamilyNode root = ...;
    root.traverseNode();
    

    Note that this method is using recursion. If your tree is very big then I suggest you go for using queues instead as recursion might end up in a StackOverFlow exception.

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

Sidebar

Related Questions

I have written this clone method for when the parent of the Employee class
I have a tree of nodes like this: class Node: next # the next
have written this little class, which generates a UUID every time an object of
I have written the following code for constructing a tree from its inorder and
I have an interface Tree and an abstract class RBTree which implements this interface.
I've written an n-ary tree ADT which works fine. However, I need to store
I have an app written in XUL which contains a custom tree view that
Context I have written a TreeList/TreeGrid (i.e. tree with columns) control that inherits the
I have written a code for finding diameter of Binary Tree. Need suggestions for
I have an app written in python that presents some of its data in

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.