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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:18:40+00:00 2026-05-14T20:18:40+00:00

So I have 2 interfaces: A node that can have children public interface INode

  • 0

So I have 2 interfaces:

A node that can have children

public interface INode
{
    IEnumeration<INode> Children { get; }
    void AddChild(INode node);
}

And a derived “Data Node” that can have data associated with it

public interface IDataNode<DataType> : INode
{
    DataType Data;
    IDataNode<DataType> FindNode(DataType dt);
}

Keep in mind that each node in the tree could have a different data type associated with it as its Data (because the INode.AddChild function just takes the base INode)

Here is the implementation of the IDataNode interface:

internal class DataNode<DataType> : IDataNode<DataType>
{
    List<INode> m_Children;

    DataNode(DataType dt)
    {
        Data = dt;
    }

    public IEnumerable<INode> Children
    {
        get { return m_Children; }
    }

    public void AddChild(INode node)
    {
        if (null == m_Children)
            m_Children = new List<INode>();

        m_Children.Add(node);
    }   

    public DataType Data { get; private set; }

Question is how do I implement the FindNode function without knowing what kinds of DataType I will encounter in the tree?

    public IDataNode<DataType> FindNode(DataType dt)
    {
        throw new NotImplementedException();    
    }
}

As you can imagine something like this will not work out

    public IDataNode<DataType> FindNode(DataType dt)
    {
        IDataNode<DataType> result = null;

        foreach (var child in Children)
        {
            if (child is IDataNode<DataType>)
            {
                var datachild = child as IDataNode<DataType>;

                if (datachild.Data.Equals(dt))
                {
                    result = child as IDataNode<DataType>;
                    break;
                }
            }
            else 
            {
                 // What??
            }

            // Need to recursively call FindNode on the child
            // but can't because it could have a different
            // DataType associated with it. Can't call FindNode 
            // on child because it is of type INode and not IDataNode
            result = child.FindNode(dt); // can't do this!

            if (null != result)
                break;
       }

       return result; 
    }

Is my only option to do this when I know what kinds of DataType a particular tree I use will have? Maybe I am going about this in the wrong way, so any tips are appreciated. Thanks!

  • 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-14T20:18:41+00:00Added an answer on May 14, 2026 at 8:18 pm

    First of all, you need to put the FindNode method in INode. Otherwise, you cannot find a node of some type DataType… before having found a node of type DataType. Even if you have a reference to an object that you know is a DataNode<X>, this won’t help you if someone tells you to find a DataNode<Y>.

    There are now two roads you may take: if you want DataNode to be templated, then you need to know all possible types of data in the tree at compile time. If you know that, you can use a generic DataNode. If there’s a chance that you may want to find a node with data of some type that will only become known to you at runtime (e.g. from the return value of some method that you do not control) then you cannot use generics.

    I will illustrate the generic solution below.

    public interface INode
    {
        IEnumerable<INode> Children { get; }
        IDataNode<DataType> FindNode<DataType>(DataType value);
        void AddChild(INode node);
    }
    
    public interface IDataNode<DataType> : INode
    {
        DataType Data { get; }
    }
    

    INode.FindNode could be implemented like this:

    public IDataNode<DataType> FindNode<DataType> (DataType value) {
        // If we are searching for ourselves, return this
        var self = this as IDataNode<DataType>;
        if (self != null && self.Data.Equals(value)) {
            return self;
        }
    
        // Otherwise:
        // 1. For each of our children, call FindNode on it. This will
        //    find the target node if it is our child, since each child
        //    will check if it is the node we look for, like we did above.
        // 2. If our child is not the one we are looking for, FindNode will
        //    continue looking into its own children (depth-first search).
        // 3. Return the first descendant that comes back and is not null.
        //    If no node is found, FirstOrDefault means we will return null.
        return this.children.Select(c => c.FindNode(value))
                            .FirstOrDefault(found => found != null);
    }
    

    I have to say that the above recursive implementation with LINQ tries perhaps to be too clever and is maybe not very easy to understand. It could always be written with foreach, to make it more clear.

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

Sidebar

Ask A Question

Stats

  • Questions 477k
  • Answers 477k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This answer might help: How to get email and their… May 16, 2026 at 5:11 am
  • Editorial Team
    Editorial Team added an answer If you Google around, you'll find various way to do… May 16, 2026 at 5:11 am
  • Editorial Team
    Editorial Team added an answer Just had to reload the string in the XML parser,… May 16, 2026 at 5:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.