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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:50:46+00:00 2026-05-13T11:50:46+00:00

Edited for a larger tree, for more examples. I have a tree structure that

  • 0

Edited for a larger tree, for more examples.

I have a tree structure that I need to generate all of the possible permutations of, with some restrictions. Given a tree like this:

    A1----(B1, B2)
     \    
      \___(C1)
             \__(E1, E2)
/       
-  A2----(B3, B4)
\     \     \
       \     \__(D1)
        \
         \_(F1, F2)
                |
                (D4)   

    A3----(B5, B6)
                \__(D2, D3)

Or if this is a little vague here’s the same structure done with a Perl notation:

my $nodes = [
{
    name => 'A1',
    children => [
        [ { name => 'B1', children => []  }, 
          { name => 'B2', children => []  }
        ],
        [
          { name => 'C1', 
                    children => [
                        [
                            { name => 'E1', children => [] },
                            { name => 'E2', children => [] }
                        ]
                    ]
            }
        ]
    ]
},
{
    name => 'A2',
    children => [
        [ { name => 'B3', 
                children => [
                    [
                        { name => 'D1', children => [] }
                    ]
                ] 
          },
          { name => 'B4', children => [] }
        ],
        [
          { name => 'F1', children => [] },
          { name => 'F2', children => [
                        [ { name => 'D4', children => [] } ]
                    ]
            }
        ]
    ]
},
{
    name => 'A3',
    children => [
        [ { name => 'B5', children => [] },
          { name => 'B6', children => [
                    [ { name => 'D2', children => [] },
                      { name => 'D3', children => [] }
                    ] 
                ]                 
            }
        ]
    ]
}

];

(Frankly, if you can figure this out in readable Perl I’ll take that too.)

I’m looking to traverse the tree and retrieve all of the possible “paths” from the top level downward. All of the descendant groups of a node have to be represented by exactly 1 member in the “path”. For example, in A1 one of (B1, B2) and one of (C1) needs to be represented. So each path descending from A1 will begin with either:

A1 B1 C1

or

A1 B2 C1

If B1, B2, or C1 have children, those will need to be represented as well.

Working this out by hand for the above tree I get these possibilities:

A1 B1 C1 E1
A1 B1 C1 E2
A1 B2 C1 E1
A1 B2 C1 E2

A2 B3 D1 F1
A2 B3 D1 F2 D4
A2 B4 F1
A2 B4 F2 D4

A3 B5
A3 B6 D2
A3 B6 D3

Each node here is a DataRow object:

internal class DataRow
{
    internal string tag = "";
    internal int id = 0;
    internal Dictionary<string, List<DataRow>> children = null;

    internal DataRow(int id, string tagname)
    {
        this.tag = tagname;
        this.id = id;
    }        internal void AddChildren(string type, List<DataRow> drList)
    {
        if (children == null)
            children = new Dictionary<string, List<DataRow>>();
        if (!children.ContainsKey(type))
            children[type] = new List<DataRow>();
        children[type].AddRange(drList);
    }
    internal void AddChild(string type, DataRow dr)
    {
        List<DataRow> drList = new List<DataRow>();
        drList.Add(dr);
        AddChildren(type, drList);
    }
    public override string ToString()
    {
        return this.tag + " " + this.id;
    }
}

To build the sample tree above (except for the E and F levels, added later):

        DataRow fullList = new DataRow(null, "");
        DataRow dr, dr2, dr3;

        // First node above
        dr = new DataRow(1, "A");
        List<DataRow> drList = new List<DataRow>();
        drList.Add(new DataRow(1, "B"));
        drList.Add(new DataRow(2, "B"));
        dr.AddChildren("B", drList);
        drList.Clear();
        dr2 = new DataRow(1, "C");
        dr2.AddChild("C", new DataRow(1, "E"));
        dr2.AddChild("C", new DataRow(2, "E"));
        drList.Add(dr2);
        dr.AddChildren("C", drList);
        fullList.AddChild("A", dr);


        // Second Node above (without the "F" stuff)
        drList.Clear();
        dr = new DataRow(3, "B");
        dr.AddChild("D", new DataRow(1, "D"));
        drList.Add(dr);
        drList.Add(new DataRow(4, "B"));
        dr = new DataRow(2, "A");
        dr.AddChildren("B", drList);
        fullList.AddChild("A", dr);

        // Third node above
        drList.Clear();
        dr3 = new DataRow(6, "B");
        dr3.AddChild("B", new DataRow(2, "D"));
        dr3.AddChild("B", new DataRow(3, "D"));
        dr2 = new DataRow(3, "A");
        dr2.AddChild("B", new DataRow(5, "B"));
        dr2.AddChild("B", dr3);
        fullList.AddChild("A", dr2);

Walking the entire tree is trivial:

    internal void PermutedList()
    {
        if ( children == null ) return;
        foreach (string kidType in children.Keys)
        {
            foreach (DataRow dr in children[kidType])
            {
                dr.PermutedList();
            }
        }
    }

But that’s not what I need. This problem is a full tree walk, but in a specific order. What am I not getting? What kind of walk is this?

I’ve got a messy & slow implementation of this I wrote in Perl 10 years ago, but I can’t decipher my own code anymore (shame on me!).

Edit:
The graph and the lists below have been expanded, the code has not.

If I could describe the graph, I could program it. If I knew what it was called, I could look it up. But I can’t. So let me explain a little further.

The bucket names aren’t significant!

Each node has “buckets of children”. A1 has two buckets one containing “B” and another containing “C”. If that’s all it had (and C didn’t have buckets under it) I would have “A1 B1 C1” and “A1 B2 C1” — at least one representative from all of the child buckets present.

So I think each bucket needs the cross-product of its children (all the way down).

  • 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-13T11:50:46+00:00Added an answer on May 13, 2026 at 11:50 am

    Use the following sub:

    use 5.10.0;  # for // (aka defined-or)
    
    use subs 'enumerate';
    sub enumerate {
      my($root) = @_;
    
      my $kids = $root->{children};
      return [ $root->{name} // () ]
        unless @$kids;
    
      my @results;
      foreach my $node (@{ $kids->[0] }) {
        my @fronts = map [ $root->{name} // (), @$_ ],
                         enumerate $node;
    
        my @below = enumerate {
          name => undef,
          children => [ @{$kids}[1 .. $#$kids ] ],
        };
    
        foreach my $a (@fronts) {
          foreach my $b (@below) {
            push @results => [ @$a, @$b ];
          }
        }
      }
    
      @results;
    }
    

    You might print it with

    foreach my $tree (@$nodes) {
      foreach my $path (enumerate $tree) {
        print "@$path\n";
      }
    
      print "\n";
    }
    

    to get the following output:

    A1 B1 C1 E1
    A1 B1 C1 E2
    A1 B2 C1 E1
    A1 B2 C1 E2
    
    A2 B3 D1 F1
    A2 B3 D1 F2 D4
    A2 B4 F1
    A2 B4 F2 D4
    
    A3 B5
    A3 B6 D2
    A3 B6 D3

    I used $path above, but it will seriously confuse anyone who maintains your code because path has a well-understood meaning. You could skirt the naming issue entirely with a bit of cleverness:

    print join "\n" =>
          map { join "" => map "@$_\n", @$_ }
          map [ enumerate($_) ] => @$nodes;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 288k
  • Answers 288k
  • 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 Welcome to SO! You will need a Javascript function that… May 13, 2026 at 5:25 pm
  • Editorial Team
    Editorial Team added an answer Try to use seek (the -ss flag) instead of delaying… May 13, 2026 at 5:25 pm
  • Editorial Team
    Editorial Team added an answer You could just try "\n", I believe Environment.NewLine is "\r\n".… May 13, 2026 at 5:25 pm

Related Questions

In one of my current side projects, I am scanning through some text looking
Looking for a solution in bash (will be part of a larger script). Given
I am writing a C# .NET 2.0 .dll that is a plug in to
As a self-taught python hobbyist, how would I go about learning to import and
I can name three advantages to using double (or float ) instead of decimal

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.