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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:23:33+00:00 2026-06-07T08:23:33+00:00

hi i have this code to search a n-ary tree but it dosent works

  • 0

hi i have this code to search a n-ary tree but it dosent works correctly and i dont know whats wrong with that
when searching n4 and n5 it return n3
whats wrong?
enter image description here

public FamilyNode findNodeByName(FamilyNode nodeName ){
       if(this.name.equals(nodeName.name)){
          // 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 : this.children){
            if(child.findNodeByName(nodeName) != null) 
              return child;
                // 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;
    }
  • 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-07T08:23:35+00:00Added an answer on June 7, 2026 at 8:23 am

    The reason is because you give back the node in which a node was found. Once a node is found, this node needs to be returned, and the parent nodes in case a FamilyNode was found needs to return the found FamilyNode. Check what is done with the found variable.

    You need to do something like this:

    FamilyNode found = child.findNodeByName(nodeName);
    if(found != null) 
        return found;
    

    The entire method will look like:

    public FamilyNode findNodeByName(FamilyNode nodeName ){
        if(this.name.equals(nodeName.name)){
            return this;
        } 
        for(FamilyNode child : this.children){
            FamilyNode found = child.findNodeByName(nodeName);
            if(found != null) 
                return found;
        }
        return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that works ok when search button is pressed, but if
i have this code that can do fast search on database. it works excellent
I have this code which works nice in Chrome and Firefox, but on IE
I have this code which iterates over a tree, doing a depth-first search. Every
I have this code to parse url string such as ?var=val but when search
I have this code about binary search tree , I want Calculated efficancy for
I have this code snippet, which works great somewhere else, but gives me a
i have this code to get the search resutls from the api: querygoogle.php: <?php
i have this code from a tutorial: // Do the search and show the
I have this PHP code to highlight the Query on search results. if (isset($_REQUEST['k'])){

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.