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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:18:47+00:00 2026-06-18T02:18:47+00:00

I have tree node defined like this: class TreeNode : IEnumerable<TreeNode> { private readonly

  • 0

I have tree node defined like this:

class TreeNode : IEnumerable<TreeNode>
{
    private readonly Dictionary<string, TreeNode> _childs = new Dictionary<string, TreeNode>();

    public readonly string ID;

    public TreeNode Parent { get; private set; }

    public int Level { get; set; }

    public TreeNode(string id)
    {
        this.ID = id;
    }
    // some other methods
}

I created tree from keywords by this and now I have sometimes branch where parent tree node has one child and that child could have one child too and after some nodes there are 2 childs. So I want now to reduce all that one child (remove it) to “level” where there are at least 2 child nodes.

I tried something like this:

private void TreeReduction(TreeNode node)
    {
        while (node.Count() == 1)
        {
            node = node.GetFirstChild();
        }
        foreach (var child in node)
        {
            TreeReduction(child);
        }
    }

and I call it to main node. It’s look okay and it’s going through tree but that nodes aren’t rewrite. I tried out parameter for treenode but I have problem with foreach loop. How can I fix it to have it working? 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-06-18T02:18:48+00:00Added an answer on June 18, 2026 at 2:18 am

    Making many assumptions about what would happen, for example to single-branch single-leaf tree, you could go for something like this.

    var redux = TreeReduction(rootNode, 0);
    

    Aside this, the key point is for your recursive method to return a TreeNode which you will be able to set as childnode.

    I Left out the Parent property as its setter is private. If AddChild does not set it, you should make it public and carry it as a parameter.

    private TreeNode TreeReduction(TreeNode node, int currentLevel)
        {
            if(node==null)
              return null;
            if(node.Count() == 1)
            {
                var redux = TreeReduction(node.GetFirstChild(), currentLevel);
                return redux?? new TreeNode(node.ID{level=currentLevel});
            }
            var newNode = new TreeNode(node.ID{level=currentLevel});
            foreach (var child in node)
            {
                var newChild = TreeReduction(child, currentLevel+1);
                if(newChild!=null)
                   newNode.AddChild(newChild);
            }
            return newNode;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I have this very simple C++ class: class Tree { public: Node *head; };
I have this class called Node. I've been considering renaming it Tree, but either
Im having a problem with python.. I have a binary tree node type: class
I have a binary tree class that is created with a root node and
I have a tree in WPF that has no root node, but has defined
Say I have a tree defined as per the recommendation in this post ,
I have a dictionary-tree in an non-standard form like so: tree = {'0': {'A':
I have defined a class which represents a tree structure GeographicalUnits = Ext.extend(Ext.tree.TreePanel, {
I have a class Node ,then I created my tree node in my main

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.