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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:06:18+00:00 2026-05-12T10:06:18+00:00

Given a randomly distributed set of keys, with each key mapped to a set

  • 0

Given a randomly distributed set of keys, with each key mapped to a set of values, how would you transform it into multiple trees?

Example Data Set

  • NB2 => {NC2 ND2}
  • ND1 => {NG1 NH1}
  • NA1 => {NB1}
  • NB1 => {NC1 ND1 NE1}
  • NA2 => {NB2}
  • NC1 => {NF1}
  • NE1 => {NI1 NJ1 NK1}

Resulting Tree for NA1

NA1
`-- NB1
    |-- NC1
    |   `-- NF1
    |-- ND1
    |   |-- NG1
    |   `-- NH1
    `-- NE1
        |-- NI1
        |-- NJ1
        `-- NK1

Resulting Tree for NA2

NA2
`-- NB2
    |-- NC2
    `-- ND2
  • 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-12T10:06:18+00:00Added an answer on May 12, 2026 at 10:06 am

    I’m not aware of any library methods that will do this transformation. Here’s how I’d do it. It’s pretty straightforward, IMO.

    public class Tree {
        public Tree(String key) {
            // ...
        }
        public void addChild(Tree child) {
            // ...
        }
    }
    
    public Set<Tree> transform(Map<String, List<String>> input) {
        // Potential tree roots.  We start with all LHS keys as potential roots,
        // and eliminate them when we see their keys on the RHS.
        Set<String> roots = new HashSet<String>(input.keySet());
    
        // This map associates keys with the tree nodes that we create for them
        Map<String, Tree> map = new HashMap<String, Tree>();
    
        for (Map.Entry<String, List<String>> entry : input.entrySet()) {
            String key = entry.getKey();
            List<String> childKeys = entry.getValue();
            Tree tree = map.get(key);
            if (tree == null) {
                tree = new Tree(key);
                map.put(key, tree);
            }
            for (String childKey : childKeys) {
                roots.remove(childKey);
                Tree child = map.get(childKey);
                if (child == null) {
                    child = new Tree(childKey);
                    map.put(childKey, child);
                }
                tree.addChild(child);
            }
        }
        Set<Tree> res = new HashSet<Tree>(roots.size());
        for (String key : roots) {
            res.add(map.get(key));
        }
        return res;
    }
    

    EDIT: Note this algorithm will “work” if the input represents a set of DAGs (Directed Acyclic Graphs). However, I’ve just realized that the resulting a set of trees will share TreeNode instances for any common subtrees in the input data.

    Beware that I haven’t debugged this code 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 193k
  • Answers 193k
  • 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 There's no way to have an XPath query that returns… May 12, 2026 at 6:32 pm
  • Editorial Team
    Editorial Team added an answer There's already a $EDITOR variable, which is extremely standard and… May 12, 2026 at 6:32 pm
  • Editorial Team
    Editorial Team added an answer Reasons I have for scripting include When: you do it… May 12, 2026 at 6:32 pm

Related Questions

The expected probability of randomly selecting an element from a set of n elements
One of those classic programming interview questions... You are given two marbles, and told
I wish to implement my software on a shareware basis, so that the user
I'm am looking for specific suggestions or references to an algorithm and/or data structures

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.