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

The Archive Base Latest Questions

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

I want to implement a method that will enable me to find a node

  • 0

I want to implement a method that will enable me to find a node in a tree. The way I do it is recursively using global variables to know when to stop.

I have the class:

class Node    // represents a node in the tree
{ 
     // constructor
     public Node() {
          Children = new List<Node>();
     }

     public List<Node> Children; 
     public string Name;
     public string Content;            
}

And the method I have right now is:

    private bool IsNodeFound = false; // global variable that I use to decide when to stop

    // method to find a particular node in the tree
    private void Find(Node node, string stringToFind, Action<Node> foundNode)
    {
        if(IsNodeFound)
           return;

        if (node.Content.Contains(stringToFind)){
            foundNode(node); 
            IsNodeFound =true;               
        }

        foreach (var child in node.Children)
        {
            if (child.Content.Contains(stringToFind)){
                foundNode(node);
                IsNodeFound =true;               
            }

            Find(child, stringToFind, foundNode);
        }

    }

and the way I use the Find method is like:

   // root is a node that contain children and those children also contain children
   // root is the "root" of the tree
   IsNodeFound =false;
   Node nodeToFind = null;
   Find(root, "some string to look for", (x)=> nodeToFind=x);

So my question is how can I make this method more elegant. I will like the signature of the method to look like:

   public Node FindNode(Node rootNode);

I think it is to redundant what am I doing and there is probably a better way of creating that method. Or perhaps I could alter the Node class so that I can achieve the same thing with a linq query.

  • 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-06T07:13:44+00:00Added an answer on June 6, 2026 at 7:13 am

    I would do it this way:

    Write an instance method to generate a subtree of a node (you could make it an extension if you don’t control the Node class):

    public IEnumerable<Node> GetNodeAndDescendants() // Note that this method is lazy
    {
         return new[] { this }
                .Concat(Children.SelectMany(child => child.GetNodeAndDescendants()));    
    }
    

    Then you can just find nodes with a little bit of LINQ:

    var foundNode = rootNode.GetNodeAndDescendants()
                            .FirstOrDefault(node => node.Content.Contains(stringToFind));
    
    if(foundNode != null)
    {
        DoSomething(foundNode);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement a method that will find stuff in my custom class.
I want to implement a TableModel, that will enable my existing data structure to
I want to implement a method in java that could be used to categorize
i'm using ECLiPSe programming logic system. i want to implement the yield method for
I want to implement some sort of lookup table in C++ that will act
Do you know a quick way to implement method(s) from an Interface to a
Whats the best way to implement this. I am building application that will be
I am trying to implement an extension method that will work with linq2entities. I
I want to call a callback method that may or may not be implemented
I want to implement a generic method on a generic class which would allow

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.