将具有多个值的映射转换为树?

发布于 2024-08-02 04:24:47 字数 1011 浏览 4 评论 0原文

给定一组随机分布的键,每个键映射到一组值,如何将其转换为多棵树?

示例数据集

  • NB2 => {NC2 ND2}
  • ND1 => {NG1 NH1}
  • 不适用1 => {NB1}
  • NB1 => {NC1 ND1 NE1}
  • 不适用2 => {NB2}
  • NC1 => {NF1}
  • NE1 => {NI1 新泽西1 NK1}

NA 的结果树1

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

NA2的结果树

NA2
`-- NB2
    |-- NC2
    `-- ND2

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过度放纵 2024-08-09 04:24:47

我不知道有任何库方法可以进行这种转换。我就是这样做的。 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;
}

编辑:请注意,如果输入表示一组 DAG(有向无环图),则该算法将“起作用”。然而,我刚刚意识到,生成的一组树将共享输入数据中任何公共子树的 TreeNode 实例。

请注意,我还没有调试这段代码:-)

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 :-)

活泼老夫 2024-08-09 04:24:47

当你谈到将它们转化为一组树时,你是在谈论它们在内存中的表示方法吗?

或者也许是我们用来遍历您的键和值集并将它们放入内存中表示的算法?

或者你在谈论图形表示?

When you talk about transforming them into a set of trees, are you talking about the method of representing them in memory?

Or perhaps the algorithm by which we would walk your set of keys and values and put them into that in-memory representation?

Or are you talking about a graphical representation?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文