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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:37:23+00:00 2026-05-23T07:37:23+00:00

I have run across this method in our code base and wonder what the

  • 0

I have run across this method in our code base and wonder what the Big O is. The method takes a flat list and creates a tree, assigning the Parent and Children values as it goes.

private void AddChildren(Group group, IEnumerable<Group> groups)
{
    foreach (var g in groups)
    {
        if (g.ParentId == group.Id)
        {
            g.Parent = group;
            group.Children.Add(g);
            AddChildren(g, groups);
        }
    }
}

It’s been a while since I have done Big O outside of identifying straight forward n^2 (or worse) methods, but my take on it goes like this:

  • We are iterating every node in the list, giving us n
  • We are using a conditional to process a subset of the items being iterated. There can be multiple matches here and don’t know how to express that number, or how it modifies the recursive call to AddChildren
  • We have some simple assignments, and I don’t know if that warrants a +1 modifier
  • We are recursing but it’s not for every item in the enclosing iteration

Just tossing something out there so I can see if I was in the ballpark:

n + (x * n)

where x is the number of matches in the if loop.

Any thoughts on what this actually is would be great, 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-23T07:37:24+00:00Added an answer on May 23, 2026 at 7:37 am

    Observe that the recursive function is only called once per parent-child relationship. In a tree structure with n nodes, there are n – 1 such relationships, so AddChildren() is called n times (including the initial call). In each call, the work performed by the method itself (excluding the recursive call) is O(n) due to the iteration. Hence, O(n^2) in total.

    You can improve the complexity to O(n) by putting all groups in a hashmap first and traverse the list once, looking up each parent node in the hashmap.

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

Sidebar

Related Questions

In a web application that I have run across, I found the following code
I have run across an XML Schema with the following definition: <xs:simpleType name=ClassRankType> <xs:restriction
I have recently run across these terms few times but I am quite confused
I have run into a bit of a tricky problem in some C++ code,
So I have run up against this problem a few times: I have some
I'm building an XML Deserializer for a project and I run across this type
While building several different projects in QtCreator, I have run across the following build
I have found this example on StackOverflow: var people = new List<Person> { new
I was microbenchmarking some code (please be nice) and came across this puzzle: when
I have often come across this snippet : General form of invokeLater is -

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.